Round Robin Scheduling Program in C

Problem Description:

Write a C Program that implements the Round Robin Scheduling algorithm and determines the average waiting time and turnaround time.

What is Round Robin Scheduling in C?

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

advertisement
advertisement
Problem Solution

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.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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

Advantages:
  • 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.
Disadvantages:
  • More time is wasted on context switching.
  • Frequent context switching increases CPU overhead.
  • Average Waiting time increases for each process.
Program/Source Code

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.

advertisement
  1. /*
  2.  * Round Robin Scheduling Program in C
  3.  */
  4.  
  5. #include<stdio.h>
  6.  
  7. int main()
  8. {
  9.     //Input no of processed
  10.     int  n;
  11.     printf("Enter Total Number of Processes:");
  12.     scanf("%d", &n);
  13.     int wait_time = 0, ta_time = 0, arr_time[n], burst_time[n], temp_burst_time[n];
  14.     int x = n;
  15.  
  16.     //Input details of processes
  17.     for(int i = 0; i < n; i++)
  18.     {
  19.         printf("Enter Details of Process %d \n", i + 1);
  20.         printf("Arrival Time:  ");
  21.         scanf("%d", &arr_time[i]);
  22.         printf("Burst Time:   ");
  23.         scanf("%d", &burst_time[i]);
  24.         temp_burst_time[i] = burst_time[i];
  25.     }
  26.  
  27.     //Input time slot
  28.     int time_slot;
  29.     printf("Enter Time Slot:");
  30.     scanf("%d", &time_slot);
  31.  
  32.     //Total indicates total time
  33.     //counter indicates which process is executed
  34.     int total = 0,  counter = 0,i;
  35.     printf("Process ID       Burst Time       Turnaround Time      Waiting Time\n");
  36.     for(total=0, i = 0; x!=0; )  
  37.     {  
  38.         // define the conditions
  39.         if(temp_burst_time[i] <= time_slot && temp_burst_time[i] > 0)    
  40.         {  
  41.             total = total + temp_burst_time[i];  
  42.             temp_burst_time[i] = 0;  
  43.             counter=1;  
  44.         }     
  45.         else if(temp_burst_time[i] > 0)  
  46.         {  
  47.             temp_burst_time[i] = temp_burst_time[i] - time_slot;  
  48.             total  += time_slot;    
  49.         }  
  50.         if(temp_burst_time[i]==0 && counter==1)  
  51.         {  
  52.             x--; //decrement the process no.  
  53.             printf("\nProcess No %d  \t\t %d\t\t\t\t %d\t\t\t %d", i+1, burst_time[i],
  54.                    total-arr_time[i], total-arr_time[i]-burst_time[i]);  
  55.             wait_time = wait_time+total-arr_time[i]-burst_time[i];  
  56.             ta_time += total -arr_time[i];  
  57.             counter =0;     
  58.         }  
  59.         if(i==n-1)  
  60.         {  
  61.             i=0;  
  62.         }  
  63.         else if(arr_time[i+1]<=total)  
  64.         {  
  65.             i++;  
  66.         }  
  67.         else  
  68.         {  
  69.             i=0;  
  70.         }  
  71.     }  
  72.     float average_wait_time = wait_time * 1.0 / n;
  73.     float average_turnaround_time = ta_time * 1.0 / n;
  74.     printf("\nAverage Waiting Time:%f", average_wait_time);
  75.     printf("\nAvg Turnaround Time:%f", average_turnaround_time);
  76.     return 0;
  77. }
Program Explanation

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).

advertisement
Run Time Testcases

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]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.