G. L.
Bajaj Institute of Technology &
Management
Greater Noida
Uttar Pradesh Technical University
Lucknow
G.L. Bajaj Institute of
Technology & Management
B. TECH.
Computer Science & Engineering
Department
(Session: 2013-14) ODD SEMESTER
Batch (2011-15)
THIRD YEAR (V SEMESTER)
OS LAB FILE
Submitted to
Submitted By
Mr. Laxmi Kant Sagar
Singh
(1219210901)
Brij Bhushan
CONTENTS
[Link].
NAME OF THE PROGRAM
1.
WAP to implement First
Come First Serve (FCFS)
Scheduling
WAP to implement
shortest job first (SJF)
scheduling
WAP to implement
Priority based scheduling
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
G. L. Bajaj Institute of Technology & Management
Plot no. 2, Knowledge Park III, Greater Noida, UP-201308
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
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)
G. L. Bajaj Institute of Technology & Management
Plot no. 2, Knowledge Park III, Greater Noida, UP-201308
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
{
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)
G. L. Bajaj Institute of Technology & Management
Plot no. 2, Knowledge Park III, Greater Noida, UP-201308
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
for(i=1;i<n;i++)
{
burst=burst+mat[i][2];
att[i]=(burst-mat[i][1]);
}
for(i=0;i<n;i++)
attime=attime+att[i];
attime=attime/n;
printf("\nAvg. Turnaround time=%f",attime);
getch();
}
Output
Enter
Enter
0
24
Enter
0
3
Enter
0
3
the no. of processes:3
arrival time and burst time for 1 process:
arrival time and burst time for 2 process:
arrival time and burst time for 3 process:
Process Arrival time Burst time
1
0
24
2
0
3
3
0
3
After sorting processes acc. to arrival time:
Process
1
2
3
Arrival time
0
0
0
Burst time
24
3
3
3|Page
Singh
Brij Bhushan
(1219210901)
G. L. Bajaj Institute of Technology & Management
Plot no. 2, Knowledge Park III, Greater Noida, UP-201308
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
Avg. Waiting time= 17.000000
Avg. Turnaround time=27.000000
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)
G. L. Bajaj Institute of Technology & Management
Plot no. 2, Knowledge Park III, Greater Noida, UP-201308
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
{ sw=sw+(c[i-1]-a[i]);
st=st+(c[i]-a[i]);
}
awt=sw/n;
att=st/n;
printf("\n\naverage waiting time is:%f",awt);
printf("\naverage turnaround time is:%f",att);
getch();
}
void sort(int a[],int b[],int n)
{
inti,j,temp,t;
for(i=2;i<=n;i++)
{ for(j=i+1;j<=n;j++)
{ if(a[i]>a[j])
{ temp=a[i];
a[i]=a[j];
a[j]=temp;
t=b[i];
b[i]=b[j];
b[j]=t;
}
}
}
}
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)
G. L. Bajaj Institute of Technology & Management
Plot no. 2, Knowledge Park III, Greater Noida, UP-201308
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
burst time:4
average waiting time is:7.250000
average turnaround time is:12.750000
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)
G. L. Bajaj Institute of Technology & Management
Plot no. 2, Knowledge Park III, Greater Noida, UP-201308
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
pri[j]=pri[j+1];
pri[j+1] = temp;
}
printf("table - \n");
for(i=0;i<n;i++)
printf("\nArrival time = %d Burst time = %d Priority =
%d",arr[i],bur[i],pri[i]);
for(i=1;i<n-1;i++)
for(j=0;j<(n-i-1);j++)
if(pri[j]>pri[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];
pri[j]=pri[j+1];
pri[j+1] = temp;
}
for(i=0;i<n;i++)
{
x= x+bur[i];
tot[i] = x;
}
for(i=0;i<n;i++)
{
if(i==0)
awt = awt+arr[i];
else
awt = awt + (tot[i-1] - arr[i]);
}
awt = awt/n;
for(i=0;i<n;i++)
tt = tt + tot[i] - arr[i];
tt = tt/n;
7|Page
Singh
Brij Bhushan
(1219210901)
G. L. Bajaj Institute of Technology & Management
Plot no. 2, Knowledge Park III, Greater Noida, UP-201308
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
printf("Average waiting time = %f",awt);
printf("Average turn around time = %f",tt);
getch();
}
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 -
burst and priority for 0 process
burst and priority for 1 process
burst and priority for 2 process
burst and priority for 3 process
Arrival time = 0 Burst time = 10 Priority = 4
Arrival time = 1 Burst time = 6 Priority = 3
Arrival time = 3 Burst time = 2 Priority = 1
Arrival time = 5 Burst time = 4 Priority = 2Average waiting time =
6.250000
Average turn around time = 10.250000
8|Page
Singh
Brij Bhushan
(1219210901)
G. L. Bajaj Institute of Technology & Management
Plot no. 2, Knowledge Park III, Greater Noida, UP-201308
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
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)
G. L. Bajaj Institute of Technology & Management
Plot no. 2, Knowledge Park III, Greater Noida, UP-201308
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
for(i=0;i<4;i++)
{
if(need[i][0]<=ava[0]&&need[i][1]<=ava[1]&&finish[i]==0)
{
ava[0] =ava[0]+all[i][0];
ava[1] =ava[1]+all[i][1];
finish[i]=1;
printf("P%d-->",i);
}
}
for(i=0;i<4;i++)
{
if(finish[i]==0)
{
flag =0;
break;
}
}
if(flag ==1)
printf("\nSystem is in safe state");
else
printf("\nSystem is in unsafe state");
getch();
}
10 | P a g e
Singh
Brij Bhushan
(1219210901)
G. L. Bajaj Institute of Technology & Management
Plot no. 2, Knowledge Park III, Greater Noida, UP-201308
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
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)
G. L. Bajaj Institute of Technology & Management
Plot no. 2, Knowledge Park III, Greater Noida, UP-201308
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
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)
G. L. Bajaj Institute of Technology & Management
Plot no. 2, Knowledge Park III, Greater Noida, UP-201308
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
flag1=0,flag2=0;
for(i=0;i<frsize;i++)
if(fr[i]==p[j])
{
flag1=1;
flag2=1;
break;
}
if(flag1==0)
//if there is an empty place
{
for(i=0;i<frsize;i++)
if(fr[i]==-1)
{
fr[i]=p[j];
flag2=1;
pf++;
break;
}
}
if(flag2==0) //if there is no empty place
{
for(i=0;i<frsize;i++)
fs[i]=0;
for(k=j-1,l=1;l<=frsize-1;l++,k--)
{
for(i=0;i<frsize;i++)
{
if(fr[i]==p[k])
fs[i]=1;
}
}
for(i=0;i<frsize;i++)
{
if(fs[i]==0)
index=i;
}
13 | P a g e
Singh
Brij Bhushan
(1219210901)
G. L. Bajaj Institute of Technology & Management
Plot no. 2, Knowledge Park III, Greater Noida, UP-201308
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
fr[index]=p[j];
pf++;
}
display();
}
pfr=pf/frsize;
printf("\n no of page faults :%f",pf);
printf("\n page fault rate : %f", pfr);
getch();
}
void display()
{
inti;
printf("\n");
for(i=0;i<frsize;i++)
printf("\t%d",fr[i]);
}
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)
G. L. Bajaj Institute of Technology & Management
Plot no. 2, Knowledge Park III, Greater Noida, UP-201308
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
1
3
2
1
0
2
1
0
2
1
0
7
1
0
7
1
0
7
no of page faults :12.000000
page fault rate : 4.000000
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)
G. L. Bajaj Institute of Technology & Management
Plot no. 2, Knowledge Park III, Greater Noida, UP-201308
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
scanf("%d",&time);
printf("process\t arrival time \t burst time\n");
for(i=0;i<n;i++)
{printf("P%d\t %d \t\t %d\n",prcs[i],arv[i],brst[i]);}
printf("\n");
for(i=0;i<n;i++)
{for(j=0;j<n-1;j++)
{if(arv[j]>arv[j+1])
{tempa=arv[j];
arv[j]=arv[j+1];
arv[j+1]=tempa;
tempb=brst[j];
brst[j]=brst[j+1];
brst[j+1]=tempb;
tempp=prcs[j];
prcs[j]=prcs[j+1];
prcs[j+1]=tempp; }}}
max=brst[0];
maxi=0;
for(i=1;i<n;i++)
{if(max<brst[i])
{max=brst[i];
maxi=i;} }
while( y>0 )
{ for(i=0;i<n;i++)
{if (brst[i]>0)
{if (brst[i]>time)
{brst[i]=brst[i]-time;
st=time+st;
wt[i]=wt[i]+time;}
else
{ wtt[i]=st-wt[i];
st=st+brst[i];
printf("wtt of %d is %d",i,wtt[i]);
wat=wat+wtt[i]-arv[i];
16 | P a g e
Singh
Brij Bhushan
(1219210901)
G. L. Bajaj Institute of Technology & Management
Plot no. 2, Knowledge Park III, Greater Noida, UP-201308
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
tt=tt+st-arv[i];
printf("\nfor %d waiting time is %f \nturn around time is %f",i,wat/n,tt/n);
brst[i]=0;} }
if(brst[maxi]==0)
y=0;}}
printf("\nwaiting time is %f \nturn around time is %f",wat/n,tt/n);
getch();}
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)
G. L. Bajaj Institute of Technology & Management
Plot no. 2, Knowledge Park III, Greater Noida, UP-201308
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
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)
G. L. Bajaj Institute of Technology & Management
Plot no. 2, Knowledge Park III, Greater Noida, UP-201308
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
p=0;
ft=1;
for(i=1;i<=n;i++)
{c=0;
for(j=1;j<=k;j++)
{if(a[i]==f[j])
{c=1;}}
if (c==1)
{p=p;}
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)
G. L. Bajaj Institute of Technology & Management
Plot no. 2, Knowledge Park III, Greater Noida, UP-201308
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
for(m=1;m<=k;m++)
{c=0;
for(i=curposit+1;(i<=n)&&(c==0);i++)
{if (f[m]==a[i])
{futp[m]=n-i+1;
c=1;}
else
{futp[m]=-1;}
}
}
t=futp[1];
ch=1;
for(i=2;i<=k;i++)
{if(t>futp[i])
{t=futp[i];
ch=i;}}
returnch;
}
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)
G. L. Bajaj Institute of Technology & Management
Plot no. 2, Knowledge Park III, Greater Noida, UP-201308
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
Elements in frame after 12 iteration:
Elements in frame after 13 iteration:
Elements in frame after 14 iteration:
Elements in frame after 15 iteration:
Page faults are 8
Page fault rate is 2.666667
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)
G. L. Bajaj Institute of Technology & Management
Plot no. 2, Knowledge Park III, Greater Noida, UP-201308
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
if(n==2)
signal();
else
if(n==3)
exit();
}
while(1);
}
void wait()
{
count--;
if(count<0)
{
printf("process
}
else
printf("process
}
void signal()
{
printf("process
count++;
if(count<=0)
printf("process
}
is linked into the queue at %d",count);
executing in cs");
completed execution in cs\n");
next in queue can execute");
Output
22 | P a g e
Singh
Brij Bhushan
(1219210901)
G. L. Bajaj Institute of Technology & Management
Plot no. 2, Knowledge Park III, Greater Noida, UP-201308
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
23 | P a g e
Singh
Brij Bhushan
(1219210901)
G. L. Bajaj Institute of Technology & Management
Plot no. 2, Knowledge Park III, Greater Noida, UP-201308
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
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)
G. L. Bajaj Institute of Technology & Management
Plot no. 2, Knowledge Park III, Greater Noida, UP-201308
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
printf("%d item being produce",k);
}}
else
if(n==2)
{
printf("how many item u want to consume");
scanf("%d",&i);
if(buffer<1)
printf("no more item in buffer");
else
{for(j=1;j<=i;j++)
{k=j;
buffer--;
if(buffer<1)
break;
}
if(k==0)
printf("no more item in buffer");
else
printf("%d item being consumed",k);
}}
else
if(n==3)
exit();
}while(1);
}
25 | P a g e
Singh
Brij Bhushan
(1219210901)
G. L. Bajaj Institute of Technology & Management
Plot no. 2, Knowledge Park III, Greater Noida, UP-201308
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
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)
G. L. Bajaj Institute of Technology & Management
Plot no. 2, Knowledge Park III, Greater Noida, UP-201308
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
2 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 consume5
no more item in buffer
Plese select from following
enter 1 to produce
enter 2 to consume
enter 3 to exit
27 | P a g e
Singh
Brij Bhushan
(1219210901)
G. L. Bajaj Institute of Technology & Management
Plot no. 2, Knowledge Park III, Greater Noida, UP-201308
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
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)
G. L. Bajaj Institute of Technology & Management
Plot no. 2, Knowledge Park III, Greater Noida, UP-201308
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
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)
G. L. Bajaj Institute of Technology & Management
Plot no. 2, Knowledge Park III, Greater Noida, UP-201308
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
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)
G. L. Bajaj Institute of Technology & Management
Plot no. 2, Knowledge Park III, Greater Noida, UP-201308
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
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)
G. L. Bajaj Institute of Technology & Management
Plot no. 2, Knowledge Park III, Greater Noida, UP-201308
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
{printf("the process%dallotedblock%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();}
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)
G. L. Bajaj Institute of Technology & Management
Plot no. 2, Knowledge Park III, Greater Noida, UP-201308
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
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)
G. L. Bajaj Institute of Technology & Management
Plot no. 2, Knowledge Park III, Greater Noida, UP-201308
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
b[j]=0;}}
for(i=0;i<sp;i++)
{if(p[i]!=10000)
printf("the process%ddoesnt get any block\n",i);}
getch();}
intselworst(intp,int b[10],int bi)
{int k=0,ar[10],arin[10],max,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
{max=ar[0];
index=arin[0];
for(i=0;i<k;i++)
{if(max<ar[i])
{max=ar[i];
index=arin[i];}}
return index;}
}
34 | P a g e
Singh
Brij Bhushan
(1219210901)
G. L. Bajaj Institute of Technology & Management
Plot no. 2, Knowledge Park III, Greater Noida, UP-201308
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
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)