1) Write a program that will simulate FCFS, SJN, SRT, and Round Robin scheduling
ID: 3740322 • Letter: 1
Question
1) Write a program that will simulate FCFS, SJN, SRT, and Round Robin scheduling algorithms. For each algorithm, the program should compute waiting time and turnaround time of every job as well as the average waiting time and average turnaround time. The average values should be consolidated in a table for easy comparison. You may use the following data to test your program. The time quantum for round robin is 4 ms. (Assume that context switching time is 0) Job Arrival Times CPU Cycle (in milliseconds) 0 10 10 12 14 16 17 19 4 1?Explanation / Answer
C language program
For FCFS:
#include<stdio.h>
main()
{
int n,a[10],b[10],t[10],w[10],g[10],i,m;
float att=0,awt=0;
for(i=0;i<10;i++)
{
a[i]=0; b[i]=0; w[i]=0; g[i]=0;
}
printf("enter the number of process");
scanf("%d",&n);
printf("enter the burst times");
for(i=0;i<n;i++)
scanf("%d",&b[i]);
printf(" enter the arrival times");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
g[0]=0;
for(i=0;i<10;i++)
g[i+1]=g[i]+b[i];
for(i=0;i<n;i++)
{
w[i]=g[i]-a[i];
t[i]=g[i+1]-a[i];
awt=awt+w[i];
att=att+t[i];
}
awt =awt/n;
att=att/n;
printf(" process waiting time turn arround time ");
for(i=0;i<n;i++)
{
printf(" p%d %d %d ",i,w[i],t[i]);
}
printf("the average waiting time is %f ",awt);
printf("the average turn around time is %f ",att);
}
For SJF :
#include<stdio.h>
int main()
{
int n,j,temp,temp1,temp2,pr[10],b[10],t[10],w[10],p[10],i;
float att=0,awt=0;
for(i=0;i<10;i++)
{
b[i]=0;w[i]=0;
}
printf("enter the number of process");
scanf("%d",&n);
printf("enter the burst times");
for(i=0;i<n;i++)
{
scanf("%d",&b[i]);
p[i]=i;
}
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
if(b[i]>b[j])
{
temp=b[i];
temp1=p[i];
b[i]=b[j];
p[i]=p[j];
b[j]=temp;
p[j]=temp1;
}
}
}
w[0]=0;
for(i=0;i<n;i++)
w[i+1]=w[i]+b[i];
for(i=0;i<n;i++)
{
t[i]=w[i]+b[i];
awt=awt+w[i];
att=att+t[i];
}
awt=awt/n;
att=att/n;
printf(" process waiting time turn around time ");
for(i=0;i<n;i++)
printf(" p[%d] %d %d ",p[i],w[i],t[i]);
printf("the average waitingtimeis %f ",awt);
printf("the average turn around time is %f ",att);
return 1;
}
For SRT:
#include<stdio.h>
main()
{
int at[10],bt[10],rt[10],endTime,i,smallest,processGantt[100];
int remain=0,n,time,sum_wait=0,sum_turnaround=0;
printf("Enter no of Processes : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter arrival time for Process P%d : ",i+1);
scanf("%d",&at[i]);
printf("Enter burst time for Process P%d : ",i+1);
scanf("%d",&bt[i]);
rt[i]=bt[i];
}
printf(" Process |Turnaround Time| Waiting Time ");
rt[9]=9999;
for(time=0;remain!=n;time++)
{
smallest=9;
for(i=0;i<n;i++)
{
if(at[i]<=time && rt[i]<rt[smallest] && rt[i]>0)
{
processGantt[time]=i;
smallest=i;
}
}
rt[smallest]--;
if(rt[smallest]==0)
{
remain++;
endTime=time+1;
printf(" P[%d] | %d | %d",smallest+1,endTime-at[smallest],endTime-bt[smallest]-at[smallest]);
sum_wait+=endTime-bt[smallest]-at[smallest];
sum_turnaround+=endTime-at[smallest];
}
}
printf(" Average waiting time = %f ",sum_wait*1.0/n);
printf("Average Turnaround time = %f ",sum_turnaround*1.0/5);
for(i=0;i<=time;i++)
{
printf("%d->P%d ",i,processGantt[i]+1);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.