Os Practical File
Os Practical File
Semester : 5th
CONTENTS
#include<stdio.h>
#include<conio.h>
int main()
{
int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;
printf("Enter total number of processes(maximum 20):");
scanf("%d",&n);
return 0;
}
Output
#include<stdio.h>
#include<conio.h>
void main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process:");
scanf("%d",&n);
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0; //waiting time for first process will be zero
//calculate waiting time
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
avg_wt=(float)total/n; //average waiting time
total=0;
Output
#include<stdio.h>
int main()
{
int
bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_t
at;
printf("Enter Total Number of Process:");
scanf("%d",&n);
temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
total+=wt[i];
}
return 0;
}
Output
Enter the total no. of processes:4
Enter the process burst time and priority
P1
Burst time :6
priority: 3
P2
Burst time :2
priority: 2
P3
Burst time :14
priority: 1
P4
Burst time :6
priority: 4
PROGRAM 4
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");
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];
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
turn around time is 15.666667
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++)
{
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;
}
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
PROGRAM 6
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;}
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++)
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