C Program for Round Robin algorithm
#include<stdio.h>
#include<stdlib.h>
struct procdetail
{
int pno;
int arrivaltime;
int bursttime;
int waittime;
int turnaroundtime;
int prevslot;
}proces[100]={0},temp;
int main()
{
int slottime,slot,i,j,complete=0,no,idle;
float avgwait=0,avgturn=0;
printf("Enter time allocated for a slot?\n");
scanf("%d",&slottime);
printf("Enter No of process?\n");
scanf("%d",&no);
printf("Enter the process details?\n");
for(i=0;i<no;i++)
{
printf("Enter the arrival time and burst tiime of process p%d\n",i);
scanf("%d%d",&proces[i].arrivaltime,&proces[i].bursttime);
proces[i].pno=i;
}
for(i=0;i<no-1;i++)
{
for(j=0;j<no-i-1;j++)
{
if(proces[j].arrivaltime>proces[j+1].arrivaltime)
{
temp=proces[j];
proces[j]=proces[j+1];
proces[j+1]=temp;
}
}
}
slot=0;
i=0;
idle=0;
while(!complete)
{
if(proces[i].arrivaltime<=slot && proces[i].bursttime>0)
{
idle=0;
if(proces[i].bursttime>slottime)
{
proces[i].bursttime=proces[i].bursttime-slottime;
proces[i].waittime=proces[i].waittime+(slot-proces[i].prevslot);
proces[i].turnaroundtime=proces[i].turnaroundtime+slottime;
slot=slot+slottime;
proces[i].prevslot=slot;
}
else
{
proces[i].waittime=proces[i].waittime+(slot-proces[i].prevslot);
proces[i].turnaroundtime=proces[i].turnaroundtime+proces[i].bursttime;
slot=slot+proces[i].bursttime;
proces[i].prevslot=slot;
proces[i].bursttime=0;
}
printf("P%d ",proces[i].pno);
}
if(idle==no)
{
slot=slot+1;
idle=0;
}
idle++;
i=(i+1)%no;
complete=1;
for(j=0;j<no;j++)
{
if(proces[j].bursttime!=0)
{
complete=0;
break;
}
}
}
printf("\n process\twaiting Time\t \ttourn around time\n");
for(i=0;i<no;i++)
{
proces[i].waittime=proces[i].waittime-proces[i].arrivaltime;
proces[i].turnaroundtime=proces[i].turnaroundtime+proces[i].waittime;
printf("p%d\t%d\t \t\t\t%d\n",proces[i].pno,proces[i].waittime,proces[i].turnaroundtime);
avgturn=avgturn+proces[i].turnaroundtime+proces[i].waittime;
avgwait=avgwait+proces[i].waittime;
}
printf("Average waiting time= %f\n",(float)avgwait/no);
printf("Average Turn around time= %f\n",(float)avgturn/no);
}
#include<stdio.h>
#include<stdlib.h>
struct procdetail
{
int pno;
int arrivaltime;
int bursttime;
int waittime;
int turnaroundtime;
int prevslot;
}proces[100]={0},temp;
int main()
{
int slottime,slot,i,j,complete=0,no,idle;
float avgwait=0,avgturn=0;
printf("Enter time allocated for a slot?\n");
scanf("%d",&slottime);
printf("Enter No of process?\n");
scanf("%d",&no);
printf("Enter the process details?\n");
for(i=0;i<no;i++)
{
printf("Enter the arrival time and burst tiime of process p%d\n",i);
scanf("%d%d",&proces[i].arrivaltime,&proces[i].bursttime);
proces[i].pno=i;
}
for(i=0;i<no-1;i++)
{
for(j=0;j<no-i-1;j++)
{
if(proces[j].arrivaltime>proces[j+1].arrivaltime)
{
temp=proces[j];
proces[j]=proces[j+1];
proces[j+1]=temp;
}
}
}
slot=0;
i=0;
idle=0;
while(!complete)
{
if(proces[i].arrivaltime<=slot && proces[i].bursttime>0)
{
idle=0;
if(proces[i].bursttime>slottime)
{
proces[i].bursttime=proces[i].bursttime-slottime;
proces[i].waittime=proces[i].waittime+(slot-proces[i].prevslot);
proces[i].turnaroundtime=proces[i].turnaroundtime+slottime;
slot=slot+slottime;
proces[i].prevslot=slot;
}
else
{
proces[i].waittime=proces[i].waittime+(slot-proces[i].prevslot);
proces[i].turnaroundtime=proces[i].turnaroundtime+proces[i].bursttime;
slot=slot+proces[i].bursttime;
proces[i].prevslot=slot;
proces[i].bursttime=0;
}
printf("P%d ",proces[i].pno);
}
if(idle==no)
{
slot=slot+1;
idle=0;
}
idle++;
i=(i+1)%no;
complete=1;
for(j=0;j<no;j++)
{
if(proces[j].bursttime!=0)
{
complete=0;
break;
}
}
}
printf("\n process\twaiting Time\t \ttourn around time\n");
for(i=0;i<no;i++)
{
proces[i].waittime=proces[i].waittime-proces[i].arrivaltime;
proces[i].turnaroundtime=proces[i].turnaroundtime+proces[i].waittime;
printf("p%d\t%d\t \t\t\t%d\n",proces[i].pno,proces[i].waittime,proces[i].turnaroundtime);
avgturn=avgturn+proces[i].turnaroundtime+proces[i].waittime;
avgwait=avgwait+proces[i].waittime;
}
printf("Average waiting time= %f\n",(float)avgwait/no);
printf("Average Turn around time= %f\n",(float)avgturn/no);
}
No comments:
Post a Comment