Program to simulate the round-robin CPU scheduling algorithm

Concept: To aim is to calculate the average waiting time. There will be a time slice, each process should be executed within that time-slice and if not it will go to the waiting state so first check whether the burst time is less than the time-slice. If it is less than it assign the waiting time to the sum of the total times. If it is greater than the burst-time then subtract the time slot from the actual burst time and increment it by time-slot and the loop continues until all the processes are completed.

ALGORITHM:
Step 1: Start 
Step 2: Accept the number of processes in the ready Queue and time quantum (or) time slice
Step 3: For each process in the ready Q, assign the process id and accept the CPU burst time
Step 4: Calculate the no. of time slices for each process where No. of time slice for process (n) = burst time process(n)/time slice
Step 5: If the burst time is less than the time slice then the no. of time slices =1.
Step 6: Consider the ready queue is a circular Q, calculate
        a) Waiting time for process(n) = waiting time of process(n-1)+ burst time of process(n-1 ) + the time difference in getting the                       CPU from process(n-1)
        b) Turnaround time for process(n) = waiting time of process(n) + burst time of process(n)+ the time difference in getting CPU                     from process(n).
Step 7: Calculate
        c) Average waiting time = Total waiting Time / Number of process
        d) Average Turnaround time = Total Turnaround Time / Number of process
Step 8: Stop 

				
					#include<stdio.h>
int ttime,i,j,temp;
main()
{
int pname[10],btime[10],pname2[10],btime2[10];
int n,x,z;
printf("Enter the no. of process:");
scanf("%d",&n);
printf("Enter the process name and burst time for the process\n");
for(i=0;i
{
printf("Enter the process name:");
scanf("%d",&pname2[i]);
printf("Enter burst time for the process %d:",pname2[i]);
scanf("%d",&btime2[i]);
}
printf("PROCESS NAME \t\t BURST TIME\n");
for(i=0;i
printf("%d\t\t\t %d\n",pname2[i],btime2[i]);
z=1;
while(z==1)
{
ttime=0;
for(i=0;i
{ pname[i]=pname2[i];
btime[i]=btime2[i];
}
printf ("PRESS 1.ROUND ROBIN 2.EXIT\n");
scanf("%d",&x);
switch(x)
{
case 1:
rrobin(pname,btime,n);
break;
case 2:
exit(0); break;
default:
printf("Invalid option");
break;
}
printf("\n\n If you want to continue press 1:");
scanf("%d",&z);
}
}
rrobin(int pname[],int btime[],int n)
{
int tslice;
j=0;
printf("\n\t ROUND ROBIN SCHEDULING \n\n");
printf("Enter the time slice:\n");
scanf("%d",&tslice);
printf("PROCESS NAME \t REMAINING TIME\t TOTAL TIME");
while(j
{
for(i=0;i
{
if(btime[i]>0)
{
if(btime[i]>=tslice)
{
ttime+=tslice;
btime[i]=btime[i]-tslice;
printf("\n%d\t\t %d \t\t %d",pname[i],btime[i],ttime);
if(btime[i]==0)
j++;
}
else
{
ttime+=btime[i];
btime[i]=0;
printf("\n%d\t\t %d \t\t %d",pname[i],btime[i],ttime);
}
}
}
}
}
				
			
OUTPUT:
Enter the no. of process: 4
Enter the process name and burst time for the process
Enter the process name: 1
Enter burst time for the process 1: 8
Enter the process name: 2
Enter burst time for the process 2: 3
Enter the process name: 3
Enter burst time for the process 3: 6
Enter the process name: 4
Enter burst time for the process 4: 1
PROCESS NAME BURST TIME
1 8
2 3
3 6
4 1
PRESS 1.ROUND ROBIN 2.EXIT
1
ROUND ROBIN SCHEDULING
Enter the time slice:2
PROCESSNAME REMAININGTIME TOTALTIME
1               6             2
2               1             4
3               4             6
4               0             7
1               4             9
2               0             10
3               2             12
1               2             14
3               0             16

Leave a Reply