Operating System Lab Practical File
Operating System Lab Practical File
Program:
/* A program to simulate the FCFS CPU scheduling algorithm */
#include<stdio.h>
int main()
{
char pn[10][10];
int arr[10],bur[10],star[10],finish[10],tat[10],wt[10],i,n;
int totwt=0,tottat=0;
OUTPUT:
$ vi fcfs.c
$ cc fcfs.c
$ ./a.out
Enter the number of processes: 3
Enter the Process Name, Arrival Time & Burst Time: 1 2 3
Enter the Process Name, Arrival Time & Burst Time: 2 5 6
Enter the Process Name, Arrival Time & Burst Time: 3 6 7
PName Arr琀椀me Bur琀椀me Start TAT Finish
1 2 3 2 3 5
2 5 6 5 6 11
3 6 7 11 12 18
Average Wai琀椀ng 琀椀me: 1.666667
Average Turn Around Time: 7.000000
Experiment - 02
Q.2 Program to implement SJF CPU scheduling algorithm
ALGORITHM:
Step 1: Start the program.
Step 2: Get the number of process.
Step 3: Get the id and service time for each process.
Step 4: Initially the waiting time of first short process as 0 and total time of first short is process
the
service time of that process.
Step 5: Calculate the total time and waiting time of remaining process.
Step 6: Waiting time of one process is the total time of the previous process.
Step 7: Total time of process is calculated by adding the waiting time and service time of each
process.
Step 8: Total waiting time calculated by adding the waiting time of each process.
Step 9: Total turn around time calculated by adding all total time of each process.
Step 10: Calculate average waiting time by dividing the total waiting time by total number of
process.
Step 11: Calculate average turn around time by dividing the total waiting time by total number
of
process.
Step 12: Display the result.
Step 13: Stop the program.
Program:
/* A program to simulate the SJF CPU scheduling algorithm */
#include<stdio.h>
#include<string.h>
main()
{
int i=0,pno[10],bt[10],n,wt[10],temp=0,j,tt[10];
float sum,at;
printf("\n Enter the no of process ");
scanf("\n %d",&n);
printf("\n Enter the burst time of each process");
for(i=0;i<n;i++)
{
printf("\n p%d",i);
scanf("%d",&bt[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(bt[i]>bt[j])
{
temp=bt[i];
bt[i]=bt[j];
bt[j]=temp;
temp=pno[i];
pno[i]=pno[j];
pno[j]=temp;
}
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=bt[i-1]+wt[i-1];
sum=sum+wt[i];
}
printf("\n process no \t burst time\t waiting time \t turn around time\n");
for(i=0;i<n;i++)
{
tt[i]=bt[i]+wt[i];
at+=tt[i];
printf("\n p%d\t\t%d\t\t%d\t\t%d",i,bt[i],wt[i],tt[i]);
}
printf("\n\n\t Average waiting time%f\n\t Average turn around time%f", sum, at);
}
OUTPUT:
$ vi sjf.c
$ cc sjf.c
$ ./a.out
Enter the no of process 5
Enter the burst 琀椀me of each process
p0 1
p1 5
p2 2
p3 3
p4 4
process no burst 琀椀me wai琀椀ng 琀椀me turn around 琀椀me
p0 1 0 1
p1 2 1 3
p2 3 3 6
p3 4 6 10
p4 5 10 15
Average wai琀椀ng 琀椀me 4.000000
Average turn around 琀椀me 7.000000
Experiment - 03
Q.3 Write a program to implemen priority scheduling
ALGORITHM:
1. Start the process
2. Get the number of processes to be inserted
3. Get the corresponding priority of processes
4. Sort the processes according to the priority and allocate the one with highest priority
to execute first
5. If two process have same priority then FCFS scheduling algorithm is used
6. Calculate the total and average waiting time and turnaround time
7. Display the values
8. Stop the process
Program:
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
void main()
{
clrscr();
int x,n,p[10],pp[10],pt[10],w[10],t[10],awt,atat,i;
printf("Enter the number of process : ");
scanf("%d",&n);
printf("\n Enter process : time priorities \n");
for(i=0;i<n;i++){
printf("\nProcess no %d : ",i+1);
scanf("%d %d",&pt[i],&pp[i]);
p[i]=i+1;
}
for(i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
if(pp[i]<pp[j])
{
x=pp[i]; pp[i]=pp[j];
pp[j]=x;
x=pt[i];
pt[i]=pt[j];
pt[j]=x;
x=p[i];
p[i]=p[j];
p[j]=x;
}
}
}
w[0]=0;
awt=0;
t[0]=pt[0];
atat=t[0];
for(i=1;i<n;i++)
{
w[i]=t[i-1];
awt+=w[i]; t[i]=w[i]
+pt[i]; atat+=t[i];
}
printf("\n\n Job \t Burst Time \t Wait Time \t Turn Around TimePriority \n");
for(i=0;i<n;i++){
printf("\n %d \t\t %d \t\t %d \t\t %d \t\t %d
\n",p[i],pt[i],w[i],t[i],pp[i]);
awt/=n;
atat/=n;
printf("\n Average Wait Time : %d \n",awt);
printf("\n Average Turn Around Time : %d \n",atat);
getch();
}
OUTPUT:-
Enter process:p1
Enter cpu burst:10
Enter priroty:3
Enter process:p2
Enter cpu burst:1
Enter priroty:1
Enter process:p3
Enter cpu burst:2
Enter priroty:4
Enter process:p4
Enter cpu burst:1
Enter priroty:5
Enter process:p5
Enter cpu burst:5
Enter priroty:2
Process gaint chart is below::
0-p2-1-p5 -6- p1 -16- p3 -18- p4-19
TP:0.26 TAT:12 RT:12 WT:6.2
Experiment – 04
OUTPUT:
/tmp/N1tvTs4VL4.o
Enter The No Of Process :3
Enter The Quantum 琀椀me (in ms) :5
Enter The Burst Time (in ms) For Process # 1 :25 Enter The Burst Time (in ms) For
Process # 2 :30 Enter The Burst Time (in ms) For Process # 3 :54 Order Of Execu琀椀on
process # 1 0 5 20
process # 2 5 10 25
process # 3 10 15 49
process # 1 15 20 15
process # 2 20 25 20
process # 3 25 30 44
process # 1 30 35 10
process # 2 35 40 15
process # 3 40 45 39
process # 1 45 50 5
process # 1 60 65 0
process # 2 65 70 5
process # 3 70 75 29
process # 2 75 80 0
process # 3 80 85 24
process # 3 85 90 19
process # 3 90 95 14
process # 3 95 100 9
process # 3 100 105 4
process # 3 105 109 0
OUTPUT:
OUTPUT:
$ vi mesg.h
$ vi sender1.c
$ vi receiver1.c
$ cc sender1.c
$ ./a.out
mcet
$ cc receiver1.c
$ ./a.out
mcet
Experiment – 07
ALGORITHM:
Step 1: Start the program.
Step 2: Define the number of philosophers.
Step 3: Declare one thread per philosopher.
Step 4: Declare one chopsticks per
philosopher. Step 5: When a philosopher is
hungry.
i. See if chopsticks on both sides are free.
ii. Acquire both chopsticks or.
iii. Eat.
iv. restore the chopsticks.
v. If chopsticks aren’t free.
Step 6: Wait till they are available.
Step 7: Stop the program.
Program:
int tph, philname[20], status[20], howhung, hu[20], cho;
int main()
{
int i;
clrscr();
printf("\n\nDINING PHILOSOPHER PROBLEM");
printf("\nEnter the total no. of philosophers: ");
scanf("%d",&tph);
for(i=0;i<tph;i++)
{
philname[i] = (i+1);
status[i]=1;
}
printf("How many are hungry : ");
scanf("%d", &howhung);
if(howhung==tph)
{
printf("\nAll are hungry..\nDead lock stage will occur");
printf("\nExiting..");
}
else
{
for(i=0;i<howhung;i++)
{printf("Enter philosopher %d position: ",(i+1));
scanf("%d", &hu[i]);
status[hu[i]]=2;
}
do
{
printf("1.One can eat at a time\t2.Two can eat at a time\t3.Exit\nEnter your
choice:");
scanf("%d", &cho);
switch (cho)
{
break();
case 2: two();
break;
case 3: exit(0);
default: printf("\nInvalid option..");
}
}
while(1);
}
}
one()
{
int pos=0, x, i;
printf("\nAllow one philosopher to eat at any time\n");
for(i=0;i<howhung; i++, pos++)
{
printf("\nP %d is granted to eat", philname[hu[pos]]);
for(x=pos;x<howhung;x++)
printf("\nP %d is waiting", philname[hu[x]]);
}
}
two()
{
nt i, j, s=0, t, r, x;
printf("\n Allow two philosophers to eat at same time\n");
for(i=0;i<howhung;i++)
{
for(j=i+1;j<howhung;j++)
{
if(abs(hu[i]-hu[j])>=1&& abs(hu[i]-hu[j])!=4)
{
printf("\n\ncombination %d \n", (s+1));
t=hu[i];
r=hu[j];
s++;
printf("\nP %d and P %d are granted to eat", philname[hu[i]],
philname[hu[j]]);
for(x=0;x<howhung;x++)
{
if((hu[x]!=t)&&(hu[x]!=r))
printf("\nP %d is waiting", philname[hu[x]]);
}
}
}
}
OUTPUT:
DINING PHILOSOPHER PROBLEM
Enter the total no. of philosophers: 5 How many are hungry : 3
Enter philosopher 1 posi琀椀on: 2
Enter philosopher 2 posi琀椀on: 4
Enter philosopher 3 posi琀椀on: 5
1.One can eat at a 琀椀me 2.Two can eat at a 琀椀me 3.Exit Enter your choice: 1
Allow one philosopher to eat at any 琀椀me
P 3 is granted to eat P 3 is wai琀椀ng
P 5 is wai琀椀ng P 0 is wai琀椀ng
P 5 is granted to eat P 5 is wai琀椀ng
P 0 is wai琀椀ng
P 0 is granted to eat P 0 is wai琀椀ng
1.One can eat at a 琀椀me 2.Two can eat at a 琀椀me 3.Exit Enter your choice: 2
Allow two philosophers to eat at same 琀椀me combina琀椀on
P 3 and P 5 are granted to eat P 0 is wai琀椀ng
P 5 is wai琀椀ng combina琀椀on
P 5 and P 0 are granted to eat P 3 is wai琀椀ng
1.One can eat at a 琀椀me 2.Two can eat at a 琀椀me 3.Exit Enter your choice: 3
Experiment - 08
Q.8 Programs to implement FIFO Page Replacement Algorithms:
ALGORITHM:
Step 1: Start the program
Step 2: Read the number of frames
Step 3: Read the number of pages
Step 4: Read the page numbers
Step 5: Initialize the values in frames to -1
Step 6: Allocate the pages in to frames in First in first out order.
Step 7: Display the number of page faults.
Step 8: Stop the program
Program:
/* A program to simulate FIFO Page Replacement Algorithm */
#include<stdio.h>
main()
{
int a[5],b[20],n,p=0,q=0,m=0,h,k,i,q1=1;
char f='F'
printf("Enter the Number of Pages:");
scanf("%d",&n);
printf("Enter %d Page Numbers:",n);
for(i=0;i<n;i++)
scanf("%d",&b[i]);
for(i=0;i<n;i++)
{
if(p==0)
{
if(q>=3)
q=0;
a[q]=b[i];
q++;
if(q1<3)
{
q1=q;
}
}
printf("\n%d",b[i]);
printf("\t");
for(h=0;h<q1;h++)
printf("%d",a[h]);
if((p==0)&&(q<=3))
{
printf("-->%c",f);
m++;
}
p=0;
for(k=0;k<q1;k++)
{
if(b[i+1]==a[k])
p=1;
}
}
printf("\nNo of faults:%d",m);
OUTPUT:
$ vi 昀椀fo.c
$ cc 昀椀fo.c
$ ./a.out
Enter the Number of Pages: 12
Enter 12 Page Numbers:
232152453252
2 2-->F
3 23-->F
2 23
1 231-->F
5 531-->F
2 521-->F
4 524-->F
5 524
3 324-->F
2 324
5 354-->F
2 352-->F
No of faults: 9
Experiment - 09
OUTPUT:
$ vi lru.c
$ cc lru.c
$ ./a.out
Enter the number of pages: 12
Enter 12 Page Numbers:
232152453252
2 2-->F
3 23-->F
2 23
1 231-->F
5 251-->F
2 251
4 254-->F
5 254
3 354-->F
2 352-->F
5 352
2 352
No of faults: 7