Operating Systems use various scheduling algorithms to schedule the execution of different processes on the CPU. The following are some scheduling algorithms:
First Come First Serve (FCFS) scheduling
Shortest job next (SJN) scheduling
Priority scheduling
Shortest remaining time
Round Robin (RR) scheduling
Multiple-level queues scheduling
Note: Algorithms are either
or preemptive 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. . non-preemptive Non-preemptive algorithms are designed so that once a process enters the running state, it cannot be preempted until it completes its allotted time.
In this answer, we'll discuss the RR scheduling algorithm.
In Round Robin scheduling, every process is assigned a time quantum to use CPU resources for its execution in a cyclic way. If we modify the FCFS algorithm to make it preemptive, it will work like Round Robin scheduling.
This scheduling algorithm focus on the time-sharing technique for the utilization of CPU resources and that unit of time is called
Every process in the
Suppose we have a set of three processes whose
Process Queue | Burst Time |
P1 | 4 |
P2 | 3 |
P3 | 5 |
Let's handle this process scheduling using round-robin scheduling. To understand it better, have a look at the figure below:
Have a look at the
Let's calculate the waiting time using the above Gantt chart:
Wait Time : Total of (Service Time - Arrival Time)
To deduce the average waiting time, we calculate the waiting time for each process and divide it by the total number of processes.
// C++ program for Round-Robin scheduling#include<iostream>using namespace std;// Function to find the waiting timevoid findWaitingTime(int processes[], int size,int bursttime[], int waittime[], int quantum){int rem_bt[size]; // To store remaining burst timesint i = 0;while(i < size){rem_bt[i] = bursttime[i];i++;}int t = 0; // Current time// Keep traversing processes in round robin manner untill complete executionwhile (1){bool done = true;int i = 0;// Traverse all processes in a cyclewhile( i < size){// Only execute a process with burst time > 0if (rem_bt[i] > 0){done = false; // A pending processif (rem_bt[i] > quantum){//Process execution so fart += quantum;// Decrement burst time of process with respect to quantumrem_bt[i] -= quantum;}// Last cycle for this processelse{//Process execution for unit timet = t + rem_bt[i];//Wait time formulawaittime[i] = t - bursttime[i];//After complete execution of process make it burst time 0rem_bt[i] = 0;}}i++;}// If all processes are doneif (done == true)break;}}// Function to calculate average timevoid findavgTime(int processes[], int size, int bursttime[],int quantum){int waittime[size], total_wt = 0;// Function to find waiting timefindWaitingTime(processes, size, bursttime, waittime, quantum);// Display processes detailscout << "ProcessNo "<< "BurstTime"<< " WaitTime";// Calculate total waiting timeint i = 0;cout <<endl;while(i<size){total_wt = total_wt + waittime[i];cout << " " << i+1 << "\t\t" << bursttime[i] <<"\t "<< waittime[i] <<"\t\t "<<endl;i++;}cout << "Average waiting time = "<< (float)total_wt / (float)size;}// Driver codeint main(){// process id'sint processes[] = { 1, 2, 3};int n = sizeof processes / sizeof processes[0];// Burst time of all processesint burst_time[] = {4, 3, 5};// Time quantumint quantum = 2;findavgTime(processes, n, burst_time, quantum);return 0;}
The advantages of the RR algorithm are as follows:
All process in the queue gets an equal share of CPU resources, unlike FCFS.
Since round-robin uses time-sharing, it gives each process a time slot.
Every process gets a chance to re-use the CPU after a fixed time quantum.
It's easy to implement and is also
The disadvantages of the RR algorithm are as follows:
The waiting time and response time are relatively more extensive.
The
There is an overhead of context switching.
If quantum time is less for scheduling, it gives too big a Gantt chart.
Free Resources