Operating System (Practical File)
Operating System (Practical File)
Operating System (Practical File)
OS LAB FILE
Submitted to
Submitted By
Mr. Laxmi Kant Sagar
Singh
(1219210901)
Brij Bhushan
CONTENTS
S.No.
1.
2.
3.
4.
5.
6.
7.
8.
9.
WAP to implement
Dinning Philosophers
WAP to implement FIFO
page replacement
algorithm
WAP to implement LRU
page replacement
algorithm
Write a program to
implement Round Robin
WAP to implement
Optimal page
replacement algorithm
WAP to implement
DeadLock Detection
10.
WAP to implement
WORST FIT algorithm
11.
WAP to implement
Sleeping Barber Problem
Date
Remark
PROGRAM 1
Write a program to implement First Come First
Serve (FCFS) Scheduling.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n,at,bt,temp,temp1,temp2,awt[50],c,c_loc;
floatawtime,att[50],burst,attime,a_wait;
int mat[50][3];
clrscr();
printf("Enter the no. of processes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
mat[i][0]=i+1;
printf("Enter arrival time and burst time for %d
process:\n",i+1);
for(j=1;j<3;j++)
scanf("%d",&mat[i][j]);
}
printf("\nProcess \tArrival time \t Burst time\n");
for(i=0;i<n;i++)
{
for(j=0;j<3;j++)
printf("%d\t ",mat[i][j]);
printf("\n");
}
printf("\nAfter sorting processes acc. to arrival time:\n");
for(i=0;i<n-1;i++)
{
c=mat[i][1];
c_loc=i;
for(j=i+1;j<n;j++)
1|Page
Brij BhushanSingh
(1219210901)
if(mat[j][1] < c)
{
c= mat[j][1];
c_loc=j;
}
}
if(i!=c_loc)
{
temp=mat[c_loc][1];
mat[c_loc][1]=mat[i][1];
mat[i][1]=temp;
temp1=mat[c_loc][2];
mat[c_loc][2]=mat[i][2];
mat[i][2]=temp1;
temp2=mat[c_loc][0];
mat[c_loc][0]=mat[i][0];
mat[i][0]=temp2;
}
}
printf("\nProcess \tArrival time \t Burst time\n");
for(i=0;i<n;i++)
{
for(j=0;j<3;j++)
printf("%d\t ",mat[i][j]);
printf("\n");
}
awt[0]=0;
a_wait=mat[0][1];
for(i=0;i<n-1;i++)
{
awt[i+1]=a_wait+(mat[i][2]-mat[i+1][1]);
a_wait=a_wait+mat[i][2];
}
for(i=0;i<n;i++)
awtime=awtime+awt[i];
awtime=awtime/n;
printf("\nAvg. Waiting time= %f",awtime);
att[0]=mat[0][2]-mat[0][1];
burst=mat[0][2];
2|Page
Singh
Brij Bhushan
(1219210901)
Output
Enter
Enter
0
24
Enter
0
3
Enter
0
3
Arrival time
0
0
0
Burst time
24
3
3
3|Page
Singh
Brij Bhushan
(1219210901)
PROGRAM 2
Write a program to implement shortest job first (SJF)
scheduling.
#include<stdio.h>
#include<conio.h>
void sort(int a[],int b[],int n);
void main()
{
int a[10],b[10],i,n,c[10],j,temp;
floatawt=0.0,att=0.0,sw=0.0,st=0.0;
c[0]=0;
clrscr();
printf("enter number of processes to be exexuted:");
scanf("%d",&n);
printf("enter arrival time and burst time of\n:");
for(i=1;i<=n;i++)
{ printf("\nP%d",i);
printf("\narrival time:");
scanf("%d",&a[i]);
printf("\nburst time:");
scanf("%d",&b[i]);
}
sort(a,b,n);
c[1]=c[0]+b[1];
sw=sw+(c[0]-a[1]);
st=st+(c[1]-a[1]);
sort(b,a,n);
for(i=2;i<=n;i++)
{
c[i]=c[i-1]+b[i];
}
for(i=2;i<=n;i++)
4|Page
Singh
Brij Bhushan
(1219210901)
Output
Enter arrival time and burst time of:P1
arrival time:0
burst time:10
P2
arrival time:1
burst time:6
P3
arrival time:3
burst time:2
P4
arrival time:5
5|Page
Singh
Brij Bhushan
(1219210901)
PROGRAM 3
Write a program to implement Priority based
scheduling.
#include<stdio.h>
#include<conio.h>
void main()
{
intarr[10],bur[10],tot[10],pri[10],x=0,i,j,n,temp;
floatawt = 0, tt =0;
printf("\nEnter no of processes\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter arrival time and burst and priority for %d
process\n",i);
scanf("%d",&arr[i]);
scanf("%d",&bur[i]);
scanf("%d",&pri[i]);
}
for(i=0;i<n-1;i++)
for(j=0;j<(n-i-1);j++)
if(arr[j]>arr[j+1])
{
temp =arr[j];
arr[j]=arr[j+1];
arr[j+1] = temp;
temp =bur[j];
bur[j]=bur[j+1];
bur[j+1] = temp;
temp =pri[j];
6|Page
Singh
Brij Bhushan
(1219210901)
Brij Bhushan
(1219210901)
Output
Enter no of processes
4
Enter arrival time and
0 10 4
Enter arrival time and
163
Enter arrival time and
321
Enter arrival time and
542
table -
8|Page
Singh
Brij Bhushan
(1219210901)
PROGRAM 4
Write a program to implement Bankers Algorithm:
#include<stdio.h>
#include<conio.h>
void main()
{
inti,j,all[4][2],max[4][2],need[4][2],ava[2],finish[4],flag=1;
clrscr();
for(i=0;i<4;i++)
{
printf("\nEnter the values for process %d \n",i);
printf("\nEnter allocation\n");
for(j=0;j<2;j++)
scanf("%d",&all[i][j]);
printf("\nEnter max need\n");
for(j=0;j<2;j++)
{
scanf("%d",&max[i][j]);
need[i][j] = max[i][j] - all[i][j];
}
finish[i] = 0;
}
printf("\nEnter total available resources \n");
for(j=0;j<2;j++)
scanf("%d",&ava[j]);
for(j=1;j<=3;j++)
9|Page
Singh
Brij Bhushan
(1219210901)
10 | P a g e
Singh
Brij Bhushan
(1219210901)
Output
Enter the values for process 0
Enter allocation
24
Enter max need
35
Enter the values for process 1
Enter allocation
42
Enter max need
53
Enter the values for process 2
Enter allocation
45
Enter max need
13
Enter the values for process 3
Enter allocation
89
Enter max need
11 12
Enter total available resources
12 12
P0-->P1-->P2-->P3-->
System is in safe state
11 | P a g e
Singh
Brij Bhushan
(1219210901)
PROGRAM 5
Write a program to implement LRU page replacement
algorithm
#include<stdio.h>
#include<conio.h>
intfr[20],frsize;
void main()
{
void display();
int p[50],i,j,fs[20],n;
int index,k,l,flag1=0,flag2=0;
floatpfr,pf=0;
clrscr();
printf("\n ENTER THE NUMBER OF PAGES:\n");
scanf("%d",&n);
printf("\n ENTER THE PAGE NUMBER :\n");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("\n ENTER THE NUMBER OF FRAMES :");
scanf("%d",&frsize);
for(i=0;i<frsize;i++)
fr[i]=-1;
for(j=0;j<n;j++)
{
12 | P a g e
Singh
Brij Bhushan
(1219210901)
Brij Bhushan
(1219210901)
Output
ENTER THE NUMBER OF FRAMES : 3
7
7
7
2
2
2
2
4
4
4
0
0
0
1
-1
0
0
0
0
0
0
0
0
3
3
3
3
3
-1
-1
1
1
1
3
3
3
2
2
2
2
2
2
14 | P a g e
Singh
Brij Bhushan
(1219210901)
PROGRAM 6
Write a program to implement Round Robin Scheduling
#include<conio.h>
#include<stdio.h>
void main()
{
intprcs[10],n,i,arv[10],brst[10],temp,tempb,tempp,sumb,j;
inttempa,t,time,wt[10],wtt[10],max,maxi,y=1;
floatst=0.0,tt=0.0,wat=0.0;
clrscr();
printf("enter the number of processes");
scanf("%d",&n);
for(i=0;i<n;i++)
{prcs[i]=i;
printf("\nenter the arrival time for P%d",i);
scanf("%d",&arv[i]);
printf("enter the burst time for P%d",i);
scanf("%d",&brst[i]);
wt[i]=0;
wtt[i]=0;}
printf("enter the time slice");
15 | P a g e
Singh
Brij Bhushan
(1219210901)
Brij Bhushan
(1219210901)
OUTPUT
Enter the number of processes
3
Enter the arrival time for P0 0
Enter the burst time for P0
24
Enter the arrival time for P1 0
Enter the burst time for P1
3
Enter the arrival time for P2 0
Enter the burst time for P2
3
Enter the time slice 4
process arrival time burst time
P0
0
24
P1
0
3
P2
0
3
waiting time is 5.666667
turnaround time is 15.666667
17 | P a g e
Singh
Brij Bhushan
(1219210901)
PROGRAM 7
Write a program to implement Optimal page
replacement algorithm
#include<stdio.h>
#include<conio.h>
int a[21],f[8],n,k;
void main()
{
intft,c,i,j,p,m,min;
clrscr();
printf("enter the no. of frames\t");
scanf("%d",&k);
printf("enter the length of string\t");
scanf("%d",&n);
printf("enter the %d string enteries\t",n);
for(i=1;i<=n;i++)
{scanf("%d",&a[i]);}
for(i=1;i<=k;i++)
{f[i]=-1;}
18 | P a g e
Singh
Brij Bhushan
(1219210901)
else
{if(f[i]==-1)
{f[ft]=a[i];
ft++;}
else
{min=selminp(i);
f[min]=a[i];}
p++;
}
printf("\nelements in frame after %d iteration:\t",i);
for(m=1;m<=k;m++)
{if (f[m]==-1)
printf("NIL\t");
else
printf("%d\t",f[m]);}
}
printf("\npage faults are %d",p);
printf("\npage fault rate is %f",p/2.0);
getch();
}
intselminp(intcurposit)
{inti,m,futp[10],ch,t,c;
for(i=1;i<=k;i++)
19 | P a g e
Singh
Brij Bhushan
(1219210901)
Output
Enter the no. of frames
3
Enter the length of string
15
Enter the 15 string enteries 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2
Elements
Elements
Elements
Elements
Elements
Elements
Elements
Elements
Elements
Elements
Elements
in
in
in
in
in
in
in
in
in
in
in
frame
frame
frame
frame
frame
frame
frame
frame
frame
frame
frame
after
after
after
after
after
after
after
after
after
after
after
1 iteration:
2 iteration:
3 iteration:
4 iteration:
5 iteration:
6 iteration:
7 iteration:
8 iteration:
9 iteration:
10 iteration:
11 iteration:
7
7
7
2
2
2
2
2
2
2
2
NIL
0
0
0
0
0
0
4
4
4
0
20 | P a g e
Singh
NIL
NIL
1
1
1
3
3
3
3
3
3
Brij Bhushan
(1219210901)
2
2
2
2
0
0
1
1
3
3
3
3
PROGRAM 8
Write a program to implement counting semaphore.
#include<conio.h>
#include<stdio.h>
void signal();
void wait();
int count=1;
void main()
{ int n;
clrscr();
getch();
do
{printf("\nenter 1 if process want to enter in cs enter\nenter 2 if
process want to leave cs\nenter 3 to exit ");
scanf("%d",&n);
if(n==1)
{wait();}
else
21 | P a g e
Singh
Brij Bhushan
(1219210901)
Output
22 | P a g e
Singh
Brij Bhushan
(1219210901)
23 | P a g e
Singh
Brij Bhushan
(1219210901)
PROGRAM 9
Write a program to implement producer-consumer
problem
#include<stdio.h>
#include<conio.h>
void main()
{intn,buffer,p,i,j,k,r;
p=5;
buffer=0;
clrscr();
getch();
do
{k=0;
printf("\nPlese select from following \n enter 1 to produce \n enter 2 to
consume \n enter 3 to exit");
scanf("%d",&n);
if(n==1)
{if(buffer>p)
printf("u have to wait as buffer is full");
else
{printf("how many item u want to produce");
scanf("%d",&r);
for(j=1;j<=r;j++)
{buffer++;
if(buffer>p)
break;
k=j;
}
if(k==0)
printf("buffer is full");
else
24 | P a g e
Singh
Brij Bhushan
(1219210901)
25 | P a g e
Singh
Brij Bhushan
(1219210901)
Output
Please select from following
enter 1 to produce
enter 2 to consume
enter 3 to exit1
how many item u want to produce3
3 item being produce
Plese select from following
enter 1 to produce
enter 2 to consume
enter 3 to exit2
how many item u want to consume4
3 item being consumed
Plese select from following
enter 1 to produce
enter 2 to consume
enter 3 to exit1
how many item u want to produce5
5 item being produce
Plese select from following
enter 1 to produce
enter 2 to consume
enter 3 to exit2
how many item u want to consume3
3 item being consumed
Plese select from following
enter 1 to produce
enter 2 to consume
enter 3 to exit2
how many item u want to consume4
26 | P a g e
Singh
Brij Bhushan
(1219210901)
27 | P a g e
Singh
Brij Bhushan
(1219210901)
PROGRAM 10
Write a program to implement best fit
#include<conio.h>
#include<stdio.h>
void main()
{
intsb,sp,p[10],b[10],i,j;
clrscr();
printf("enter the no. of blocks:\t");
scanf("%d",&sb);
printf("enter the sizes of block:\t");
for(i=0;i<sb;i++)
{scanf("%d",&b[i]);}
printf("enter the no. of processes:\t");
scanf("%d",&sp);
printf("enter the size of processes:\t");
for(i=0;i<sp;i++)
{scanf("%d",&p[i]);}
printf("processes and blocks entered values\n");
for(i=0;i<sp;i++)
{printf("process%d\t %d\n",i,p[i]);}
for(i=0;i<sb;i++)
{printf("block%d\t %d\n",i,b[i]);}
for(i=0;i<sp;i++)
{j=selbest(p[i],b,sb);
if(j!=-1)
{
28 | P a g e
Singh
Brij Bhushan
(1219210901)
printf("process%d is allotedblock%d\n",i,j);
p[i]=10000;
b[j]=0;}}
for(i=0;i<sp;i++)
{if(p[i]!=10000)
printf("the process%ddoesnt get any block\n",i);}
getch();
}
intselbest(intp,int b[10],int bi)
{int k=0,ar[10],arin[10],min,i,j,index;
for(i=0;i<bi;i++)
{if(p<=b[i])
{ar[k]=b[i];
arin[k]=i;
k++;}}
if(k==0)
{return -1;}
else
{min=ar[0];
index=arin[0];
for(i=0;i<k;i++)
{if(min>ar[i])
{min=ar[i];
index=arin[i];}}
return index;}}
29 | P a g e
Singh
Brij Bhushan
(1219210901)
Output
Enter the no. of blocks:
5
enter the sizes of block:
100 50 150 200 300
enter the no. of processes:
5
enter the size of processes: 99 120 50 400 200
processes and blocks entered values
process0
99
process1
120
process2
50
process3
400
process4
200
block0 100
block1 50
block2 150
block3 200
block4 300
process0 is alloted block0
process1 is alloted block2
process2 is alloted block1
process4 is alloted block3
the process3 doesnt get any block
30 | P a g e
Singh
Brij Bhushan
(1219210901)
PROGRAM 11
Write a prorgram to implement FIRST FIT algorithm.
#include<conio.h>
#include<stdio.h>
void main()
{
intsb,sp,p[10],b[10],i,j;
clrscr();
printf("enter the no. of blocks:\t");
scanf("%d",&sb);
printf("enter the sizes of block:\t");
for(i=0;i<sb;i++)
{scanf("%d",&b[i]);}
printf("enter the no. of processes:\t");
scanf("%d",&sp);
printf("enter the size of processes:\t");
for(i=0;i<sp;i++)
{scanf("%d",&p[i]);}
printf("processes and blocks entered values\n");
for(i=0;i<sp;i++)
{printf("process%d\t %d\n",i,p[i]);}
for(i=0;i<sb;i++)
{printf("block%d\t %d\n",i,b[i]);}
for(i=0;i<sp;i++)
{for(j=0;j<sb;j++)
{if(p[i]<=b[j])
31 | P a g e
Singh
Brij Bhushan
(1219210901)
Output
Enter the no. of blocks:
4
Enter the sizes of block:
100 500 200 50
Enter the no. of processes:
4
Enter the size of processes: 50 100 500 200
Processes and blocks entered values
process0
50
process1
100
process2
500
process3
200
block0 100
block1 500
block2 200
block3 50
The process0 alloted block0
The process1 alloted block1
The process3 alloted block2
The process2 doesnt get any block
32 | P a g e
Singh
Brij Bhushan
(1219210901)
PROGRAM 12
Write a program to implement WORST FIT algorithm.
#include<conio.h>
#include<stdio.h>
void main()
{intsb,sp,p[10],b[10],i,j;
clrscr();
printf("enter the no. of blocks:\t");
scanf("%d",&sb);
printf("enter the sizes of block:\t");
for(i=0;i<sb;i++)
{scanf("%d",&b[i]);}
printf("enter the no. of processes:\t");
scanf("%d",&sp);
printf("enter the size of processes:\t");
for(i=0;i<sp;i++)
{scanf("%d",&p[i]);}
printf("processes and blocks entered values\n");
for(i=0;i<sp;i++)
{printf("process%d\t %d\n",i,p[i]);}
for(i=0;i<sb;i++)
{printf("block%d\t %d\n",i,b[i]);}
for(i=0;i<sp;i++)
{j=selworst(p[i],b,sb);
if(j!=-1)
{
printf("process%d is allotedblock%d\n",i,j);
p[i]=10000;
33 | P a g e
Singh
Brij Bhushan
(1219210901)
34 | P a g e
Singh
Brij Bhushan
(1219210901)
Output
Enter the no. of blocks:
4
Enter the sizes of block:
100 500 250 50
Enter the no. of processes:
4
Enter the size of processes: 200 50 500 200
Processes and blocks entered values
process0
200
process1
50
process2
500
process3
200
block0 100
block1 500
block2 250
block3 50
Process0 is alloted block1
Process1 is alloted block2
The process2 doesnt get any block
The process3 doesnt get any block
35 | P a g e
Singh
Brij Bhushan
(1219210901)