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
If you found anything incorrect or have any doubts regarding above round robin scheduling program in C then comment below.
how about if there is an idle time? what to do?
Then we should take at and bt into an matrix then we should sort according to our at along with bt.
Dude you saved my hours… Thank you so much!
hi..loved ur post man…call me.. wanna discuss future..
ok thank you bro
what is the difference if we don’t use flag ????
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 .
bhari help mila h bhai
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]
What time quantum have u taken?
I had give input but average turn around time given as an o/P is 0.0000 why so
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.
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?
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]
above gives correct output for your inputs
thanks Neeraj ! you saved my time 😉
I want hellllllp to do the project 🙁
its does not work for all problems..
it works miss aapko use krna nhi aaya hga
She is right. It doesn’t work for all cases. Try implementing with different test cases.
Do you have hrrn and mlfq scheduling program in C ?Thanks in advance
You saved my graduation’s project
LOL:)
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
the pgm is wrong i found some mistakes
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!
No it returns output correctlt
great bro.. learning OS in this semester 😛
Greaty!
Can you explain the program for me pls?
not
what not?
the is not giving correct output for some testcases.
How to implement of Google code in c language
it have errors i.e the output is not correct because of wrong algorithm
thats the reason i love india u r the best in the earth and u will rech your goal
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
yes i doesnt work in all cases but u can implement more advanced but the solution is here … 🙂
pura code galat hai kya bhai kuch bhi kr re ho
if(rt[count]0) why this condition use please explain that condition
can you post multicore scheduler c program for Earliest Deadline First scheduler
sir, we need algorithm and flow chart FCFS,SJF,Round Robin and other scheduling algorithm…. can you please send me the algorithm and flow charts … My mail address: manishkkathane@gmail.com
Thank you for this program I ask about program for how to keep trak in banking balance in multi threading
need an urgent question:
how to implement multilevel Round Robin feedback queue scheduling program in c
what is the burst time here…
burst time means service time. in other words burst time is the time taken to complete the execution of the job.
very beautiful,sundar
helpfull
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…..
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
why you increase and again writing count=0
where is the condition for Time is checking the process is arrived or not
i.e it will not work when the arrival time of first job is 0.
anyone can contact me on coolamanjain2044@gmail.com
How does the loop “for(time = 0;count = 0;rem != 0)” work?
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.
hey
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???
thanks dada
can u explain every line code there.. u can simply email it to me at intanzulaikha1794@gmail.com
tq
for loop doesn’t have any variable to increment
why we have used flag and remain as variable
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 …
yes , have you found any solution for that
im trying adding && arrival[i]=total_time
dont if that works
can you help
what does bt, at, rt, means
and why we are using remain and why the value of flag is given as flag=0:
It is excuting for only one process
I’m gonna borrow your program for my homework i hope if you don´t mind,Thise is for respect for you programm.
Cesar
How does work the code of three level queue Round Rabin algorithm??
can you provide GANTT CHART for this example
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)
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………..????
ur answer was totally wrong .p2 tat is 18 ,p3 tat is 13 and p4 tat is 17
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
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
Run kiye baad pura arrival time burst time Dala quantum time dala fir enter kiya…to run nhi hora direct program dikhara🙄🙄🙄
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
Thanks you so so so much.have a good day
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
Your program is not giving the correct output for different arrival time..
Complexity of this code
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 .
Is it possible to
write for loop in the format mentioned above?
Thank u for this… I love u
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
an simple algorithm for RRScheduling algorithm
Pls give me this programing in c++ language
Ali Pls give me this programing in c++ language
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
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.