Os Lab Pgms-1-5
Os Lab Pgms-1-5
Os Lab Pgms-1-5
a) Round Robin
#include<stdio.h>
void main()
{
int bt[10],bt1[10],wt[10],tt[10],i,k,t=0,twt=0,tat=0,q,n;
float awt,att;
printf("Enter no of process:");
scanf("%d",&n);
k=n;
for(i=1;i<=n;i++)
{
printf("Enter burst time of p%d:",i);
scanf("%d",&bt[i]);
bt1[i]=bt[i];
}
printf("Enter quantum time:");
scanf("%d",&q);
for(i=1;k!=0;)
{
if(bt1[i]<=q&&bt1[i]>0)
{
t=t+bt1[i];
bt1[i]=0;
wt[i]=t-bt[i];
tt[i]=t;
k--;
}
else if(bt1[i]>q)
{
bt1[i]=bt1[i]-q;
t=t+q;
}
if(i==n)
i=1;
else
i++;
}
for(i=1;i<=n;i++)
{
printf("Waiting and turn around time of p%d:%d %d\n",i,wt[i],tt[i]);
twt=twt+wt[i];
tat=tat+tt[i];
}
awt=twt/(float)n;
att=tat/(float)n;
printf("Average waiting time is:%f\n",awt);
printf("Average turn around time is:%f\n",att);
}
Page 1 of 33
b) SJF
#include<stdio.h>
void main()
{
//declaration section
int tt[10],bt[10],wt[10],p[10],i,k,j,n;
float awt=0,att=0;
printf("Enter no of process:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter burst time of p%d:",i);
scanf("%d",&bt[i]);
p[i]=i;
}
//logic section
printf("Now the process are scheduled as below:\n");
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(bt[i]>bt[j])
{
k=bt[i];
bt[i]=bt[j];
bt[j]=k;
k=p[i];
p[i]=p[j];
p[j]=k;
}
}
printf("p%d\t",p[i]);
}
printf("\n");
for(i=1;i<=n;i++)
{
if(i==1)
{
wt[i]=0;
}
else
{
wt[i]=wt[i-1]+bt[i-1];
}
awt=awt+wt[i];
printf("The waiting time of p%d:%d\n",p[i],wt[i]);
}
awt=awt/(float)n;
for(i=1;i<=n;i++)
{
tt[i]=bt[i]+wt[i];
att=att+tt[i];
printf("The turn around time of p%d:%d\n",p[i],tt[i]);
}
Page 2 of 33
att=att/(float)n;
printf("Average waiting time:%f\n",awt);
printf("Average turn around time:%f\n",att);
}
c) FCFS
#include<stdio.h>
void main()
{
int tt[10],bt[10],wt[10],i;
float awt=0,att=0,n;
printf("Enter no of process:");
scanf("%f",&n);
for(i=1;i<=n;i++)
{
printf("Enter burst time of p%d:",i);
scanf("%d",&bt[i]);
}
for(i=1;i<=n;i++)
{
if(i==1)
{
wt[i]=0;
}
else
{
wt[i]=wt[i-1]+bt[i-1];
}
awt=awt+wt[i];
printf("The waiting time of p%d:%d\n",i,wt[i]);
}
awt=awt/n;
for(i=1;i<=n;i++)
{
tt[i]=bt[i]+wt[i];
att=att+tt[i];
printf("The turn around time of p%d:%d\n",i,tt[i]);
}
att=att/n;
printf("Average waiting time:%f\n",awt);
printf("Average turn around time:%f\n",att);
}
d) Priority
#include<stdio.h>
void main()
{
int tt[10],bt[10],wt[10],pt[10],p[10],i,k,j;
float awt=0,att=0,n;
printf("Enter no of process:");
scanf("%f",&n);
for(i=1;i<=n;i++)
{
Page 3 of 33
printf("Enter burst time and priority of p%d:",i);
scanf("%d %d",&bt[i],&pt[i]);
p[i]=i;
}
printf("process\t burst time\t priority\n");
for(i=1;i<=n;i++)
{
printf("p%d\t%4d\t\t%4d\n",i,bt[i],pt[i]);
}
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(pt[i]<pt[j])
{
{
k=pt[i];
pt[i]=pt[j];
pt[j]=k;
}
{
k=bt[i];
bt[i]=bt[j];
bt[j]=k;
}
{
k=p[i];
p[i]=p[j];
p[j]=k;
}
}
}
}
printf("process are scheduled as below:\n");
for(i=1;i<=n;i++)
{
printf("%d\t",p[i]);
}
printf("\n");
for(i=1;i<=n;i++)
{
if(i==1)
{
wt[i]=0;
}
else
{
wt[i]=wt[i-1]+bt[i-1];
}
awt=awt+wt[i];
printf("The waiting time of p%d:%d\n",p[i],wt[i]);
}
awt=awt/n;
for(i=1;i<=n;i++)
Page 4 of 33
{
tt[i]=bt[i]+wt[i];
att=att+tt[i];
printf("The turn around time of p%d:%d\n",p[i],tt[i]);
}
att=att/n;
printf("Average waiting time:%f\n",awt);
printf("Average turn around time:%f\n",att);
}
#include<stdio.h>
struct fileTable
{
char name[20];
int sb,nob;
}ft[30];
void main()
{
int i, j, n;
char s[20];
printf("Enter no of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter file name %d :",i+1);
scanf("%s",ft[i].name);
printf("Enter starting block of file %d :",i+1);
scanf("%d",&ft[i].sb);
printf("Enter no of blocks in file %d :",i+1);
scanf("%d",&ft[i].nob);
}
printf("\nEnter the file name to be searched: ");
scanf("%s",s);
for(i=0;i<n;i++)
{
if(strcmp(s, ft[i].name)==0)
break;
}
if(i==n)
printf("\nFile Not Found");
else
{
printf("\nFILE NAME START BLOCK NO OF BLOCKS BLOCKS OCCUPIED\n");
printf("\n%s\t\t%d\t\t%d\t",ft[i].name,ft[i].sb,ft[i].nob); for(j=0;j<ft[i].nob;j++)
printf("%d, ",ft[i].sb+j);
}
}
Page 5 of 33