Write the simulation program for Round Robin with time quantum of 2 units. The arrival of time and first CPU bursts of different jobs should be input to the system. The output should give the Gantt chart and waiting time for each process and average waiting time.
Write the simulation program for Round Robin with time quantum of 2 units. The arrival of time and first CPU bursts of different jobs should be input to the system. The output should give the Gantt chart and waiting time for each process and average waiting time.
Program:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct ps{
int at,wt,bt,ft,st;
}pr[50], temp[50];
int rq[50], time,x,n,t;
void add(){
int i;
for(i=0;i<n;i++){
if(time==pr[i].at){
rq[x++]=i;
}
}
}
int select(){
int pp, i;
if(x==0){
return(-1);
}else{
pp = rq[0];
for(i=0;i<x;i++){
rq[i]=rq[i+1];
}
x--;
return(pp);
}
}
int main(void){
setbuf(stdout,NULL);
float awt;
int i,p;
t=0;
time=0;
x=0;
printf("Enter the Number of processes:");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("\nEnter Arrival Time:");
scanf("%d",&pr[i].at);
printf("\nEnter CPU burst time:");
scanf("%d",&pr[i].bt);
temp[i].at = pr[i].at;
temp[i].bt = pr[i].bt;
}
add();
while(t<n){
p=select();
if(p==-1){
printf("idle");
time++;
add();
} else {
if(temp[p].bt==pr[p].bt){
pr[p].st=time;
for(i=0;i<2;i++){
if(temp[p].bt>0){
printf("P%d|",p);
temp[p].bt--;
}else{
t++;
pr[p].ft=time;
p=select();
if(p==-1){
printf("idle|");
}
}
time++;
add();
}
if(temp[p].bt>0){
rq[x++]=p;
}else{
pr[p].ft=time;
t++;
}
}
}
}
awt=0;
printf("\nWaiting Time for each Process:");
for(i=0;i<n;i++){
pr[i].wt=(pr[i].ft-pr[i].at)-pr[i].bt;
printf("\nP%d = %d",i,pr[i].wt);
awt+=pr[i].wt;
}
awt=awt/n;
printf("\nAverage waiting time:%f",awt);
return EXIT_SUCCESS;
}
Comments
Post a Comment