Operating System Experiments for B.Tech
Operating System Experiments for B.Tech
LIST OF EXPERIMENTS
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:
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.
EXERCISE 2:
Write programs using the following UNIX operating system calls opendir, readdir, closedir.
EXERCISE 3:
/* 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
(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
(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
4 3 0
1 6 3
3 7 9
2 8 16
(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--------------
---------------------------------------------------------------
--------------------------------------------------------------
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
Aim:Write a C program to implement the various process scheduling mechanisms such as Round Robin
Scheduling.
Source Code:
#include<stdio.h>
main()
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 open_port() {
sem_wait(&port_semaphore);
printf("Port opened\n");
// Simulate some work
sleep(2);
printf("Port closed\n");
sem_post(&port_semaphore);
}
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);
}
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
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);
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);
open_port();
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];
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:
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
int i;
printf("thread1\n");
for(i=1;i<=10;i++)
printf("i=%d\n",i);
return NULL;
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
}
int j;
printf("thread2 \n");
for(j=1;j<=10;j++)
printf("j=%d\n",j);
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
j=4
j=5
j=6
j=7
j=8
j=9
j=10
EXERCISE 7:
#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;
};
int sem_set_id;
int child_pid;
int i, rc;
if(sem_set_id == -1){
perror("main: semget");
exit(1);
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);
delay.tv_sec = 0;
delay.tv_nsec = 10;
nanosleep(&delay,NULL);
break;
return 0;
OUTPUT:
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:
#include<stdio.h>
#define max 5
int main()
scanf("%d",&nb);
scanf("%d",&nf);
for(i=1;i<=nb;i++)
printf("Block %d:",i);
scanf("%d",&b[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:
Block 1:5
Block 2:7
Block 3:4
File 1:1
File 2:4
1 1 2 2 1
2 4 1 5 1
#include<stdio.h>
#define max 5
int main()
scanf("%d",&nf);
for(i=1;i<=nb;i++)
printf("Block %d:",i);
scanf("%d",&b[i]);
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)
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 :
Block 1:5
Block 2:2
Block 3:7
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
#include<stdio.h>
#define max 5
int main()
scanf("%d",&nb);
scanf("%d",&nf);
for(i=1;i<=nb;i++)
printf("Block %d:",i);
scanf("%d",&b[i]);
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)
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
Block 1:5
Block 2:2
Block 3:7
File 1:1
File 2:4
1 1 2 2 1
2 4 1 5 1
EXERCISE 9:
(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 :
5
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
Enter the reference string..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
(b)LRU :
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()
scanf("%d",&nof);
scanf("%d",&nor);
scanf("%d",&ref[i]);
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;
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;
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++)
temp2 = lrucal[j];
for(i=0;i<nof;i++)
if(ref[temp2]==frm[i])
return i;
return 0;
OUTPUT :
…………………………………. 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
Aim: To implement page replacement algorithms like LFU (Least Frequently Used)
Source Code:
#include<stdio.h>
int main()
scanf("%d", &total_pages);
scanf("%d", &total_frames);
frame[m] = -1;
arr[m] = 0;
scanf("%d", &pages[m]);
}
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
printf("\n");
arr[pages[m]]++;
time[pages[m]] = m;
flag = 1;
k = frame[0];
if(frame[n] != -1)
hit++;
flag = 0;
frame[n] = pages[m];
break;
k = frame[n];
if(flag)
{
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
minimum_time = 25;
temp = n;
minimum_time = time[frame[n]];
arr[frame[temp]] = 0;
frame[temp] = pages[m];
printf("%d\t", frame[n]);
printf("\n");
return 0;
OUTPUT :
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:
include<stdio.h>
int main()
scanf("%d", &ms);
scanf("%d", &ps);
nop = ms/ps;
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++)
scanf("%d",&s[i]);
break;
for(j=0;j<s[i];j++)
scanf("%d",&fno[i][j]);
scanf("%d%d%d",&x,&y,&offset);
if(x>np||y>=s[i]||offset>=ps)
else
pa = fno[x][y]*ps+offset;
return 0;
}
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
OUTPUT :
Memory is FULL
EXERCISE 11:
#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;
{2,0,0},
{3,0,2},
{2,1,1},
{0,0,2}};
{3,2,2},
{9,0,2},
{2,2,2},
{4,3,3}};
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++)
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++){
flag = 1;
break;
if(flag == 0)
ans[ind++] = i;
avail[y] += alloc[i][y];
f[i] = 1;
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:
EXERCISE 12:
(a)SEQUENTIAL:
#include<stdio.h>
int main()
int n,i,j,b[20],sb[20],t[20],x,c[20][20];
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&b[i]);
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]);
scanf("%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 :
1 1 5
2 3 8
VIJAYA INSTITUTE OF TECHNOLOGY FOR WOMEN
II [Link] – II SEMESTER –(R23)
OPERATING SYSTEM (2024 – 2025)
3 3 4
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;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d%d",&sb[i],&s[i]);
scanf("%d",&m[i]);
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]);
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]);
for(j=0;j<m[i];j++)
printf("%3d",b[i][j]);
return 0;
OUTPUT :
1 1 3
2 2 3
3 9 1
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;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%s", &f[i].fname);
scanf("%d", &f[i].start);
f[i].block[0]=f[i].start;
scanf("%d", &f[i].size);
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 :
12 15 45 32 25
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.
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.
bash
bash
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.
bash
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.
bash
./nachos
This command runs the Nachos simulator, and you’ll see the output of the default configuration.
- 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/).
- 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
- 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 ..