Menu
Round-robin scheduling algorithm is one of the simplest scheduling algorithms. It is designed especially for time-sharing systems. The ready queue is treated as a circular queue. The algorithm assigns a time slice (also called time quantum) to each process in the ready queue in order, handling all processes without priority. A maximum of one time slice is allocated at once.
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
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
Average Normalized Turnaround Time = 3.10
Average Waiting Time = 10
Round Robin Scheduling Program in C
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 | { intcount,j,n,time,remain,flag=0,time_quantum; intwait_time=0,turnaround_time=0,at[10],bt[10],rt[10]; scanf('%d',&n); for(count=0;count<n;count++) printf('Enter Arrival Time and Burst Time for Process Process Number %d :',count+1); scanf('%d',&bt[count]); } scanf('%d',&time_quantum); printf('nnProcesst|Turnaround Time|Waiting Timenn'); { { rt[count]=0; } { time+=time_quantum; if(rt[count]0&&flag1) remain--; printf('P[%d]t|t%dt|t%dn',count+1,time-at[count],time-at[count]-bt[count]); turnaround_time+=time-at[count]; } count=0; count++; count=0; printf('nAverage Waiting Time= %fn',wait_time*1.0/n); printf('Avg Turnaround Time = %f',turnaround_time*1.0/n); return0; |
Output
If you found anything incorrect or have any doubts regarding above round robin scheduling program in C then comment below.
You May Also Like:
A Process Scheduler schedules different processes to be assigned to the CPU based on particular scheduling algorithms. There are six popular process scheduling algorithms which we are going to discuss in this chapter −
- First-Come, First-Served (FCFS) Scheduling
- Shortest-Job-Next (SJN) Scheduling
- Priority Scheduling
- Shortest Remaining Time
- Round Robin(RR) Scheduling
- Multiple-Level Queues Scheduling
These algorithms are either non-preemptive or preemptive. Non-preemptivealgorithms are designed so that once a process enters the running state, it cannot be preempted until it completes its allotted time, whereas the preemptive scheduling is based on priority where a scheduler may preempt a low priority running process anytime when a high priority process enters into a ready state.
First Come First Serve (FCFS)
- Jobs are executed on first come, first serve basis.
- It is a non-preemptive, pre-emptive scheduling algorithm.
- Easy to understand and implement.
- Its implementation is based on FIFO queue.
- Poor in performance as average wait time is high.
Wait time of each process is as follows −
Process | Wait Time : Service Time - Arrival Time |
---|---|
P0 | 0 - 0 = 0 |
P1 | 5 - 1 = 4 |
P2 | 8 - 2 = 6 |
P3 | 16 - 3 = 13 |
Average Wait Time: (0+4+6+13) / 4 = 5.75
Shortest Job Next (SJN)
- This is also known as shortest job first, or SJF
- This is a non-preemptive, pre-emptive scheduling algorithm.
- Best approach to minimize waiting time.
- Easy to implement in Batch systems where required CPU time is known in advance.
- Impossible to implement in interactive systems where required CPU time is not known.
- The processer should know in advance how much time process will take.
Given: Table of processes, and their Arrival time, Execution time
Process | Arrival Time | Execution Time | Service Time |
---|---|---|---|
P0 | 0 | 5 | 0 |
P1 | 1 | 3 | 5 |
P2 | 2 | 8 | 14 |
P3 | 3 | 6 | 8 |
Waiting time of each process is as follows −
Process | Waiting Time |
---|---|
P0 | 0 - 0 = 0 |
P1 | 5 - 1 = 4 |
P2 | 14 - 2 = 12 |
P3 | 8 - 3 = 5 |
Average Wait Time: (0 + 4 + 12 + 5)/4 = 21 / 4 = 5.25
Priority Based Scheduling
- Priority scheduling is a non-preemptive algorithm and one of the most common scheduling algorithms in batch systems.
- Each process is assigned a priority. Process with highest priority is to be executed first and so on.
- Processes with same priority are executed on first come first served basis.
- Priority can be decided based on memory requirements, time requirements or any other resource requirement.
Given: Table of processes, and their Arrival time, Execution time, and priority. Here we are considering 1 is the lowest priority.
Process | Arrival Time | Execution Time | Priority | Service Time |
---|---|---|---|---|
P0 | 0 | 5 | 1 | 0 |
P1 | 1 | 3 | 2 | 11 |
P2 | 2 | 8 | 1 | 14 |
P3 | 3 | 6 | 3 | 5 |
Waiting time of each process is as follows −
Process | Waiting Time |
---|---|
P0 | 0 - 0 = 0 |
P1 | 11 - 1 = 10 |
P2 | 14 - 2 = 12 |
P3 | 5 - 3 = 2 |
Average Wait Time: (0 + 10 + 12 + 2)/4 = 24 / 4 = 6
Shortest Remaining Time
- Shortest remaining time (SRT) is the preemptive version of the SJN algorithm.
- The processor is allocated to the job closest to completion but it can be preempted by a newer ready job with shorter time to completion.
- Impossible to implement in interactive systems where required CPU time is not known.
- It is often used in batch environments where short jobs need to give preference.
Round Robin Scheduling
- Round Robin is the preemptive process scheduling algorithm.
- Each process is provided a fix time to execute, it is called a quantum.
- Once a process is executed for a given time period, it is preempted and other process executes for a given time period.
- Context switching is used to save states of preempted processes.
Wait time of each process is as follows −
Process | Wait Time : Service Time - Arrival Time |
---|---|
P0 | (0 - 0) + (12 - 3) = 9 |
P1 | (3 - 1) = 2 |
P2 | (6 - 2) + (14 - 9) + (20 - 17) = 12 |
P3 | (9 - 3) + (17 - 12) = 11 |
Average Wait Time: (9+2+12+11) / 4 = 8.5
Multiple-Level Queues Scheduling
Multiple-level queues are not an independent scheduling algorithm. They make use of other existing algorithms to group and schedule jobs with common characteristics.
- Multiple queues are maintained for processes with common characteristics.
- Each queue can have its own scheduling algorithms.
- Priorities are assigned to each queue.
For example, CPU-bound jobs can be scheduled in one queue and all I/O-bound jobs in another queue. The Process Scheduler then alternately selects jobs from each queue and assigns them to the CPU based on the algorithm assigned to the queue.