Round Robin Scheduling Program in C

By | September 7, 2016

Let us learn how to implement the Round Robin scheduling program in C language with its explanation, output, advantages, disadvantages and much more.

What is Round Robin Scheduling Algorithm?

  • The Round robin algorithm is a pre-emptive process scheduling algorithm. Here, every job request in the queue is associated with a fixed execution time called as quantum.
  • A pre-emptive process enables the job scheduler to pause a process under execution and move to the next process in the job queue.
  • Once a particular process/job is executed for a given time quantum, the job scheduler saves the current state of the job and moves to another job in the queue.
  • The context switch saves the current state of the process. This algorithm is beneficial in terms of its response time.
  • In the round robin algorithm, every process gets an equal time of execution which is defined by the quantum time. Therefore, no process will be able to hold the CPU for a longer time period.
  • The round-robin job scheduling algorithm is, therefore, used in a multi-user, time-sharing or multi-tasking operating systems.
  • As a result, it is probably the best scheduling algorithm in operating systems for distributed terminal response time.
  • Furthermore, the efficiency of this algorithm is totally dependent on the size of the time quantum and number of context switches that occur.

Advantages

  • The decision making overhead is very low.
  • Equal priority is given to each job within the queue, unlike other scheduling algorithms.
  • Hence, starvation does not occur so frequently.

Disadvantages

  • The throughput in round-robin algorithm is highly dependent on the quantum length.
  • If the quantum is less, then the process switching occurs frequently which decreases the efficiency.
  • If the quantum is more, the system may become unresponsive.

Some of the famous scheduling algorithms in C programs that you must know are:

Note: This round robin scheduling program in C language using arrival time and an Array data structure is compiled with GNU GCC compiler using Linux terminal on Linux Ubuntu operating system.

 

Round Robin Scheduling Program in C

#include<stdio.h> 
 
int main() 
{ 
      int i, limit, total = 0, x, counter = 0, time_quantum; 
      int wait_time = 0, turnaround_time = 0, arrival_time[10], burst_time[10], temp[10]; 
      float average_wait_time, average_turnaround_time;
      printf("\nEnter Total Number of Processes:\t"); 
      scanf("%d", &limit); 
      x = limit; 
      for(i = 0; i < limit; i++) 
      {
            printf("\nEnter Details of Process[%d]\n", i + 1);
            printf("Arrival Time:\t");
            scanf("%d", &arrival_time[i]);
            printf("Burst Time:\t");
            scanf("%d", &burst_time[i]); 
            temp[i] = burst_time[i];
      } 
      printf("\nEnter Time Quantum:\t"); 
      scanf("%d", &time_quantum); 
      printf("\nProcess ID\t\tBurst Time\t Turnaround Time\t Waiting Time\n");
      for(total = 0, i = 0; x != 0;) 
      { 
            if(temp[i] <= time_quantum && temp[i] > 0) 
            { 
                  total = total + temp[i]; 
                  temp[i] = 0; 
                  counter = 1; 
            } 
            else if(temp[i] > 0) 
            { 
                  temp[i] = temp[i] - time_quantum; 
                  total = total + time_quantum; 
            } 
            if(temp[i] == 0 && counter == 1) 
            { 
                  x--; 
                  printf("\nProcess[%d]\t\t%d\t\t %d\t\t\t %d", i + 1, burst_time[i], total - arrival_time[i], total - arrival_time[i] - burst_time[i]);
                  wait_time = wait_time + total - arrival_time[i] - burst_time[i]; 
                  turnaround_time = turnaround_time + total - arrival_time[i]; 
                  counter = 0; 
            } 
            if(i == limit - 1) 
            {
                  i = 0; 
            }
            else if(arrival_time[i + 1] <= total) 
            {
                  i++;
            }
            else 
            {
                  i = 0;
            }
      } 
      average_wait_time = wait_time * 1.0 / limit;
      average_turnaround_time = turnaround_time * 1.0 / limit;
      printf("\n\nAverage Waiting Time:\t%f", average_wait_time); 
      printf("\nAvg Turnaround Time:\t%f\n", average_turnaround_time); 
      return 0; 
}

Output

Round Robin Scheduling Program in C using Arrival Time and Array

If you have any doubts about the implementation of the round robin scheduling program in C language, let us know about it in the comment section. Find more about it on Wikipedia.

14 thoughts on “Round Robin Scheduling Program in C

  1. Chaitrali Naik

    This is the best code for Round Robin Algorithm that I found. Thank you so much!

    Reply
  2. Vedant Mishra

    I want to display the Average Turnaround and Waiting with only 2 digits after decimal. How to do that?

    Reply
  3. Pankaj Dhende

    Most of the Time Sharing systems use Time Slice or Round Robin CPU Scheduling algorithm.

    Reply
  4. Vineeth Daniel

    The round robin disk scheduling algorithm is widely used in Network switches, routers, multiplexers and other such networking devices.

    Reply
  5. GurinDer Batth

    PROCESSES BT AT
    P1 29 0
    P2 14 6
    P3 10 8
    P4 8 1
    P5 6 13
    PLZ HELP ME I THINK THAT CODE GIVE ME WRONG OUTPUT ..M PLZ ANYONE SHOW ME GHAINT CHART OF THIS PROBLEM THANKS IN ADVANCE…

    Reply
  6. Fullmetal

    Hello! I am trying to code this with response time and elapsed time of the process. How to find out elapsed time and response time by making additions to this code?

    Reply
  7. Meg

    Please send the opengl source code for round robin for computer graphics

    Reply
  8. Sumit

    This code used here is wrong implementation…. it just give the first process correct all other outputs are wrong.

    Reply
  9. Akash Gade

    Is there a way to create a non pre-emptive version of round robin program?

    Reply

Let's Discuss