C Program to Simulate Round Robin CPU Scheduling Algorithm (With Detailed Comments - Simplest Method)

In this article, we are going to simulate the Round Robin CPU Scheduling Algorithm using the C Programming Language.
Simulating Round Robin seems more like a pretty complicated program. But the logic is simple once you understand the way each calculation goes. It is easier when you try to understand each line of code and why it is calculated like that. We have seen many codes online for this Round Robin but we were not satisfied with any of those. Especially because those were good codes but didn't contain enough information about the methods used. So, in this article, we are trying to make you understand the logic behind the Round Robin Algorithm and how to simulate it using C programming language. The code might seem complicated. But we have commented literally every single line of code in order to make it extremely easy to understand for everyone.
So, carefully read each line of comment that we gave in the code to understand everything.
The logic is actually pretty simple. The difference comes only when calculating the Waiting time.
So unlike our previous posts in which the other scheduling algorithms were simulated, we are going with a different approach. We are going to use Functions in this to make things easier for you. We have defined functions to calculate the turnaround time and waiting time. Pay closer attention to the function waitingtime(); because that's where most of the important steps are.
For the sake of making it simpler and easier to understand, we are assuming the Arrival time of all processes as 0 in this program.


Open in OnlineGDB

//C Program to Simulate Round Robin CPU Scheduling by Nived Kannada
//We are defining functions to calculate Turnaroundtime and waiting time.
//Function to find Turnaround time
int turnarroundtime(int p[], int n,
int bt[], int wt[], int tat[]) {
    int i;
   for (i = 0; i < n ; i++)
   tat[i] = bt[i] + wt[i];
   return 1;
//Function to calculate waiting time
int waitingtime(int p[], int n,
int bt[], int wt[], int quantum) 
   int rem_bt[n];  //rem_bt is remaining burst time for each process(stored as array). It is initially set as equal to bt[].
   //Copying bt[] to rem_bt[]
   for (int i = 0 ; i < n ; i++)
   rem_bt[i] = bt[i];
   int t = 0; //Setting initial time as 0
   //Traversing processes in Round Robin manner until all of them are not done.
   while (1) {
      int finished = 1;
      for (int i = 0 ; i < n; i++) //Traversing through each process one by one repeatedly.
         //We have to process only if remaining burst time of a process is greater than zero.
         if (rem_bt[i] > 0) //Checking if remaining burst time of current process is greater than zero.
            finished = 0; // rem_bt[i]>0 means the current process is not finished yet.
            if (rem_bt[i] > quantum) {
               t = t+quantum;    //Adding time_quantum to current time t.
               rem_bt[i] = rem_bt[i] - quantum; //Subtracting time_quantum from remaining burst time.
            else    // ie. if remaining burst time is less than time_quantum
               t = t + rem_bt[i];   //adding the remaining burst time with current time t.  
               wt[i] = t - bt[i];   // Waiting time = current time - burst time of current process.
               //in this case, rem_bt[i] is less than the time_quantum. 
               //ie. process will be finished in this cycle. So we can set rem_bt[i] as 0.
               rem_bt[i] = 0;   
      //If all processes are finished
      if (finished == 1)
       {  break; }
   return 1;

//Main Function
void main()
    int n, bt[20], at[20],p[20],wt[20],tat[20] ,i, total=0, time_quantum; //Here a new variable is introduced --> time_quantum
    int x;
    float avgwt=0, avgtat=0;
    //Reading number of processes from input.
    printf("Enter number of processes: ");
    //Reading Burst times of all processes. (( ASSUMING ARRIVAL TIME IS 0 FOR ALL PROCESSES. ))
    printf("Enter the Burst time value of each process: \n");
        printf("Burst Time of P[%d]: ",i);
    //Reading the Time Quantum value
    printf("Enter the Time Quantum: ");
    //Calling the functions to calculate waiting time and turnaround time
    waitingtime(p, n, bt, wt, time_quantum);
    turnarroundtime(p, n, bt, wt, tat);
    //Displaying Table
    //Calculating Average Waiting time and Average Turnaround Time
        avgwt = avgwt + wt[i];
        avgtat = avgtat + tat[i];
    avgwt = avgwt/n;
    avgtat = avgtat/n;
    //Displaying Average WT and Average TAT 
    printf("\nAverage WT = %f\n", avgwt);
    printf("\nAverage TAT = %f\n", avgtat);




  1. Hi, can i know how to translate it to flowchart ? Im sorry, im newb. Thank you.

  2. Converting it to flowchart is not very difficult but it will take a lot of time because this is a very large program. Also, since it uses multiple functions in the program, you will have to draw more than one flowchart for each function as well. You need to know the basics of drawing a flowchart in order to do that.



Nanogalaxy is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for website owners to earn advertising fees by advertising and linking to amazon.com and any other website that may be affiliated with Amazon Service LLC Associates Program.