Round Robin Scheduling Program in C

In this tutorial you will learn about round robin scheduling program in C.

Process scheduling is an important component for process management. In a multi-user and a time-sharing system, response time is one of the most important objective to be accomplished.

There are many scheduling algorithms in C for process management such as:

1. First Come First Serve
2. Shortest Job First
3. Priority Scheduling
4. Round Robin Scheduling

However, this tutorial will get you clear with understanding of Round Robin Scheduling program in C.

Round Robin Scheduling Algorithm

1. The queue structure in ready queue is of First In First Out (FIFO) type.

2. A fixed time is allotted to every process that arrives in the queue. This fixed time is known as time slice or time quantum.

3. The first process that arrives is selected and sent to the processor for execution. If it is not able to complete its execution within the time quantum provided, then an interrupt is generated using an automated timer.

4. The process is then stopped and is sent back at the end of the queue. However, the state is saved and context is thereby stored in memory. This helps the process to resume from the point where it was interrupted.

5. The scheduler selects another process from the ready queue and dispatches it to the processor for its execution. It is executed until the time Quantum does not exceed.

6. The same steps are repeated until all the process are finished.

The round robin algorithm is simple and the overhead in decision making is very low. It is the best scheduling algorithm for achieving better and evenly distributed response time.

Example

Lets take one example to understand it.

Time Quantum = 2

Process Arrival Time Burst Time
P1 0 9
P2 1 5
P3 2 3
P4 3 4

 

Process Arrival Time Burst Time (x) Turnaround Time(t) Normalized Turnaround Time(t/x) Waiting Time
P1 0 9 21 2.34 12
P2 1 5 17 3.4 12
P3 2 3 11 3.67 8
P4 3 4 12 3 8

Average Turnaround Time = 15.25
Average Normalized Turnaround Time = 3.10
Average Waiting Time = 10

Round Robin Scheduling Program in C

#include<stdio.h> 

int main() 
{ 

  int count,j,n,time,remain,flag=0,time_quantum; 
  int wait_time=0,turnaround_time=0,at[10],bt[10],rt[10]; 
  printf("Enter Total Process:\t "); 
  scanf("%d",&n); 
  remain=n; 
  for(count=0;count<n;count++) 
  { 
    printf("Enter Arrival Time and Burst Time for Process Process Number %d :",count+1); 
    scanf("%d",&at[count]); 
    scanf("%d",&bt[count]); 
    rt[count]=bt[count]; 
  } 
  printf("Enter Time Quantum:\t"); 
  scanf("%d",&time_quantum); 
  printf("\n\nProcess\t|Turnaround Time|Waiting Time\n\n"); 
  for(time=0,count=0;remain!=0;) 
  { 
    if(rt[count]<=time_quantum && rt[count]>0) 
    { 
      time+=rt[count]; 
      rt[count]=0; 
      flag=1; 
    } 
    else if(rt[count]>0) 
    { 
      rt[count]-=time_quantum; 
      time+=time_quantum; 
    } 
    if(rt[count]==0 && flag==1) 
    { 
      remain--; 
      printf("P[%d]\t|\t%d\t|\t%d\n",count+1,time-at[count],time-at[count]-bt[count]); 
      wait_time+=time-at[count]-bt[count]; 
      turnaround_time+=time-at[count]; 
      flag=0; 
    } 
    if(count==n-1) 
      count=0; 
    else if(at[count+1]<=time) 
      count++; 
    else 
      count=0; 
  } 
  printf("\nAverage Waiting Time= %f\n",wait_time*1.0/n); 
  printf("Avg Turnaround Time = %f",turnaround_time*1.0/n); 
  
  return 0; 
}

Output

Round Robin Scheduling Program in C

If you found anything incorrect or have any doubts regarding above round robin scheduling program in C then comment below.

87 thoughts on “Round Robin Scheduling Program in C”

    1. flag=0 for false and flag=1
      if we not using as given in the above program we will not get correct answer or we will get garbage value .

  1. Input Arrival time and BT shown blow. after compile with these value the output is incorrect.
    Please check and update this code.
    [P.No->1,2,3,4,5,6], [AT->0,1,2,3,4,6],[BT->4,5,2,1,6,3],[TAT->8,17,4,6,17,13],[WT->4,12,2,5,11,10]

    1. The above code assumes that all the processes are arriving at an exact interval of 1 sec each, the first process starting at t=0. In your data, the process P6 arrives at a time difference of 2 seconds from the last one.

      1. yup you are right as long as the interval is matched the code gives correct answers but changing arrival time gives incorrect answers. However thanks Neeraj Mishra for trying. Any idea where I can get the code for different arrival times?

  2. ur code is not suitable for all the problem…………….update ur code…………..

    [P.No->1,2,3,4,5,6], [AT->0,1,2,3,4,6],[BT->4,5,2,1,6,3],[TAT->8,17,4,6,17,13],[WT->4,12,2,5,11,10]

  3. vikramjeetsingh

    Enter Total Process: 6
    Enter Arrival Time and Burst Time for Process Process Number 1 :0
    4
    Enter Arrival Time and Burst Time for Process Process Number 2 :1
    5
    Enter Arrival Time and Burst Time for Process Process Number 3 :2
    2
    Enter Arrival Time and Burst Time for Process Process Number 4 :3
    1
    Enter Arrival Time and Burst Time for Process Process Number 5 :4
    6
    Enter Arrival Time and Burst Time for Process Process Number 6 :6
    3
    Enter Time Quantum: 2

    it is giving wrong answer for this

    1. if you really want to know the correct answer just solve it for once, and i checked the input given by you all the outcomes are true on the basis of this program!

  4. Hi everybody .
    I am industrial engineering student and I need a c++ code .
    thats greateful if someone help me .
    tnx a lot .
    please send me email to explain the problem .
    azerila_999@yahoo

  5. Thank you for this program I ask about program for how to keep trak in banking balance in multi threading

    1. burst time means service time. in other words burst time is the time taken to complete the execution of the job.

  6. Design a scheduling program that is capable of scheduling many processes that comes
    in at some time interval and are allocated the CPU not more that 10 time units. CPU must
    schedule processes having short execution time first. CPU is idle for 3 time units and does not
    entertain any process prior this time. Scheduler must maintain a queue that keeps the order of
    execution of all the processes. Compute average waiting and turnaround time.

    plz suggest me ho to solve this problem…..

  7. Anshuman Varshney

    Process Arrival Time Burst Time (x) Turnaround Time(t) Normalized Turnaround Time(t/x) Waiting Time
    P1 0 9 21 2.34 12
    P2 1 5 17 3.4 12
    P3 2 3 11 3.67 8
    P4 3 4 12 3 8

    P1 0 9 21 2.34 12
    P2 1 5 17 3.4 12
    P3 2 3 11 3.67 8
    P4 3 4 14 3 10

  8. Hi Guys..
    Can Anyone help me with this question???

    Implement polynomial equation using doubly linkedin list and perform following operations .
    1 . Add 2. Subtract 3. Multiply 4. Divide
    In C Language.

  9. what will happen if the arrival time of the first process is not 0 or the second shortest arrival time is way more than the time quanta???

  10. Your code is wrong
    when input is AT: 0, 3, 6 and BT: is 10,10,10 then order of execution according to your code will be

    1 2 3 ….
    It is actually 1 2 1 3 …

    1. yes , have you found any solution for that
      im trying adding && arrival[i]=total_time
      dont if that works
      can you help

  11. I’m gonna borrow your program for my homework i hope if you don´t mind,Thise is for respect for you programm.
    Cesar

  12. i want the program in C to implement round robin scheduling algorithm without arrival time (as we suppose arrival time is 0 for all processes)

  13. there is a major flaw in this code
    suppose if there 4 process arrival time is 0,11,11,12 respectively.
    and burst time is 9,5,3,4 respectively
    then after 2 iteration time becomes 9 and for the next process is coming at 11 time the whole loop get freeze………..????

  14. mitesh chopdekar

    Enter Total Process: 4
    Enter Arrival Time and Burst Time for Process Process Number 1 :0
    14
    Enter Arrival Time and Burst Time for Process Process Number 2 :2
    8
    Enter Arrival Time and Burst Time for Process Process Number 3 :4
    4
    Enter Arrival Time and Burst Time for Process Process Number 4 :6
    12
    Enter Time Quantum: 2
    P[3] | 10 | 10
    P[2] | 20 | 20
    P[1] | 28 | 28

    brother
    is this the way?
    not geting average of turn around time and wait timr
    what to do?
    help me asap
    i’m answering exam now
    i am about to fail
    hhhhheeeeellllllpppppp mmmmmeeeeeee

  15. Sir for the below input the program just stopped running.
    arrival time ={0,1,3,8,15}
    burst time = {3,1,4,6,5}
    Time quanta = 3

    1. Run kiye baad pura arrival time burst time Dala quantum time dala fir enter kiya…to run nhi hora direct program dikhara🙄🙄🙄

  16. This code is not correct
    test case process – 4
    at – 1 bt – 2
    at – 4 bt – 1
    at – 6 bt – 2
    at – 10 bt – 1

  17. The waiting time is coming negative in 1 case:
    if number of process = 1
    and arrival time != 0,

    then the waiting time = -(arrival time)

    Which is not correct

  18. Your code doesn’t care about the arrival time of process. I changed the arrival time of Process 1 to “100” and it still executes it at time -0 .

  19. what if there will be I/O events with in the process how one can implement it
    for example
    process 1 200{
    I/O 100 HD 1600
    I/O 110 SSD 25
    exit 400
    }

    process 2 480{
    I/O 100 HD 1600
    I/O 500 SSD 25
    exit 8000
    }

    where numbers after processes are their arrival times and numbers after exit are their burst time whereas the number in front of I/O means that after executing a total of 100 seconds after its arrival time it demands for I/O and numbers such as 1600 means that it needs to transfer that many bytes

  20. Enter Total Process: 3
    Enter Arrival Time and Burst Time for Process Process Number 1 :2 3
    Enter Arrival Time and Burst Time for Process Process Number 2 :4 3
    Enter Arrival Time and Burst Time for Process Process Number 3 :12 5
    Enter Time Quantum: 2

    Process |Turnaround Time|Waiting Time

    P[1] | 1 | -2

    i get this output

  21. Can someone help me with my problem?
    What if a job has a lot of process, what will the coding look like?
    For example, Job 1 with 20 process arrive at time 0 and Job 2 with 20 process arrive at time 20, with each job contain I/O burst time as well as CPU burst time.

Leave a Comment

Your email address will not be published. Required fields are marked *