0% found this document useful (0 votes)
76 views58 pages

Operating System Experiments for B.Tech

Uploaded by

pavaniypr27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views58 pages

Operating System Experiments for B.Tech

Uploaded by

pavaniypr27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN

II [Link] – II SEMESTER –(R23)


OPERATING SYSTEM (2024 – 2025)

LIST OF EXPERIMENTS

1. Practicing of Basic UNIX Commands.

2. Write programs using the following UNIX operating system calls fork , exec , getpid ,
exit , wait , close , stat , opendir and readdir.
3. Simulate UNIX commands like cp , ls , grep etc.
4. Simulate the following CPU scheduling algorithms
(a)FCFS (b) SJF
5. Simulate the following CPU scheduling algorithms
(a)Priority (b) Round Robin
6. Control the number of ports opened by the operating system with
(a) Semaphores (b) Monitors
7. Write a program to illustrate concurrent execution of threads using pthreads library.
8. Write a program to solve producer – consumer problem using semaphores.
9. Implement the following memory allocation methods for fixed partition
(a)First fit (b) Worst fit
10. Implement the following memory allocation methods for fixed partition
(a)Worst fit (b) Best fit
11. Simulate the following page replacement algorithms
(a)FIFO (b) LRU
12. Simulate the following page replacement algorithms
(a)LRU (b) LFU
13. Simulate paging technique of memory management.
14. Implement bankers algorithm for dead lock avoidance and prevention.
15. Simulate the following file allocation strategies
(a)Sequential (b) Indexed
16. Simulate the following file allocation strategies
(a) Indexed (b) Linked
17. Download and install nachos operating system and experiment with it.
EXERCISE 1:

Practicing of Basic UNIX commands.


VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)

1. CAT command
General Syntax
cat [OPTION] [FILE]...
Create a File with Cat Command
We will create a file called test2 file with below command.
# cat >test2
Awaits input from user, type desired text and press CTRL+D (hold down Ctrl Key and type ‘d‘) to exit. The
text will be
written in test2 file.
Displaying Contents of File
In the below example, it will show contains of /etc/passwd file.
# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
bin:x:1:1:bin:/bin:/sbin/nologin
narad:x:500:500::/home/narad:/bin/bash
Use Cat Command with More & Less Options
If file having large number of contains that won’t fit in output terminal and screen scrolls up very fast, we
can use parameters more and less with cat command as show above.
# cat [Link] | more
# cat [Link] | less
Display Line Numbers in File
With -n option you could see the line numbers of a file [Link] in the output terminal.
# cat -n [Link]
1 "Heal The World"
2 There's A Place In
3 Your Heart
4 And I Know That It Is Love
5 And This Place Could
6 Be Much
7 Brighter Than Tomorrow
8 And If You Really Try
9 You'll Find There's No Need
Use Standard Output with Redirection Operator
We can redirect standard output of a file into a new file else existing file with ‘>‘ (greater than) symbol.
Careful, existing contains of test1 will be overwritten by contains of test file.
# cat test > test1
Appending Standard Output with Redirection Operator
Appends in existing file with ‘>>‘ (double greater than) symbol. Here, contains of test file will be appended
at the end of test1 file.
# cat test >> test1
Redirecting Multiple File contents in a Single File
This will create a file called test3 and all output will be redirected in a newly created file.
# cat test test1 test2 > test3
Sorting Contents of Multiple Files in a Single File
This will create a file test4 and output of cat command is piped to sort and result will be redirected in a
newly created file.
# cat test test1 test2 test3 | sort > test4
Sort Command
This command is used for sorting the lines of text files
Synatx
sort [OPTION]... [FILE]...
Options:
Mandatory arguments to long options are mandatory for short options too.
-d, --dictionary-order
consider only blanks and alphanumeric characters
-f, --ignore-case
fold lower case to upper case characters
-n, --numeric-sort
compare according to string numerical value
-r, --reverse
reverse the result of comparisons
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
-c, --check
check whether input is sorted; do not sort
-k, --key=POS1
Sorts the text based on the given key position
-o, --output=FILE
write result to FILE instead of standard output
Example
Sorting ls command output
$ ls -al | sort -n -k5
This results in the following ls command sorted output, which as you can see, is a directory listing, sorted by
filesize (the
5th column):
-rw-r--r-- 1 al al 0 Aug 17 2007 [Link]
total 992
-rw-r--r-- 1 al al 240 Aug 17 2007 files
drwxr-xr-x 11 al al 374 Jul 5 17:50 ..
-rw-r--r-- 1 al al 535 Aug 18 2007 [Link]
drwxr-xr-x 18 al al 612 Aug 18 2007 images
drwxr-xr-x 20 al al 680 Aug 27 2007 .
2. Cut and Paste Commands
cut command selects a list of columns or fields from one or more files.
Options
-c - specify a number for columns to cut
-f - specify a number for fields to cut.
Example
If a file named testfile contains
this is firstline
that is secondline
what is thirdline
Examples:
cut -f1 testfile will print this to standard output (screen)
this
that
what
paste command merge the lines of one or more files into vertical columns separated by a tab.
Example if a file named testfile contains
this is firstline
and a file named testfile2 contains
this is testfile2
then running this command
paste testfile testfile2 > outputfile
will put this into outputfile
this is firstline this is testfile2
It contains contents of both files in columns.
Example of combining both cut and paste commands on a table
cut -f 1 [Link] > [Link]
cut -f 2 [Link] > [Link]
cut -f 3 [Link] > [Link]
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
cut -f 4- > [Link]
Now, use the paste command to glue the parts together in the proper order.
paste [Link] [Link] [Link] [Link] > [Link]
3. SED command
SED command in UNIX is stands for stream editor and it can perform lot's of function on file like,
searching, find and replace, insertion or deletion. Sed is a Stream Editor used for modifying the files in unix (or
linux).
Whenever you want to make changes to the file automatically, sed comes in handy to do this.
Syntax:
Following is the general syntax for sed
sed [options] /pattern/action filename
Here, pattern is a regular expression, and action is one of the commands given in the following
table. If pattern is omitted, action is performed for every line as we have seen above.
The slash characters ( /) that surround the pattern are required because they are used as delimiters.

Regular Expressions in SED command


A regular expression is a pattern that is matched against a subject string from left to right. Most characters
are ordinary: they stand for themselves in a pattern, and match the corresponding characters in the subject.

The Substitution Command:


The substitution command, denoted by s, will substitute any string that you specify with any other
string that you specify.
The following command substitutes the first occurrence on a line of the string root with the string amrood.
$ cat /etc/passwd | sed 's/root/amrood/'
amrood:x:0:0:root user:/root:/bin/sh
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
It is very important to note that sed substitutes only the first occurrence on a line. If the string root occurs more
than once on a line only the first match will be replaced.
To tell sed to do a global substitution, add the letter g to the end of the command as follows:
$ cat /etc/passwd | sed 's/root/amrood/g'
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
amrood:x:0:0:amrood user:/amrood:/bin/sh
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
Examples:
Write the commands to the following
a)Login into the system
admin@svecwcsed211:~$ sshituser23@[Link]
Output:
ituser23.0.20.240’s password:ituser23
last login :Thu Dec 8 [Link] 2016 from [Link]
b)use the command to create a file comtaining the following [Link] it mytable
uses tabs to separate the fields.
1425 Ravi 15.65
4320 Ramu 26.27
6830 Sita 36.15
1450 Raju 21.86
$cat >mytable
Output:
1425 Ravi 15.65
4320 Ramu 26.27
6830 Sita 36.15
1450 Raju 21.86
c)use the cat command to display the file ,my table
$cat mytable
Output:
1425 Ravi 15.65
4320 Ramu 26.27
6830 Sita 36.15
1450 Raju 21.86
d)use the vi command to correct any errors in the file,my table
$ vi mytable
Output:
1425 Ravi 15.65
4320 Ramu 26.27
6830 Sita 36.15
1450 Raju 21.86
1340 Raji 18.04
1240 Jaya 16.04
e)use the sort command to sort the file my table according to the first field. Call the sorted file my table
$ sort mytable
Output:
1240 Jaya 16.04
1340 Raji 18.04
1425 Ravi 15.65
1450 Raju 21.86
4320 Ramu 26.27
6830 Sita 36.15
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
a)Write a sed command that delets the first character in each line in a file.
$ sed ‘s/^.//’ f1
Output:
xxxxxxx
computer
echnology
b)Write a sed command that deletes the character before the last character in each line in a file.
$ sed ‘s/.$//’ f1
Output:
computer
technolog
c)Write a sed command that swaps the first and second words in each line in a file.
$ sed ‘s/ \ ([a-z]*\) ([a-z]*\)/ \2, \1/’ f1
Output:
XXXX, computer, technology Section A 88888

EXERCISE 2:

Write programs using the following UNIX operating system calls opendir, readdir, closedir.

UNIX OPERATING SYSTEMS (OPENDIR, READDIR, CLOSEDIR)


#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
int main() {
DIR *dir;
struct dirent *entry;
// Open the current directory
dir = opendir(".");
if (dir == NULL) {
perror("opendir");
exit(EXIT_FAILURE);
}
// Read directory entries
printf("Contents of current directory:\n");
while ((entry = readdir(dir)) != NULL) {
printf("%s\n", entry->d_name);
}
// Close the directory
if (closedir(dir) == -1) {
perror("closedir");
exit(EXIT_FAILURE);
}
return 0;
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
}
Output
Contents of current directory:
.
main
main.c
..
stdin
[Execution complete with exit code 0]

EXERCISE 3:

Simulate UNIX commands like cp, Is, grep, etc.

/* 1. Simulation of ls command */
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
int main()
{
char dirname[10];
DIR *p;
struct dirent *d;
printf("Enter directory name ");
scanf("%s",dirname);
p=opendir(dirname);
if(p==NULL)
{
perror("Cannot find dir.");
exit(-1);
}
while(d=readdir(p))
printf("%s\n",d->d_name);
}
SAMPLE OUTPUT:
enter directory name iii
.
..
f2
f1

/* 2. Simulation of grep command */


#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
Int main()
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
{
char fn[10],pat[10],temp[200];
FILE *fp;
printf("\n Enter file name : ");
scanf("%s",fn);
printf("Enter the pattern to be searched : ");
scanf("%s",pat);
fp=fopen(fn,"r");
while(!feof(fp))
{
fgets(temp,1000,fp);
if(strstr(temp,pat))
printf("%s",temp);
}
fclose(fp);
}
SAMPLE OUTPUT:
enter file name: file4
enter the pattern to be searched: roll
roll no percentage grade
EXERCISE 4:

Simulate the following CPU scheduling algorithms: (a) FCFS


(b) SJF (c) Priority (d) Round Robin

(a) FCFS:
Aim: Write a C program to implement the various process scheduling mechanism such as FCFS scheduling.
Source Code:
#include<stdio.h>
int main()
{
int i, n, sum, wt, tat, twt, ttat;
int t[10];
float awt, atat;
printf("Enter number of processors:\n");
scanf("%d",&n);
for(i=0 ; i<n ; i++)
{
printf("\n Enter the Burst Time of the process %d",i+1);
scanf("\n %d", &t[i]);
}
printf("\n\n FIRST COME FIRST SERVE SCHEDULING ALGORITHM \n");
printf("\n Process ID \t Waiting Time \t Turn Around Time \n");
printf("1 \t\t 0 \t\t %d \n",t[0]);
sum = 0;
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
twt = 0;
ttat = t[0];
for(i=1 ; i<n ; i++)
{
sum += t[i-1];
wt = sum;
tat = sum + t[i];
twt = twt + wt;
ttat = ttat + tat;
printf("\n %d \t\t %d \t\t %d",i+1,wt,tat);
printf("\n\n");
}
awt = (float)twt/n;
atat = (float)ttat/n;
printf("\n Average Waiting Time %4.2f",awt);
printf("\n Average Turn Around Time %4.2f",atat);
return 0;
}

Output:
Enter number of processors:
3
Enter the Burst Time of the process 1124 24
Enter the Burst Time of the process 23
Enter the Burst Time of the process 33
FIRST COME FIRST SERVE SCHEDULING ALGORITHM

Process ID Waiting Time Turn Around Time


1 0 24
2 24 27
3 27 30
Average Waiting Time 17.00
Average Turn Around Time 27.00

(b) SJF :

Aim:
Write a C program to implement the various process scheduling mechanisms such
as SJF Scheduling.
Source Code:
#include<stdio.h>
void main()
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
{
int i, j, n, process[10], total=0, wtime[10], ptime[10], temp, ptemp;
float avg=0;
printf("\n Enter number of Processes:");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\n Enter Process %d ID:",i+1);
scanf("%d", &process[i]);
printf("\n Enter Process %d Time:",i+1);
scanf("%d", &ptime[i]);
}
for(i=0 ; i<n-1 ; i++)
{
for(j = i+1; j<n ; j++)
{
if(ptime[i] > ptime[j])
{
temp = ptime[i];
ptime[i] = ptime[j];
ptime[j] = temp;
ptemp = process[i];
process[i] = process[j];
process[j] = ptemp;
}
}
}
wtime[0] = 0;
for(i=1 ; i<n ; i++)
{
wtime[i] = wtime[i-1] + ptime[i-1];
total = total + wtime[i];
}
Avg = (float)total/n;
printf("\nP_ID\t P_TIME \t W_TIME \n");
for(i=0;i<n;i++)
printf("%d\t %d\t %d\n", process[i], ptime[i], wtime[i]);
printf("\nTotal Waiting Time: %d \n Average Waiting Time: %f", total, avg);
getch();
}
Output:
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
Enter number of Processes:4

Enter Process 1 ID:1

Enter Process 1 Time:6

Enter Process 2 ID:2

Enter Process 2 Time:8

Enter Process 3 ID:3

Enter Process 3 Time:7

Enter Process 4 ID:4

Enter Process 4 Time:3

P_ID P_TIME W_TIME

4 3 0

1 6 3

3 7 9

2 8 16

Total Waiting Time: 28

Average Waiting Time: 7.000000

(c) Priority :

Aim:Write a C program to implement the various process scheduling mechanisms such as Priority Scheduling.

Source Code:
#include <stdio.h>
void main()
{
int i, j, n, tat[10], wt[10], bt[10], pid[10], pr[10], t, twt=0, ttat=0;
float awt, atat;
printf("\n-----------PRIORITY SCHEDULING--------------\n");
printf("Enter the No of Process: ");
scanf("%d", &n);
for (i=0;i<n;i++)
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
{
pid[i] = i;
printf("Enter the Burst time of Pid %d : ",i);
scanf("%d",&bt[i]);
printf("Enter the Priority of Pid %d : ",i);
scanf ("%d",&pr[i]);
}
for (i=0 ; i<n ; i++)
for(j=i+1 ; j<n ; j++)
{
if (pr[i] > pr[j] )
{
t = pr[i];
pr[i] = pr[j];
pr[j] = t;
t = bt[i];
bt[i] = bt[j];
bt[j] = t;
t = pid[i];
pid[i] = pid[j];
pid[j] = t;
}
}
tat[0] = bt[0];
wt[0] = 0;
for (i=1;i<n;i++)
{
wt[i] = wt[i-1] + bt[i-1];
tat[i] = wt[i] + bt[i];
}
printf("\n---------------------------------------------------------------\n");
printf("Pid\t Priority\t Burst time\t WaitingTime\t Turn Arround Time\n");
printf("\n--------------------------------------------------------------\n");
for(i=0 ; i<n ; i++)
{
printf("\n %d \t\t %d \t %d \t\t %d \t\t %d ", pid[i], pr[i], bt[i], wt[i], tat[i]);
}
for(i=0;i<n;i++)
{
ttat = ttat + tat[i];
twt = twt + wt[i];
}
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
awt = (float)twt / n;
atat = (float)ttat / n;
printf("\n\[Link] Time: %f\[Link] Around Time: %f\n",awt,atat);
getch();
}
OUTPUT:

-----------PRIORITY SCHEDULING--------------

Enter the No of Process: 5

Enter the Burst time of Pid 0 : 10

Enter the Priority of Pid 0 : 3

Enter the Burst time of Pid 1 : 1

Enter the Priority of Pid 1 : 1

Enter the Burst time of Pid 2 : 2

Enter the Priority of Pid 2 : 4

Enter the Burst time of Pid 3 : 1

Enter the Priority of Pid 3 : 5

Enter the Burst time of Pid 4 : 5

Enter the Priority of Pid 4 : 2

---------------------------------------------------------------

Pid Priority Burst time WaitingTime Turn Arround Time

--------------------------------------------------------------

1 1 1 0 1

4 2 5 1 6

0 3 10 6 16

2 4 2 16 18

3 5 1 18 19

Avg. Waiting Time: 8.200000

[Link] Around Time: 12.000000


VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)

(d) Round Robin :

Aim:Write a C program to implement the various process scheduling mechanisms such as Round Robin
Scheduling.

Source Code:

#include<stdio.h>

main()

int st[10], bt[10], wt[10], tat[10], n, tq;

int i, count = 0, swt = 0, stat = 0, temp , sq = 0;


float awt = 0.0, atat = 0.0 ;
printf("Enter number of processes:");
scanf("%d",&n);
printf("Enter Burst time for sequences:");
for(i=0 ; i<n ; i++)
{
scanf("%d", &bt[i]);
st[i] = bt[i];
}
printf("Enter time quantum:");
scanf("%d",&tq);
while(1)
{
for(i=0, count = 0 ; i<n ; i++)
{
Temp = tq;
if(st[i] = = 0)
{
count++;
continue;
}
if(st[i] > tq)st[i] = st[i] - tq;
else
if(st[i] > = 0)
{
temp = st[i];
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
st[i]=0;
}
sq = sq + temp;
tat[i] = sq;
}
if(n = = count)
break ;
}
for(i=0 ; i<n ; i++)
{
wt[i] = tat[i] - bt[i];
swt = swt + wt[i];
stat = stat + tat[i];
}
awt = (float)swt/n;
atat = (float)stat/n;
printf("Process_no \t Burst time \t Wait time \t Turn Around Time\n");
for(i=0 ; i<n ; i++)
printf("%d \t %d \t %d \t %d\n", i+1, bt[i], wt[i], tat[i]);
printf("Avg wait time is %f \n Avg Turn Around Time is %f",awt,atat);
getch();
}
OUTPUT:

Enter number of processes:3


Enter Burst time for sequences:24 3 3
Enter time quantum:4
Process_no Burst time Wait time Turn Around Time
1 24 6 30
2 3 4 7
3 3 7 10
Avg wait time is 5.666667
Avg Turn Around Time is 15.666667

EXERCISE 5 :
Control the number of ports opened by the operating system with
(a) Semaphore (b) Monitors
(a) Semaphore :
Source code :
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
#include <pthread.h>

#define MAX_PORTS 5
#define NUM_THREADS 10

sem_t port_semaphore;

void init_semaphore(int value) {


sem_init(&port_semaphore, 0, value);
}

void open_port() {
sem_wait(&port_semaphore);
printf("Port opened\n");
// Simulate some work
sleep(2);
printf("Port closed\n");
sem_post(&port_semaphore);
}

void* thread_function(void* arg) {


open_port();
return NULL;
}

int main() {
init_semaphore(MAX_PORTS);

pthread_t threads[NUM_THREADS];
for (int i = 0; i < NUM_THREADS; i++) {
pthread_create(&threads[i], NULL, thread_function, NULL);
}

for (int i = 0; i < NUM_THREADS; i++) {


pthread_join(threads[i], NULL);
}

sem_destroy(&port_semaphore);
return 0;
}
OUTPUT :
Port opened
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
Port opened
Port opened
Port opened
Port opened
Port closed
Port opened
Port closed
Port closed
Port closed
Port closed
Port opened
Port opened
Port opened
Port opened
Port closed
Port closed

(b) Monitors :

Source code :

#include <pthread.h>

#include <stdio.h>

#include <stdlib.h>

#define MAX_PORTS 5

#define NUM_THREADS 10

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

int num_ports = 0;

void init_monitor() {

pthread_mutex_init(&mutex, NULL);
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
pthread_cond_init(&cond, NULL);

void open_port() {

pthread_mutex_lock(&mutex);

while (num_ports >= MAX_PORTS) {

pthread_cond_wait(&cond, &mutex);

num_ports++;

printf("Port opened\n");

pthread_mutex_unlock(&mutex);

void close_port() {

pthread_mutex_lock(&mutex);

num_ports--;

printf("Port closed\n");

pthread_cond_signal(&cond);

pthread_mutex_unlock(&mutex);

void* thread_function(void* arg) {

open_port();

// Simulate some work

sleep(2);

close_port();
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
return NULL;

int main() {

init_monitor();

pthread_t threads[NUM_THREADS];

for (int i = 0; i < NUM_THREADS; i++) {

pthread_create(&threads[i], NULL, thread_function, NULL);

for (int i = 0; i < NUM_THREADS; i++) {

pthread_join(threads[i], NULL);

return 0;

OUTPUT :

Port opened

Port opened

Port opened

Port opened

Port opened

Port closed

Port opened

Port closed
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
Port opened

Port closed

Port opened

Port closed

Port closed

Port opened

Port opened

Port closed

Port closed

Port closed

Port closed

Port closed

EXERCISE 6:

Write a program to illustrate concurrent execution of thread using pthread library.

#include<stdio.h>

#include<stdlib.h>

#include<pthread.h>

void *mythread1(void *vargp)

int i;

printf("thread1\n");

for(i=1;i<=10;i++)

printf("i=%d\n",i);

printf("exit from thread1\n");

return NULL;
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
}

void *mythread2(void *vargp)

int j;

printf("thread2 \n");

for(j=1;j<=10;j++)

printf("j=%d\n",j);

printf("Exit from thread2\n");

return NULL;

int main()

int pthread_tid,tid;

printf("before thread\n");

pthread_create(&tid,NULL,mythread1,NULL);

pthread_create(&tid,NULL,mythread2,NULL);

pthread_join(tid,NULL);

pthread_join(tid,NULL);

OUTPUT:

before thread

thread1

thread2

j=1

i=1

i=2
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
i=3

i=4

i=5

j=2

i=6

j=3

i=7

i=8

i=9

i=10

exit from thread1

j=4

j=5

j=6

j=7

j=8

j=9

j=10

Exit from thread2

EXERCISE 7:

Write a program to slove producer – consumer problem using semaphores.

#include<stdio.h>

#include<stdlib.h>

#include<unistd.h>

#include<time.h>
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
#include<sys/types.h>

#include<sys/ipc.h>

#include<sys/sem.h>

#define NUM_LOOPS 5

union semun{

int val;

struct semid_ds *buf;

unsigned short int *array;

struct seminfo *_buf;

};

int main(int argc, char* argv[])

int sem_set_id;

union semun sem_val;

int child_pid;

int i, rc;

struct sembuf sem_op;

struct timespec delay;

sem_set_id = semget(IPC_PRIVATE, 1, 0600);

if(sem_set_id == -1){

perror("main: semget");

exit(1);

printf("semaphore set created , semaphore set id '%d'.\n",sem_set_id);

printf("\n child process is the consumer");

printf("\n parent process is the producer\n");


VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
sem_val.val =0;

rc = semctl(sem_set_id, 0, SETVAL, sem_val);

child_pid = fork();

switch(child_pid){

case -1:

perror("fork");

exit(1);

case 0:

for(i=0;i<NUM_LOOPS;i++)

sem_op.sem_num =0;

sem_op.sem_op =1;

sem_op.sem_flg =0;

semop(sem_set_id,&sem_op,1);

printf("consumer : '%d'.\n",i);

fflush(stdout);

sleep(3);

break;

default:

for(i=0;i<NUM_LOOPS;i++)

printf("producer: '%d'.\n",i);

fflush(stdout);

sem_op.sem_num =0;

sem_op.sem_op =1;

sem_op.sem_flg =0;
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
semop(sem_set_id,&sem_op,1);

sleep(2);

if(rand() > 3 * (RAND_MAX/4))

delay.tv_sec = 0;

delay.tv_nsec = 10;

nanosleep(&delay,NULL);

break;

return 0;

OUTPUT:

Semaphore set created , semaphore set id ‘0’.

Child process is the consumer

Parent process is the producer

Producer : ‘0’.

Consumer : ‘0’.

Producer : ‘1’.

Producer : ‘2’.

Producer : ‘3’.

Producer : ‘4’.
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)

EXERCISE 8:

Implement the following memory allocation methods for fixed partitions.

(a)First fit (b)Worst fit (c) Best fit

(a) First fit :

#include<stdio.h>

#define max 5

int main()

int frag[max], b[max], f[max], i, j, nb, nf, temp, lowest;

static int bf[max], ff[max];

printf("\n\t Memory Management Scheme- First Fit");

printf("\n Enter the number of blocks:");

scanf("%d",&nb);

printf("Enterthe number of files:");

scanf("%d",&nf);

printf("\n Enter the size of the blocks:-\n");

for(i=1;i<=nb;i++)

printf("Block %d:",i);

scanf("%d",&b[i]);

printf("enter the size of the files:-\n");


VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
for(i=1;i<=nf;i++)

printf("File %d:",i);

scanf("%d",&f[i]);

for(i=1;i<=nf;i++)

for(j=1;j<=nb;j++)

if(bf[j]!=1)

temp = b[j]-f[i];

if(temp>=0)

if(lowest>temp)

ff[i] = j;

lowest = temp;

frag[i] = lowest;

bf[ff[i]] = 1;

lowest = 10000;

printf("\n File No\t File Size\t Block No\t Block size\t Fragment");

for(i=1;i<=nf&&ff[i]!=0;i++)
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
printf("\n %d\t\t %d\t\t %d\t\t %d\t\t %d", i, f[i], ff[i], b[ff[i]], frag[i]);

return 0;

OUTPUT:

Memory Management Scheme- First Fit

Enter the number of blocks:3

Enterthe number of files:2

Enter the size of the blocks:-

Block 1:5

Block 2:7

Block 3:4

enter the size of the files:-

File 1:1

File 2:4

File No File Size Block No Block size Fragment

1 1 2 2 1

2 4 1 5 1

(b) Worst fit :

#include<stdio.h>

#define max 5

int main()

int frag[max], b[max], f[max], i, j, nb, nf, temp, highest;

static int bf[max], ff[max];

printf("\n\t Memory Management Scheme- Worst Fit");

printf("\n Enter the number of blocks:");


VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
scanf("%d",&nb);

printf("Enterthe number of files:");

scanf("%d",&nf);

printf("\n Enter the size of the blocks:-\n");

for(i=1;i<=nb;i++)

printf("Block %d:",i);

scanf("%d",&b[i]);

printf("enter the size of the files:-\n");

for(i=1;i<=nf;i++)

printf("File %d:",i);

scanf("%d",&f[i]);

for(i=1;i<=nf;i++)

for(j=1;j<=nb;j++)

if(bf[j]!=1)

temp = b[j]-f[i];

if(temp>=0)

if(highest < temp)

ff[i] = j;
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
highest = temp;

frag[i] = highest;

bf[ff[i]] = 1;

highest = 0;

printf("\n File No\t File Size\t Block No\t Block size\t Fragment");

for(i=1;i<=nf;i++)

printf("\n %d\t\t %d\t\t %d\t\t %d\t\t %d", i, f[i], ff[i], b[ff[i]], frag[i]);

return 0;

OUTPUT :

Memory Management Scheme- Worst Fit

Enter the number of blocks:3

Enter the number of files:2

Enter the size of the blocks:-

Block 1:5

Block 2:2

Block 3:7

enter the size of the files:-

File 1:1

File 2:4
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
File No File Size Block No Block size Fragment

1 1 3 7 6

2 4 1 5 1

(c) BEST FIT:

#include<stdio.h>

#define max 5

int main()

int frag[max], b[max], f[max], i, j, nb, nf, temp, lowest= 10000;

static int bf[max], ff[max];

printf("\n\t Memory Management Scheme- Worst Fit");

printf("\n Enter the number of blocks:");

scanf("%d",&nb);

printf("Enter the number of files:");

scanf("%d",&nf);

printf("\n Enter the size of the blocks:-\n");

for(i=1;i<=nb;i++)

printf("Block %d:",i);

scanf("%d",&b[i]);

printf("enter the size of the files:-\n");

for(i=1;i<=nf;i++)

printf("File %d:",i);

scanf("%d",&f[i]);
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
}

for(i=1;i<=nf;i++)

for(j=1;j<=nb;j++)

if(bf[j]!=1)

temp = b[j]-f[i];

if(temp>=0)

if(lowest > temp)

ff[i] = j;

lowest = temp;

frag[i] = lowest;

bf[ff[i]] = 1;

lowest = 10000;

printf("\n File No\t File Size\t Block No\t Block size\t Fragment");

for(i=1;i<=nf&&ff[i]!=0;i++)

printf("\n %d\t\t %d\t\t %d\t\t %d\t\t %d", i, f[i], ff[i], b[ff[i]], frag[i]);

return 0;

OUTPUT :
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
Memory Management Scheme- Best Fit

Enter the number of blocks:3

Enter the number of files:2

Enter the size of the blocks:-

Block 1:5

Block 2:2

Block 3:7

enter the size of the files:-

File 1:1

File 2:4

File No File Size Block No Block size Fragment

1 1 2 2 1

2 4 1 5 1

EXERCISE 9:

Simulate the following page replacement algorithms

(a)FIFO (b) LRU (c) LFU

(a)FIFO :
Aim: To implement page replacement algorithms FIFO (First In First Out)
SOURCE CODE :

#include<stdio.h>

int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;

int main()
{
printf("\n \t\t\t FIFO PAGE REPLACEMENT ALGORITHM");
printf("\n Enter [Link] frames....");
scanf("%d",&nof);
printf("Enter number of reference string..\n");
scanf("%d",&nor);
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
printf("\n Enter the reference string..");
for(i=0;i<nor;i++)
scanf("%d",&ref[i]);
printf("\nThe given reference string:");
for(i=0;i<nor;i++)
printf("%4d",ref[i]);
for(i=1;i<=nof;i++)
frm[i]=-1;
printf("\n");
for(i=0;i<nor;i++)
{
flag=0;
printf("\n\t Reference np%d->\t",ref[i]);
for(j=0;j<nof;j++)
{
if(frm[j]==ref[i])
{
flag=1;
break;
}
}
if(flag==0)
{
pf++;
victim++;
victim=victim%nof;
frm[victim]=ref[i];
for(j=0;j<nof;j++)
printf("%4d",frm[j]);
}
}
printf("\n\n\t\t [Link] pages faults...%d",pf);
return 0;
}

OUTPUT :

FIFO PAGE REPLACEMENT ALGORITHM

Enter [Link] frames....3

Enter number of reference string..

5
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
Enter the reference string..6

The given reference string: 6 4 2 8 6

Reference np6-> 6 -1 -1

Reference np4-> 6 4 -1

Reference np2-> 6 4 2

Reference np8-> 8 4 2

Reference np6-> 8 6 2

[Link] pages faults…5

(b)LRU :

Aim: To implement page replacement algorithm LRU (Least Recently Used)


Source code:
#include<stdio.h>

int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;

int recent[10],lrucal[50],count=0;

int lruvictim();

int main()

printf("\n\t\t\t LRU PAGE REPLACEMENT ALGORITHM");

printf("\n Enter [Link] Frames....");

scanf("%d",&nof);

printf(" Enter [Link] reference string..");

scanf("%d",&nor);

printf("\n Enter reference string..");


VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
for(i=0;i<nor;i++)

scanf("%d",&ref[i]);

printf("\n\n\t\t LRU PAGE REPLACEMENT ALGORITHM ");

printf("\n\t The given reference string:");

printf("\n......................................");

for(i=0;i<nor;i++)

printf("%4d",ref[i]);

for(i=1;i<=nof;i++)

frm[i]=-1;

lrucal[i]=0;

for(i=0;i<10;i++)

recent[i]=0;

printf("\n");

for(i=0;i<nor;i++)

flag=0;

printf("\n\t Reference NO %d->\t",ref[i]);

for(j=0;j<nof;j++)

if(frm[j]==ref[i])

flag=1;

break;

}
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
}

if(flag==0)

count++;

if(count<=nof)

victim++;

else

victim=lruvictim();

pf++;

frm[victim]=ref[i];

for(j=0;j<nof;j++)

printf("%4d",frm[j]);

recent[ref[i]]=i;

printf("\n\n\t [Link] page faults...%d",pf);

getch();

int lruvictim()

int i,j,temp1,temp2;

for(i=0;i<nof;i++)

temp1=frm[i];

lrucal[i]=recent[temp1];

}
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
temp2=lrucal[0];

for(j=1;j<nof;j++)

if(temp2 > lrucal[j])

temp2 = lrucal[j];

for(i=0;i<nof;i++)

if(ref[temp2]==frm[i])

return i;

return 0;

OUTPUT :

LRU PAGE REPLACEMENT ALGORITHM

Enter [Link] Frames....3

Enter [Link] reference string..5

Enter reference string..4

LRU PAGE REPLACEMENT ALGORITHM

The given reference string:

…………………………………. 4 2 6 8 1

Reference NO 4-> 4 -1 -1

Reference NO 2-> 4 2 -1

Reference NO 6-> 4 2 6

Reference NO 8-> 8 2 6

Reference NO 1-> 8 1 6

[Link] page faults...5


VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
(c)LFU :

Aim: To implement page replacement algorithms like LFU (Least Frequently Used)
Source Code:
#include<stdio.h>

int main()

int total_frames, total_pages, hit = 0;

int pages[25], frame[10], arr[25], time[25];

int m, n, page, flag, k, minimum_time, temp;

printf("Enter Total Number of Pages:\t");

scanf("%d", &total_pages);

printf("Enter Total Number of Frames:\t");

scanf("%d", &total_frames);

for(m = 0; m < total_frames; m++)

frame[m] = -1;

for(m = 0; m < 25; m++)

arr[m] = 0;

printf("Enter Values of Reference String\n");

for(m = 0; m < total_pages; m++)

printf("Enter Value No.[%d]:\t", m + 1);

scanf("%d", &pages[m]);

}
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
printf("\n");

for(m = 0; m < total_pages; m++)

arr[pages[m]]++;

time[pages[m]] = m;

flag = 1;

k = frame[0];

for(n = 0; n < total_frames; n++)

if(frame[n] == -1 || frame[n] == pages[m])

if(frame[n] != -1)

hit++;

flag = 0;

frame[n] = pages[m];

break;

if(arr[k] > arr[frame[n]])

k = frame[n];

if(flag)

{
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
minimum_time = 25;

for(n = 0; n < total_frames; n++)

if(arr[frame[n]] == arr[k] && time[frame[n]] < minimum_time)

temp = n;

minimum_time = time[frame[n]];

arr[frame[temp]] = 0;

frame[temp] = pages[m];

for(n = 0; n < total_frames; n++)

printf("%d\t", frame[n]);

printf("\n");

printf("Page Hit:\t%d\n", hit);

return 0;

OUTPUT :

Enter Total Number of Pages:5

Enter Total Number of Frames: 6

Enter Values of Reference String

Enter Value No.[1]: 2


VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
Enter Value No.[2]: 4

Enter Value No.[3]: 1

Enter Value No.[4]: 3

Enter Value No.[5]: 7

2 -1 -1 -1 -1 -1

2 4 -1 -1 -1 -1

2 4 1 -1 -1 -1

2 4 1 3 -1 -1

2 4 1 3 7 -1

Page Hit: 0

EXERCISE 10:

Simulate paging technique of memory management

include<stdio.h>

int main()

int ms, ps, nop, np, rempages, i, j, x, y, pa, offset;

int s[10], fno[10][20];

printf("\n #Enter the memory size--");

scanf("%d", &ms);

printf("\n Enter the page size--");

scanf("%d", &ps);

nop = ms/ps;

printf("\n The [Link] pages available in memory are--%d",nop);

printf("\n Enter number of processes--");

scanf("%d",&np);

rempages = nop;
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
for(i=1;i<=np;i++)

printf("\n Enter [Link] pages required for p[%d]--",i);

scanf("%d",&s[i]);

if(s[i] > rempages)

printf("\n Memory is FULL");

break;

rempages = rempages - s[i];

printf("\n Enter page table for p[%d}--",i);

for(j=0;j<s[i];j++)

scanf("%d",&fno[i][j]);

printf("\n Enter Logical Address to find Physical Address");

printf("\n Enter process no. and page number and offset--");

scanf("%d%d%d",&x,&y,&offset);

if(x>np||y>=s[i]||offset>=ps)

printf("\n Invalid Processor Page Number or offset");

else

pa = fno[x][y]*ps+offset;

printf("\n The Physical Address is-- %d",pa);

return 0;

}
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
OUTPUT :

Enter the memory size--1000

Enter the page size--100

The [Link] pages available in memory are--10

Enter number of processes--3

Enter [Link] pages required for p[1]--4

Enter page table for p[1}--8

Enter [Link] pages required for p[2]--5

Enter page table for p[2}--1

Enter [Link] pages required for p[3]--5

Memory is FULL

Enter Logical Address to find Physical Address

Enter process no. and page number and offset--2 3 60

The Physical Address is—760

EXERCISE 11:

Implement bankers algorithm for deadlock avoidance and prevention.

#include<stdio.h>

int main()
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
{

int n, m, i, j, flag, k;

n = 5;

m = 3;

int alloc[5][3] = {{0,1,0},

{2,0,0},

{3,0,2},

{2,1,1},

{0,0,2}};

int max[5][3] = {{7,5,3},

{3,2,2},

{9,0,2},

{2,2,2},

{4,3,3}};

int avail[3] = {3,3,2};

int f[n], ans[n], ind = 0;

for(k=0;k<n;k++){

f[k] =0;

int need[n][m];

for(i=0;i<n;i++){

for(j=0;j<m;j++)

need[i][j] = max[i][j] - alloc[i][j];

int y=0;

for(k=0;k<5;k++)
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
{

for(i=0;i<n;i++)

if(f[i] == 0)

flag == 0;

for(j=0;j<m;j++){

if(need[i][j] > avail[j]){

flag = 1;

break;

if(flag == 0)

ans[ind++] = i;

for(y=0; y<m ; y++)

avail[y] += alloc[i][y];

f[i] = 1;

printf("\n Following is the SAFE sequence\n");

for(i=0;i<n-1;i++)

printf("p%d ->",ans[i]);

printf("p%d",ans[n-1]);
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
return 0;

OUTPUT:

Following is the SAFE sequence

p1 ->p3 ->p4 ->p0 ->p2

EXERCISE 12:

Simulate the following file allocation strategies (a) Sequential

(b) Indexed (c) Linked.

(a)SEQUENTIAL:

#include<stdio.h>

int main()

int n,i,j,b[20],sb[20],t[20],x,c[20][20];

printf("Enter [Link] files:");

scanf("%d",&n);

for(i=0;i<n;i++)

printf("Enter no. of blocks occupied by file%d:",i+1);

scanf("%d",&b[i]);

printf("Enter the starting block of file%d:",i+1);

scanf("%d",&sb[i]);

t[i]=sb[i];
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
for(j=0;j<b[i];j++)

c[i][j]=sb[i]++;

printf("Filename\tStart block\tlength\n");

for(i=0;i<n;i++)

printf("%d\t %d \t%d\n",i+1,t[i],b[i]);

printf("Enter file name:");

scanf("%d",&x);

printf("\nFile name is:%d",x);

printf("\nlength is:%d",b[x-1]);

printf("\nblocks occupied:");

for(i=0;i<b[x-1];i++)

printf("%4d",c[x-1][i]);

return 0;

OUTPUT :

Enter [Link] files:3

Enter no. of blocks occupied by file1:5

Enter the starting block of file1:1

Enter no. of blocks occupied by file2:8

Enter the starting block of file2:3

Enter no. of blocks occupied by file3:4

Enter the starting block of file3:3

Filename Start block length

1 1 5

2 3 8
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
3 3 4

Enter file name:2

File name is:2

length is:8

blocks occupied: 3 4 5 6 7 8 9 10

(b) Indexed :

#include<stdio.h>

int main()

int n,m[20],i,j,sb[20],s[20],b[20][20],x;

printf("Enter no. of files:");

scanf("%d",&n);

for(i=0;i<n;i++)

{ printf("Enter starting block and size of file%d:",i+1);

scanf("%d%d",&sb[i],&s[i]);

printf("\nEnter blocks occupied by file%d:",i+1);

scanf("%d",&m[i]);

printf("\nenter blocks of file%d:",i+1);

for(j=0;j<m[i];j++)

scanf("%d",&b[i][j]);

} printf("\nFile\t index\tlength\n");

for(i=0;i<n;i++)

printf("%d\t%d\t%d\n",i+1,sb[i],m[i]);

}printf("\nEnter file name:");

scanf("%d",&x);
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
printf("\nfile name is:%d\n",x);

i=x-1;

printf("\nIndex is:%d",sb[i]);

printf("\nBlock occupied are:");

for(j=0;j<m[i];j++)

printf("%3d",b[i][j]);

return 0;

OUTPUT :

Enter no. of files:3

Enter starting block and size of file1:1 6

Enter blocks occupied by file1:3

enter blocks of file1:1 2 3

Enter starting block and size of file2:2 3

Enter blocks occupied by file2:3

enter blocks of file2:1 2 3

Enter starting block and size of file3:9 5

Enter blocks occupied by file3: 1

enter blocks of file3: 3

File index length

1 1 3

2 2 3

3 9 1

Enter file name:3

file name is:3

Index is: 9
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
Block occupied are: 3

(c) Linked :

#include<stdio.h>

struct file

char fname[10];

int start,size,block[10];

f[10];

int main()

int i,j,n;

printf("Enter no. of files:");

scanf("%d",&n);

for(i=0;i<n;i++)

printf("Enter file name:");

scanf("%s", &f[i].fname);

printf("Enter starting block:");

scanf("%d", &f[i].start);

f[i].block[0]=f[i].start;

printf("Enter [Link] blocks:");

scanf("%d", &f[i].size);

printf("Enter block numbers:");

for(j=1;j<=f[i].size;j++)

{
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
scanf("%d", &f[i].block[j]);

printf("File\tstart\tsize\tblock\n");

for(i=0;i<n;i++)

printf("%s\t%d\t%d\t",f[i].fname,f[i].start,f[i].size);

for(j=1;j<=f[i].size-1;j++)

printf("%d--->",f[i].block[j]);

printf("%d",f[i].block[j]);

printf("\n");

return 0;

OUTPUT :

Enter no. of files: 3

Enter file name: swetha

Enter starting block: 20

Enter [Link] blocks: 6

Enter block numbers: 4

12 15 45 32 25

Enter file name: sathya

Enter starting block: 12

Enter [Link] blocks: 5

Enter block numbers: 6 5 4 3 2

Enter file name: venkat


VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
Enter starting block: 5

Enter [Link] blocks: 5

Enter block numbers: 4 2 6 8 3

File start size block

swetha 20 6 4--->12--->15--->45--->32--->25

pooja 12 5 6--->5--->4--->3--->2

bhumi 5 5 4--->2--->6--->8--->3

EXERCISE 13 :

Download and install nachos operating system and experiment with it.

What is Nachos?

Nachos (Not Another Completely Heuristic Operating System) is an educational operating system designed for
teaching purposes. It's commonly used in computer science courses to demonstrate core concepts of operating
systems such as processes, memory management, scheduling, and more.

Step-by-Step Guide to Download, Install, and Experiment with Nachos

Step 1: Set Up Your Environment

1. Choose a System:

- Nachos is best run on a Unix-like environment such as Linux or macOS. If you're using Windows, consider
installing Windows Subsystem for Linux (WSL) or using a Linux virtual machine.

2. Install Required Packages:

- You need the following development tools:

- g++: C++ compiler


VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
- make: Build automation tool

- flex: Scanner generator

- bison: Parser generator

On Ubuntu, you can install these using the following commands:

bash

sudo apt-get update

sudo apt-get install build-essential g++ make flex bison

Step 2: Download Nachos

1. Clone the Repository:

- Open a terminal and run:

bash

git clone [Link]

Note: The URL may point to a specific version of Nachos or another educational adaptation. Make sure to
access a suitable repository related to your course if needed.

2. Navigate to the Nachos Directory:

bash

cd chiventure Or navigate to the directory where Nachos was cloned

Step 3: Build Nachos

1. Compile the Source Code:

- Run the following command to build Nachos:

bash
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
make

This command compiles the Nachos source code and creates the executable files. If you encounter errors during
the build process, ensure that you have installed all the prerequisites correctly.

Step 4: Run Nachos

1. Execute the Nachos Binary:

- To start using Nachos, simply execute:

bash

./nachos

This command runs the Nachos simulator, and you’ll see the output of the default configuration.

Step 5: Experiment with Nachos

Here are a few ideas to get you started with experimentation:

1. Modify the Scheduler:

- Explore the scheduler implementation in the source code and try changing the scheduling algorithm (e.g., from
First-Come-First-Serve to Round Robin). Look in files related to the scheduling (usually under a directory
named like threads/ or userprog/).

2. Implement New System Calls:

- Add your own system calls to understand how they are processed and handled. This typically involves
modifying existing files that handle system call interfaces.
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
3. Run Provided Tests or Examples:

- Nachos may come with several example programs or test scripts. To run them, you can usually execute:

bash

make test

4. Read the Documentation:

- If available, read the comments and documentation within the codebase to understand how different
components interact. This will give you insights into the operating system's architecture.

Step 6: Clean Up

If you wish to remove Nachos after experimentation, simply delete the cloned directory:

bash

cd ..

rm -rf chiventure or the name of the directory where Nachos is cloned.

You might also like