OS Lab Rec Programs-1
OS Lab Rec Programs-1
OUTPUT:
I am parent and ID : 3635
I am child and ID : 3636
PROGRAM:
PROGRAM: PROGRAM:
#include<stdio.h>
#include<stdio.h> #include<stdio.h>
#include<sys/wait.h>
#include<sys/wait.h>
#include<stdlib.h> #include<sys/stat.h>
#include<stdlib.h>
#include<unistd.h> #include<stdlib.h> #include<unistd.h>
void main() #include<dirent.h>
void main()
{ struct dirent *d;
{
void main()
int pid=fork();
char path[10]; {
if(pid == 0)
char buff[100];
struct stat *buf;
printf("I am child \n");
DIR *dptr;
else if(pid > 0) buf=(struct stat *)malloc(sizeof(struct
printf("Enter the directory\n");
stat));
{ scanf("%s",buff);
printf("Enter the path of file :"); dptr=opendir(buff);
printf("I am parent\n");
scanf("%s",path); if(dptr == NULL)
wait(NULL);
printf("Directory not
printf("Parent waited for stat(path,buf); found\n");
child\n"); else
printf("User ID : %d\n",buf->st_uid);
} {
rintf("Block Size :
p while(d=readdir(dptr))
else
ld\n",buf->st_blksize);
%
rintf("The child process
p printf("DIR name
:%s\n",d->d_name);
creation failed\n"); printf("Time : %ld\n",buf->st_atime);
closedir(dptr);
} rintf("Permission :
p }
d\n",buf->st_mode);
%
}
OUTPUT: rintf("Number of link :
p
ld\n",buf->st_nlink);
%
I am parent OUTPUT:
} Enter the directory
I am child
os_reeba
Parent waited for child
OUTPUT: DIR name : .
Enter the path of file : getpids.c DIR name : forks.c
User ID : 2259 DIR name : ..
Block Size : 4096
Time : 1736483175
Permission : 33204
Number of link : 1
ROGRAM:
P ROGRAM:
P ROGRAM:
P rocess 1 : 3 Enter Arrival Time of process
p
#include<sys/shm.h> #include<stdio.h> #include<sys/shm.h> #include<stdio.h> //FCFS 2 : 1 Enter Burst Time of process 2 : 4
#include<stdlib.h> #include<unistd.h> #include<stdlib.h> #include<unistd.h> #include<stdio.h> int main() PROCESS AT BT CT TAT
#include<string.h> int main() #include<string.h> int main() { WT
{ { int P1 0 3 3 3 0
void *shared_memory; char buff[100]; void *shared_memory; char buff[100]; bt[10],at[20],p[10],wt[10],tat[10],ct[10],i,j,k,n, P2 1 4 7 6 2
int shmid; int shmid; sum=0; float total_tat=0,total_wt=0; Average Turn Around Time : 4.500000 ms
shmid=shmget((key_t)1122,1024,0666|IPC_ shmid=shmget((key_t)1122,1024,0666); printf("Enter number of processes : "); Average Waiting Time : 1.00000 ms
CREAT); printf("Key of Shared Memory is printf("Key of Shared Memory is scanf("%d",Cn);
%d\n",shmid); %d\n",shmid); for(i=0;i<n;i++)
shared_memory=shmat(shmid,NULL,0); shared_memory=shmat(shmid,NULL,0); {
printf("Process attached at printf("Process attached at printf("Enter Arrival Time of process %d :
%p\n",shared_memory); printf("Enter some %p\n",shared_memory); ",i+1); scanf("%d",Cat[i]);
data to write to shared memory"); printf("Data read from shared memory is : printf("Enter Burst Time of process %d :
read(0,buff,100); %s \n",(char *)shared_memory); return 0; ",i+1); scanf("%d",Cbt[i]);
strcpy(shared_memory,buff); } }
printf("\nYou wrote : %s\n",(char OUTPUT: sum=at[0];
*)shared_memory); return 0; Key of Shared Memory is 17 for(j=0;j<n;j++)
} Process attached at 0x7fcee69e4000 {
Data read from shared memory is : I am sum=sum+bt[j]; ct[j]=sum;
UTPUT:
O Sinu from Kerala }
Key of Shared Memory is 17 for(k=0;k<n;k++)
Process attached at 0x7fcee69e4000 {
Enter some data to write to shared memory tat[k]=ct[k]-at[k]; total_tat=total_tat+tat[k];
I am Sinu from Kerala wt[k]=tat[k]-bt[k];
You wrote : I am Sinu from Kerala total_wt=total_wt+wt[k];
}
rintf("\nPROCESS
p
\tAT\tBT\tCT\tTAT\tWT\n\n"); for(i=0;i<n;i++)
{
printf("\nP%d\t%d\t%d\t%d\t%d\t%d\n",i+1,a
t[i],bt[i],ct[i],tat[i],wt[i]);
}
printf("Average Turn Around Time : %f
ms\n",(total_tat/n)); printf("Average Waiting
Time : %f ms\n",(total_wt/n)); return 0;
}
UTPUT:
O
Enter number of processes : 2 Enter Arrival
Time of process 1 : 0 Enter Burst Time of
ROGRAM:
P rintf("Average Turn Around Time : %f
p ROGRAM:
P rintf("\nPROCESS\tPRIORITY\tAT\tBT\tCT
p
//SHORTEST JOB FIRST ms\n",(total_tat/n)); printf("Average Waiting //PRIORITY \tTAT\tWT\n\n"); for(i=0;i<n;i++)
#include<stdio.h> int main() Time : %f ms\n",(total_wt/n)); return 0; #include<stdio.h> int main() {
{ } { printf("\nP%d\t%d\t%d\t%d\t%d\t%d\t%d\n",i
int int +1,prio[i],at[i],bt[i],ct[i],tat[i],wt[i]);
bt[10],at[20],p[10],wt[10],tat[10],ct[10],i,j,k,n, UTPUT:
O at[10],bt[10],ct[10],tat[10],wt[10],i,j,n,prio[10] }
sum=0,temp; float total_tat=0,total_wt=0; Enter number of processes : 4 Enter Arrival ,temp,p[10],sum=0,k; float printf("Average Turn Around Time : %f
printf("Enter number of processes : "); Time of process 1 : 1 Enter Burst Time of total_tat=0,total_wt=0; ms\n",(total_tat/n)); printf("Average Waiting
scanf("%d",Cn); process 1 : 21 Enter Arrival Time of process printf("Enter number of process : "); Time : %f ms\n",(total_wt/n)); return 0;
for(i=0;i<n;i++) 2 : 4 Enter Burst Time of process 2 : 2 Enter scanf("%d",Cn); }
{ Arrival Time of process 3 : 2 Enter Burst for(i=0;i<n;i++)
printf("Enter Arrival Time of process %d : Time of process 3 : 3 Enter Arrival Time of { UTPUT:
O
",i+1); scanf("%d",Cat[i]); process 4 : 3 Enter Burst Time of process 4 printf("Enter priority of process %d : ",i+1); Enter number of processes : 5 Enter priority
printf("Enter Burst Time of process %d : : 6 scanf("%d",Cprio[i]); of process 1 : 5 Enter Arrival Time of
",i+1); scanf("%d",Cbt[i]); PROCESS AT BT CT TAT printf("Enter Arrival Time of process %d : process 1 : 0 Enter Burst Time of process 1
} WT ",i+1); scanf("%d",Cat[i]); : 9 Enter priority of process : 1
for(i=0;i<n;i++) P1 1 21 22 21 0 printf("Enter Burst Time of process %d : Enter Arrival Time of process 2 : 2 Enter
for(j=i+1;j<n-i-1;j++) P2 4 2 24 20 18 ",i+1); scanf("%d",Cbt[i]); Burst Time of process 2 : 5 Enter priority of
if(bt[j]>bt[j+1]CCat[j]>at[j+1]) P3 2 3 27 25 22 p[i]=i+1; process : 2
{ P4 3 6 33 30 24 } Enter Arrival Time of process 3 : 3 Enter
temp=bt[j]; bt[j]=bt[j+1]; bt[j+1]=temp; Average Turn Around Time : 24.0000 ms for(i=0;i<n;i++) Burst Time of process 3 : 7 Enter priority of
temp=p[j]; Average Waiting Time : 16.00000 ms for(j=i+1;j<n-i-1;j++) process : 3
p[j]=p[j+1]; if(prio[j]>prio[j+1]) Enter Arrival Time of process 4 : 1 Enter
p[j+1]=temp; temp=at[j]; at[j]=at[j+1]; { Burst Time of process 4 : 4 Enter priority of
at[j+1]=temp; temp=bt[j]; bt[j]=bt[j+1]; bt[j+1]=temp; process : 4
temp=p[j]; Enter Arrival Time of process 5 : 4 Enter
} p[j]=p[j+1]; Burst Time of process 5 : 3
sum=at[0]; p[j+1]=temp; PROCESS PRIORITY AT BT
for(j=0;j<n;j++) CT TAT WT
{ } P1 5 0 9 9 9
sum=sum+bt[j]; ct[j]=sum; sum=at[0]; 0
} P2 1 2 5 14 12
for(k=0;k<n;k++) temp=at[j]; at[j]=at[j+1]; at[j+1]=temp; 7
{ P3 2 3 7 21 18
tat[k]=ct[k]-at[k]; total_tat=total_tat+tat[k]; for(j=0;j<n;j++) 11
wt[k]=tat[k]-bt[k]; { P4 3 1 4 25 24
total_wt=total_wt+wt[k]; sum=sum+bt[j]; ct[j]=sum; 20
} } P5 4 4 3 28 24
printf("\nPROCESS\tAT\tBT\tCT\tTAT\tWT\n\ for(k=0;k<n;k++) 21
n"); for(i=0;i<n;i++) { Average Turn Around Time : 17.4000 ms
{ tat[k]=ct[k]-at[k]; total_tat=total_tat+tat[k]; Average Waiting Time : 11.8000 ms
printf("\nP%d\t%d\t%d\t%d\t%d\t%d\n",i+1,a wt[k]=tat[k]-bt[k];
t[i],bt[i],ct[i],tat[i],wt[i]); total_wt=total_wt+wt[k];
} }
ROGRAM:
P { rintf("\nprocess at\tbt\tct\ttat\twt\n");
p ROGRAM:
P
//ROUND ROBIN time=time+tq; execution_list[i][3]=time; for(i=0;i<n;i++) //PRODUCER CONSUMER
#include <stdio.h> execution_list[i][2]=execution_list[i][2]-tq; for(j=0;j<n;j++) #include<stdio.h> #include<stdlib.h> int
int process_list[10][6],execution_list[10][6]; total_burst=total_burst-tq; if(process_list[j][0]==i+1) mutex =1;
int main() for(j=i;j<n-1;j++) { int full =0;
{ { printf("P%d\t\t%d\t%d\t%d\t%d\t%d\n",proce int empty,x=0; void producer()
int if(execution_list[j+1][1]<=time) ss_list[j][0],pr ocess_list[j][1],process_list[j] {
n,i,time=0,temp,total_tat=0,total_wt=0,j,tq,to { [2],process_list[j][3],process_list[j][4],proces --mutex;
tal_burst=0,k; printf("enter the number of for(k=0;k<4;k++) s_list[j][5]); break; --empty;
process: "); { } ++full; x++;
scanf("%d",Cn); temp = execution_list[j][k]; printf("\navg tat: %fms\n",(float)total_tat/n); printf("\nProducer produces item %d",x);
printf("enter the TQ: "); scanf("%d",Ctq); execution_list[j][k]=execution_li st[j+1][k]; printf("avg wt:%fms",(float)total_wt/n); return ++mutex;
for(i=0;i<n;i++) execution_list[j+1][k]=temp; 0; }
{ }} } void consumer()
process_list[i][0]=i+1; else {
printf("enter the arrival time of P%d:",i+1); { UTPUT :
O --mutex;
scanf("%d",Cprocess_list[i][1]); Enter number of processes : 4 Enter the --full;
printf("enter the burst time of P%d :",i+1); } Time Slice : 2 ++empty;
scanf("%d",Cprocess_list[i][2]); } Enter Arrival Time of process 1 : 0 Enter printf("Consumer consumes item %d",x);
total_burst=total_burst+process_list[i][2]; break; Burst Time of process 1 : 5 Enter Arrival x--;
} Time of process 2 : 1 Enter Burst Time of ++mutex;
for(i=0;i<n-1;i++) time=time+execution_list[i][2]; process 2 : 4 Enter Arrival Time of process }
for(j=0;j<n-i-1;j++) execution_list[i][3]=time; 3 : 2 Enter Burst Time of process 3 : 2 Enter int main()
if(process_list[j][1]>process_list[j+1][1]) total_burst=total_burst-execution_list[i][2]; Arrival Time of process 4 : 4 Enter Burst {
{ execution_list[i][2]=0; Time of process 4 : 1 int n,i;
for(k=0;k<3;k++) break; PROCESS AT BT CT TAT printf("\[Link] 1 for producer" "\[Link] 2
{ } WT for consumer" "\[Link]");
temp = process_list[j][k]; if(i==n CC total_burst>0) time=time+1; P1 0 5 12 12 7 printf("\nEnter buffer size:");
process_list[j][k]=process_list[j+1][k]; } P2 1 4 11 10 6 scanf("%d",&empty); for(i=1;i>0;i++)
process_list[j+1][k]=temp; } P3 2 2 6 4 2 {
} P4 4 1 9 5 4 printf("\nEnter your choice:");
} for(i=0;i<n;i++) Average Turn Around Time : 7.7500ms scanf("%d",&n);
} for(j=0;j<n;j++) Average Waiting Time : 4.7500ms switch(n)
for(i=0;i<n;i++) if(process_list[i][0]==execution_list[j][0]) {
for(j=0;j<3;j++) process_list[i][3]=execution_list[j][3]; case 1: if((mutex==1)&&(empty!=0))
execution_list[i][j]=process_list[i][j]; for(i=0;i<n;i++) {
while(total_burst>0) { producer();
{ process_list[i][4]=process_list[i][3]-process_l }
for(i=0;i<n;i++) ist[i][1]; total_tat=total_tat+process_list[i][4]; else
{ process_list[i][5]=process_list[i][4]-process_l {
if(execution_list[i][1]<=time CC ist[i][2]; total_wt=total_wt+process_list[i][5]; printf("BUFFER IS FULL");
execution_list[i][2]>0) } }
{ break; case 2:
if(execution_list[i][2]>=tq) if((mutex==1)&&(full!=0))
{ ROGRAM:
P for(i=0;i<n;i++) 00
6
consumer(); //BANKERS { 011
if(finish[i]==0) 431
} include<stdio.h> void main()
# { Following is the safe sequence
else { P1P3P4P0P2
{ int int flag=0; for(j=0;j<m;j++)
printf("BUFFER IS EMPTY"); n,m,i,j,k,y,alloc[20][20],max[20][20],avail[50], {
} ind=0; printf("Enter the number of if(need[i][j]>work[j]) // BEST FIT
break; case 3: exit(0); break; process:"); scanf("%d",&n); {
} printf("Enter the number of resources:"); flag=1; break;
} scanf("%d",&m); } ROGRAM:
P
} printf("Enter the allocation matrix:"); } //FIRST FIT
for(i=0;i<n;i++) if(flag==0)
OUTPUT: { { include<stdio.h> #include<stdlib.h> void
#
for(j=0;j<m;j++) scanf("%d",&alloc[i][j]); safesequence[ind++]=i; for(y=0;y<m;y++) main()
.
1 Enter 1 for producer } work[y]+=alloc[i][y]; finish[i]=1; {
2. Enter 2 for consumer [Link] printf("Enter the max matrix"); } int p[10][3],b[10][2];
Enter buffer size:3 Enter your choice:1 for(i=0;i<n;i++) } int i,n,j,k;
Producer produces item 1 Enter your { } printf("enter the number of process :");
choice:1 Producer produces item 2 Enter for(j=0;j<m;j++) scanf("%d",&max[i][j]); } scanf("%d",&n);
your choice:1 Producer produces item 3 } printf("\nFollowing is the safesequence\n"); printf("enter the number of memeory
Enter your choice:1 BUFFER IS FULL printf("Enter the available matrix"); for(i=0;i<n;i++) partitions :"); scanf("%d",&k);
Enter your choice:2 Consumer consumes for(i=0;i<m;i++) scanf("%d",&avail[i]); printf("P%d",safesequence[i]); printf("enter the process sizes\n");
item 3 Enter your choice:2 Consumer int } for(i=1;i<=n;i++)
consumes item 2 Enter your choice:2 finish[n],safesequence[n],work[m],need[n][m {
Consumer consumes item 1 Enter your ]; for(i=0;i<n;i++) printf("process %d :",i); scanf("%d",&p[i][0]);
choice:2 BUFFER IS EMPTY { OUTPUT: p[i][1]=0;
Enter your choice:3 for(j=0;j<m;j++) need[i][j]=max[i][j]-alloc[i][j]; p[i][2]=0;
} nter the number of process:5 Enter the
E }
printf("Need matrix is"); for(i=0;i<n;i++) number of resources:3 Enter the allocation printf("enter the block sizes\n");
{ matrix:0 1 0 for(i=1;i<=k;i++)
printf("\n"); for(j=0;j<m;j++) 3 0 2 {
printf("%d",need[i][j]); 3 0 2 printf("block %d :",i); scanf("%d",&b[i][0]);
} 2 1 1 b[i][1]=0;
for(i=0;i<m;i++) 0 0 2 }
{ Enter the max matrix7 5 3 for(i=1;i<=n;i++)
work[i]=avail[i]; 3 2 2 {
} 9 0 2 for(j=1;j<=k;j++)
for(i=0;i<n;i++) 2 2 2 {
{ 4 3 3 if((b[j][0]>=p[i][0])&&(b[j][1]==0))
finish[i]=0; Enter the available matrix2 3 0 Need matrix {
} is p[i][1]=b[j][0]; p[i][2]=j;
for(k=0;k<n;k++) 743 b[j][1]=1;
{ 020 break;
} }} ROGRAM:
P rintf("Process_Id\tProcess_size\tBlock_Id
p ROGRAM
P
printf("Process_Id\tProcess_size\tBlock_Id // WORST FIT \tBlock_size\n"); for(i=1;i<=n;i++) #include <stdio.h>
\tBlock_size\n"); for(i=1;i<=n;i++) {
{ include<stdio.h> #include<stdlib.h> void
# printf("P%d\t\t%d\t\t",i,p[i][0]); int main() { int alloc[50][10], avail[10],
printf("P%d\t\t%d\t\t",i,p[i][0]); if(p[i][2]==0) main() finish[50], req[50][10]; int r, p, i, j, curr = 0,
printf("nill\t\tnill\n"); else { if(p[i][2]==0) prev = 0, poss;
printf("%d\t\t%d\n",p[i][2],p[i][1]); int p[10][3],b[10][2]; {
} int i,n,j,k,max=0,maxx; printf("nill\t\tnill\n"); rintf("\nEnter the number of resources: ");
p
} printf("enter the number of process :"); } scanf("%d", &r);
scanf("%d",&n); else
UTPUT :
O printf("enter the number of memeory { rintf("\nEnter the number of processes: ");
p
enter the number of process :4 partitions :"); scanf("%d",&k); printf("%d\t\t%d\n",p[i][2],p[i][1]); scanf("%d", &p);
enter the number of memeory partitions :6 printf("enter the process sizes\n"); }
enter the process sizes for(i=1;i<=n;i++) } rintf("\nEnter available resources: ");
p
process 1 :357 { } for (i = 0; i < r; i++)
process 2 :210 printf("process %d :",i); scanf("%d",&p[i][0]); scanf("%d", &avail[i]);
process 3 :468 p[i][1]=0; OUTPUT:
process 4 :491 p[i][2]=0; rintf("\nEnter allocated resources: ");
p
enter the block sizes block 1 :200 } nter the number of process :3
e for (i = 0; i < p; i++) {
block 2 :400 printf("enter the block sizes\n"); enter the number of memeory partitions :3 for (j = 0; j < r; j++)
block 3 :600 for(i=1;i<=k;i++) enter the process sizes scanf("%d", &alloc[i][j]);
block 4 :500 { process 1 :100 }
block 5 :300 printf("block %d :",i); scanf("%d",&b[i][0]); process 2 :500
block 6 :250 b[i][1]=0; process 3 :300 enter the block sizes block 1 rintf("\nEnter request resources: ");
p
Process_Id Process_size Block_Id } :200 for (i = 0; i < p; i++) {
Block_size for(i=1;i<=n;i++) block 2 :450 for (j = 0; j < r; j++)
P1 357 2 400 { block 3 :350 scanf("%d", &req[i][j]);
P2 210 3 600 for(j=1;j<=k;j++) Process_Id Process_size Block_Id }
P3 468 4 500 { Block_size
P4 491 nill nill if(b[j][1]==0) P1 100 2 450 for (i = 0; i < p; i++)
{ P2 500 nill nill finish[i] = 0;
if((b[j][0]>max)&&(b[j][0]>=p[i][0])) P3 300 3 350
{ while (1) {
max=b[j][0]; maxx=j; for (i = 0; i < p; i++) {
} if (finish[i] == 0) {
}} poss = 1;
if(max>0) for (j = 0; j < r; j++) {
{ if (avail[j] < req[i][j]) {
p[i][1]=max; poss = 0;
p[i][2]=maxx; break;
b[maxx][1]=1; max=0; }
} }
} if (poss == 1) {
for (j = 0; j < r; j++)
avail[j] += alloc[i][j]; ROGRAM
P = (c + 1) % f;
c 2 4 M
5
finish[i] = 1; //fifo miss++; 3 2 4 M
curr++; } else {
poss = 0; include<stdio.h>
# hit++; o of page faults : 6
n
} #include<stdlib.h> } HIT: 2
} MISS: 6
} void main() { Hit ratio: 0.25
if (curr == p) { int n, f, c = 0, d = 0, i, j, x; for(int k = 0; k < f; k++) { Miss ratio: 0.75
printf("THERE IS NO DEADLOCK IN int page[25]; if(replace[k] >= 0) {
THE GIVEN SYSTEM\n"); int replace[6]; printf("%d ", replace[k]);
break; int hit = 0, miss = 0; } else {
} else if (curr == prev) { printf("- ");
printf("THE SYSTEM IS IN A rintf("Enter the length of page string: ");
p }
DEADLOCKED STATE\n"); scanf("%d", &n); }
break;
} else if (curr > prev) { rintf("Enter the number of frames: ");
p
prev = curr; scanf("%d", &f); if(found == 1) {
} printf("H\n");
} printf("Enter the page reference } else {
return 0; string:\n"); printf("M\n");
for(i = 0; i < n; i++) { }
} scanf("%d", &page[i]); }
} printf("no of page faults : %d\n", miss);
UTPUT:
O
Enter the number of resources: 3 for(i = 0; i < f; i++) { rintf("HIT: %d\n", hit);
p
Enter the number of processes: 5 replace[i] = -1; printf("MISS: %d\n", miss);
Enter available resources: 0 0 0 } printf("Hit ratio: %.2f\n", (float)hit / n);
Enter allocated resources: printf("Miss ratio: %.2f\n", (float)miss / n);
0 1 0 printf("FIFO page replacement }
2 0 0 process:\n");
3 0 3
2 1 1 for(i = 0; i < n; i++) { UTPUT:
O
0 0 2 int found = 0; Enter the length of page string: 8
Enter request resources: Enter the number of frames: 3
2 0 2 for(j = 0; j < f; j++) { Enter the page reference string:
0 0 0 if(page[i] == replace[j]) { 2 3 1 3 5 2 4 3
0 0 0 found = 1;
1 0 0 break; IFO page replacement process:
F
0 0 2 } 2 - - M
THERE IS NO DEADLOCK IN THE GIVEN } 2 3 - M
SYSTEM 2 3 1 M
2 3 1 H
if(found == 0) { 5 3 1 M
replace[c] = page[i]; 5 2 1 M
ROGRAM
P } ROGRAM
P frame[min_index] = page[i];
//LRU } //LFU freq[min_index] = 1;
#include <stdio.h> for (j = 0; j < f; j++) { #include <stdio.h> }
int main() { if (frame[j] != -1) { int main() { printf("Page %d: ", page[i]);
int n, f, i, j, page[25], frame[10], age[10], age[j]++; int n, f, i, j, page[25], frame[10], freq[10], for (j = 0; j < f; j++) {
hit = 0, miss = 0; } hit = 0, miss = 0; if (frame[j] == -1) {
printf("Enter number of pages: "); } printf("Enter number of pages: "); printf("- ");
scanf("%d", &n); printf("Page %d:\t", page[i]); scanf("%d", &n); } else {
printf("Enter number of frames: "); for (j = 0; j < f; j++) { printf("Enter number of frames: "); printf("%d ", frame[j]);
scanf("%d", &f); if (frame[j] != -1) { scanf("%d", &f); }
printf("Enter page reference string:\n"); printf("%d\t ", frame[j]); printf("Enter page reference string:\n"); }
for (i = 0; i < n; i++) { } else { for (i = 0; i < n; i++) { printf("\n");
scanf("%d", &page[i]); printf("-\t "); scanf("%d", &page[i]); }
} } } printf("total page faults s: %d\n", miss);
for (i = 0; i < f; i++) { } for (i = 0; i < f; i++) { printf("Hits: %d\n", hit);
frame[i] = -1; printf("\n"); frame[i] = -1; printf("Misses: %d\n", miss);
age[i] = 0; } freq[i] = 0; return 0;
} printf("Total page faults : %d\n", miss); } }
printf("\n LRU Page Replacement printf("Total Hits: %d\n", hit); printf("LFU Page Replacement
Process: \n"); printf("Total Misses: %d\n", miss); Process:\n"); UTPUT
O
for (i = 0; i < n; i++) { return 0; for (i = 0; i < n; i++) { Enter number of pages: 8
int found = 0; } int found = 0; Enter number of frames: 3
for (j = 0; j < f; j++) { for (j = 0; j < f; j++) { Enter page reference string:
if (frame[j] == page[i]) { if (frame[j] == page[i]) { 2 3 1 3 5 2 4 3
hit++; OUTPUT hit++; LFU Page Replacement Process:
age[j] = 0; freq[j]++; Page 2: 2 - - M
found = 1; nter number of pages: 8
E found = 1; Page 3: 2 3 - M
break; Enter number of frames: 3 break; Page 1: 2 3 1 M
} Enter page reference string: } Page 3: 2 3 1 H
} 2 3 1 3 5 2 4 3 } Page 5: 5 3 1 M
if (!found) { LRU Page Replacement Process: if (!found) { Page 2: 5 2 1 M
miss++; Page 2: 2 - - M miss++; Page 4: 5 2 4 M
if (i >= f) { Page 3: 2 3 - M int min_freq = 99999, min_index = Page 3: 3 2 4 M
int oldest = 0; Page 1: 2 3 1 M -1;
for (j = 1; j < f; j++) { Page 3: 2 3 1 H for (j = 0; j < f; j++) { otal page faults: 7
T
if (age[j] > age[oldest]) { Page 5: 5 3 1 M if (frame[j] == -1) { Hits: 1
oldest = j; Page 2: 5 2 1 M min_index = j; Misses: 7
} Page 4: 5 2 4 M break;
} Page 3: 3 2 4 M }
frame[oldest] = page[i]; if (freq[j] < min_freq) {
age[oldest] = 0; otal page faults : 7
T min_freq = freq[j];
} else { Total Hits: 1 min_index = j;
frame[i] = page[i]; Total Misses: 7 }
age[i] = 0; }
ROGRAM
P 8 183 37 122 14 124 65 67
9 } // Then reverse and service remaining
//Fcfs disk TRAVERSED ORDER : 98 => 183 => 37 => for(i = pos; i < n; i++) {
#include<stdio.h> 122 => 14 => 124 => 65 => 67. // Find position where track ≥ current printf("%d => ", tr[i]);
#include<string.h> TOTAL HEAD MOVEMENTS : 640 head sum += abs(cr - tr[i]);
void main() int pos = 0; cr = tr[i];
{ ROGRAM
P for(i = 0; i < n; i++) { }
int tr[20],cr,n,i,sum=0,new; //Scan disk if(tr[i] >= cr) { }
printf("ENTER THE NUMBER OF #include <stdio.h> pos = i;
TRACKS : "); #include <stdlib.h> break; printf("\b\b\b. \nTOTAL HEAD
scanf("%d",&n); } OVEMENTS: %d\n", sum);
M
printf("ENTER THE HEAD POINTER void main() { } }
POSITION : "); int tr[20], n, i, j, temp, cr, sum = 0, dir,
scanf("%d",&cr); disk_size; printf("TRAVERSED ORDER: ");
printf("ENTER THE TRACKS TO BE OUTPUT
TRAVERSED : "); rintf("ENTER THE NUMBER OF
p if(dir == 1) { // Move right
for(i=0;i<n;i++) TRACKS: "); for(i = pos; i < n; i++) { NTER THE NUMBER OF TRACKS: 8
E
{ scanf("%d", &n); printf("%d => ", tr[i]); ENTER THE TRACKS TO BE
new = 0; sum += abs(cr - tr[i]); TRAVERSED:
scanf("%d",&tr[i]); printf("ENTER THE TRACKS TO BE cr = tr[i]; 98 183 37 122 14 124 65 67
new=cr-tr[i]; TRAVERSED: "); } ENTER THE HEAD POINTER POSITION:
if(new<0) for(i = 0; i < n; i++) // Move to disk end 50
{ scanf("%d", &tr[i]); if(cr != disk_size - 1) { ENTER THE TOTAL DISK SIZE (last track
new=tr[i]-cr; sum += abs(cr - (disk_size - 1)); number): 200
} printf("ENTER THE HEAD POINTER printf("%d => ", disk_size - 1); ENTER DIRECTION (0 for Left, 1 for Right):
cr=tr[i]; POSITION: "); cr = disk_size - 1; 1
sum=sum + new; scanf("%d", &cr); } TRAVERSED ORDER: 65 => 67 => 98 =>
} // Then reverse and service remaining 122 => 124 => 183 => 199 => 37 => 14.
printf("TRAVERSED ORDER : "); printf("ENTER THE TOTAL DISK SIZE for(i = pos-1; i >= 0; i--) { TOTAL HEAD MOVEMENTS: 386
for(i=0;i<n;i++) (last track number): "); printf("%d => ", tr[i]);
printf("%d => ",tr[i]); scanf("%d", &disk_size); sum += abs(cr - tr[i]);
printf("\b\b\b. \nTOTAL HEAD cr = tr[i];
MOVEMENTS : %d\n",sum); printf("ENTER DIRECTION (0 for Left, 1 }
} for Right): "); } else { // Move left
scanf("%d", &dir); for(i = pos-1; i >= 0; i--) {
printf("%d => ", tr[i]);
// Sort track requests sum += abs(cr - tr[i]);
for(i = 0; i < n-1; i++) { cr = tr[i];
OUTPUT for(j = 0; j < n-i-1; j++) { }
if(tr[j] > tr[j+1]) { // Move to track 0
NTER THE NUMBER OF TRACKS : 8
E temp = tr[j]; if(cr != 0) {
ENTER THE HEAD POINTER POSITION : tr[j] = tr[j+1]; sum += abs(cr - 0);
50 tr[j+1] = temp; printf("0 => ");
ENTER THE TRACKS TO BE TRAVERSED } cr = 0;
: } }
ROGRAM
P for(i = 0; i < n; i++) { // Jump to end
//Cscan disk if(tr[i] >= cr) { sum += abs(cr - (disk_size - 1));
#include <stdio.h> pos = i; printf("%d => ", disk_size - 1);
#include <stdlib.h> break; cr = disk_size - 1;
} // Continue from end to remaining
void main() { } tracks
int tr[20], n, i, j, temp, cr, sum = 0, dir, for(i = n-1; i >= pos; i--) {
disk_size; printf("TRAVERSED ORDER: "); printf("%d => ", tr[i]);
sum += abs(cr - tr[i]);
printf("ENTER THE NUMBER OF if(dir == 1) { // Move right only cr = tr[i];
TRACKS: "); for(i = pos; i < n; i++) { }
scanf("%d", &n); printf("%d => ", tr[i]); }
sum += abs(cr - tr[i]);
printf("ENTER THE TRACKS TO BE cr = tr[i]; printf("\b\b\b. \nTOTAL HEAD
TRAVERSED: "); } OVEMENTS: %d\n", sum);
M
for(i = 0; i < n; i++) // Move to disk end }
scanf("%d", &tr[i]); if(cr != disk_size - 1) {
sum += abs(cr - (disk_size - 1)); OUTPUT
rintf("ENTER THE HEAD POINTER
p printf("%d => ", disk_size - 1);
POSITION: "); cr = disk_size - 1; NTER THE NUMBER OF TRACKS: 8
E
scanf("%d", &cr); } ENTER THE TRACKS TO BE
// Jump to start (track 0) TRAVERSED:
printf("ENTER THE TOTAL DISK SIZE sum += abs(cr - 0); 98 183 37 122 14 124 65 67
(last track number): "); printf("0 => "); ENTER THE HEAD POINTER POSITION:
scanf("%d", &disk_size); cr = 0; 50
// Continue from start to remaining ENTER THE TOTAL DISK SIZE (last track
printf("ENTER DIRECTION (0 for Left, 1 tracks number): 200
for Right): "); for(i = 0; i < pos; i++) { ENTER DIRECTION (0 for Left, 1 for Right):
scanf("%d", &dir); printf("%d => ", tr[i]); 1
sum += abs(cr - tr[i]); TRAVERSED ORDER: 65 => 67 => 98 =>
// Sort track requests cr = tr[i]; 122 => 124 => 183 => 199 => 0 => 14 =>
for(i = 0; i < n-1; i++) { } 37.
for(j = 0; j < n-i-1; j++) { } else { // Move left only TOTAL HEAD MOVEMENTS: 382
if(tr[j] > tr[j+1]) { for(i = pos-1; i >= 0; i--) {
temp = tr[j]; printf("%d => ", tr[i]);
tr[j] = tr[j+1]; sum += abs(cr - tr[i]);
tr[j+1] = temp; cr = tr[i];
} }
} // Move to start
} if(cr != 0) {
sum += abs(cr - 0);
// Find position where track ≥ current printf("0 => ");
head cr = 0;
int pos = 0; }