Write a C Program that implements the Round Robin Scheduling algorithm and determines the average waiting time and turnaround time.
Round Robin Scheduling is a CPU scheduling algorithm in which each process is executed for a fixed time slot. Since the resources are snatched after the time slot, round robin is preemptive.
Preemptive: In this type, resources can be voluntarily snatched.
Non-Preemptive: In this type, if a process is once started, it will execute completely i.e resources cannot be snatched.
Following are the basic terminologies:
Turnaround Time: Difference between completion time and arrival time.
Turnaround Time = Completion Time – Arrival Time
Waiting Time: Time Difference between turnaround time and burst time.
Waiting Time = Turnaround Time – Burst Time
Round Robin Scheduling Algorithm:
Step 1: Start the Program.
Step 2: Input the number of processes.
Step 3: Input the burst time and arrival time of each process and the limit of the time slot.
Step 4: Push all processes into the ready queue according to their arrival time. Then execute each process upto time slot and push left over process in queue again for execution.
Step 5: After a process is completely executed, print its turn around time and waiting time.
Example:
Following is the example of round robin scheduling.
Process Id | Arrival Time | Burst Time |
---|---|---|
P1 | 0 | 10 |
P2 | 1 | 8 |
P3 | 2 | 7 |
Time Slot is 5 Sec.
First P1 is executed for 5 seconds, left burst time is 5 sec
Then P2 is executed for 5 seconds, left burst time is 3 sec
Then P3 is executed for 5 seconds, left burst time is 2 sec
Then P1 is executed for 5 seconds, execution of P1 is completed.
Then P2 is executed for 3 seconds, execution of P2 is completed.
Then P1 is executed for 2 sec, execution P3 is completed.
Execution of all processes completed
Process Id | Burst Time | Wait Time | Turn Around Time |
---|---|---|---|
P1 | 10 | 20 | 10 |
P2 | 8 | 22 | 14 |
P3 | 7 | 23 | 16 |
Average Waiting Time = (20 + 22 + 23)/3 = 65/3 = 21.666666
Average Turnaround Time = (10 + 14 + 16)/3 = 40/3 = 13.333333
- There is no starvation of resources as each process gets equal share.
- Every Process gets equal allocation of CPU.
- Increases Performance time in terms of response time.
- More time is wasted on context switching.
- Frequent context switching increases CPU overhead.
- Average Waiting time increases for each process.
Here is the source code of the C program to implement Round Robin scheduling. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* Round Robin Scheduling Program in C
*/
#include<stdio.h>
int main()
{
//Input no of processed
int n;
printf("Enter Total Number of Processes:");
scanf("%d", &n);
int wait_time = 0, ta_time = 0, arr_time[n], burst_time[n], temp_burst_time[n];
int x = n;
//Input details of processes
for(int i = 0; i < n; i++)
{
printf("Enter Details of Process %d \n", i + 1);
printf("Arrival Time: ");
scanf("%d", &arr_time[i]);
printf("Burst Time: ");
scanf("%d", &burst_time[i]);
temp_burst_time[i] = burst_time[i];
}
//Input time slot
int time_slot;
printf("Enter Time Slot:");
scanf("%d", &time_slot);
//Total indicates total time
//counter indicates which process is executed
int total = 0, counter = 0,i;
printf("Process ID Burst Time Turnaround Time Waiting Time\n");
for(total=0, i = 0; x!=0; )
{
// define the conditions
if(temp_burst_time[i] <= time_slot && temp_burst_time[i] > 0)
{
total = total + temp_burst_time[i];
temp_burst_time[i] = 0;
counter=1;
}
else if(temp_burst_time[i] > 0)
{
temp_burst_time[i] = temp_burst_time[i] - time_slot;
total += time_slot;
}
if(temp_burst_time[i]==0 && counter==1)
{
x--; //decrement the process no.
printf("\nProcess No %d \t\t %d\t\t\t\t %d\t\t\t %d", i+1, burst_time[i],
total-arr_time[i], total-arr_time[i]-burst_time[i]);
wait_time = wait_time+total-arr_time[i]-burst_time[i];
ta_time += total -arr_time[i];
counter =0;
}
if(i==n-1)
{
i=0;
}
else if(arr_time[i+1]<=total)
{
i++;
}
else
{
i=0;
}
}
float average_wait_time = wait_time * 1.0 / n;
float average_turnaround_time = ta_time * 1.0 / n;
printf("\nAverage Waiting Time:%f", average_wait_time);
printf("\nAvg Turnaround Time:%f", average_turnaround_time);
return 0;
}
1. Ask the user for number of processes n.
2. After that, ask the user for the arrival time and burst time of each process. Also input the time quantum.
3. In the loop, if time slot is greater than left burst time, execute process and find burst time.
4. Else if burst time is greater than time slot, execute it up to time slot and again push into the queue.
5. when the execution is completed, print the process information such as turnaround time and waiting time.
Time Complexity: O(total execution time)
All processes are executed up to their execution, so time complexity is O(total execution time).
Space Complexity: O(n)
Space is required to store burst time and arrival time, so space complexity is O(n).
In this case, we enter “3” as the number of processes, and the arrival time and burst time are “p1: 0 10”, “p2: 1 8”, and “p3: 2 7” to find average waiting time and average turnaround time using Round Robin Scheduling algorithm. (Quantum Time = 5)
Enter Total Number of Processes:3 Enter Details of Process 1 Arrival Time: 0 Burst Time: 10 Enter Details of Process 2 Arrival Time: 1 Burst Time: 8 Enter Details of Process 3 Arrival Time: 2 Burst Time: 7 Enter Time Slot:5 Process ID Burst Time Turnaround Time Waiting Time Process No 1 10 20 10 Process No 2 8 22 14 Process No 3 7 23 16 Average Waiting Time: 13.333333 Avg Turnaround Time: 21.666666
To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.
If you find any mistake above, kindly email to [email protected]- Watch Advanced C Programming Videos
- Practice BCA MCQs
- Apply for Computer Science Internship
- Practice Computer Science MCQs
- Check Computer Science Books