Operating systems use different scheduling algorithms to schedule the execution of processes by the CPU. Some scheduling algorithms are as follows:
First-come, first-served (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 first-come, first-served scheduling algorithm.
This algorithm is like a first-in, first-out (FIFO) algorithm. As the name suggests, the process or request which arrives first gets executed first. In case of a tie, if two processes request CPU simultaneously, the process with a smaller process ID gets the CPU allocation first. This is the simplest and easiest-to-implement CPU scheduling algorithm.
Note: This algorithm is non-preemptive.
Suppose we have a set of five processes whose
Process | Burst Time | Arrival Time |
P1 | 6 | 2 |
P2 | 2 | 5 |
P3 | 8 | 1 |
P4 | 3 | 0 |
P5 | 4 | 4 |
Lets's handle this process scheduling using FCFS. To understand it better, look at the figure below:
Let's look at the Gantt chart o
Let's calculate the waiting time using the above Gantt chart:
Waiting time = Start time - Arrival time
The code for the implementation of the FCFS algorithm in C++ is given below:
#include<iostream>using namespace std;// Function to find the waiting timevoid findWait_Time(int processes[], int size, int burst_time[],int wait_t[], int arrival_t[]){int service_time[size];service_time[0] = arrival_t[0];wait_t[0] = 0;int i = 1;// calculating waiting timewhile ( i < size ){// Add burst time of previous processesservice_time[i] = service_time[i-1] + burst_time[i-1];//waiting time = sum - at[i]wait_t[i] = service_time[i] - arrival_t[i];if (wait_t[i] < 0)wait_t[i] = 0;i++;}}// Function to calculate average waiting timevoid findavg_wait_Time(int processes[], int size, int burst_t[], int arrival_t[]){int wt[size], tat[size];findWait_Time(processes, size, burst_t, wt, arrival_t);// Display processes along with all detailscout << "Processes " << " Burst Time " << " Arrival Time "<< " Waiting Time \n";int total_wt = 0, total_tat = 0;int i = 0;while (i < size){total_wt = total_wt + wt[i];total_tat = total_tat + tat[i];int compl_time = tat[i] + arrival_t[i];cout << " " << i+1 << "\t\t" << burst_t[i] << "\t\t"<< arrival_t[i] << "\t\t" << wt[i] << "\t\t "<< endl;i++;}cout << "Average waiting time = "<< (float)total_wt / (float)size;}// Driver codeint main(){// Process numbersint processes[] = {4, 3, 1, 5, 2};int n = sizeof processes / sizeof processes[0];// Arrival time of all processesint arrival_time[] = {0, 1, 2, 4, 5};// Burst time of all processesint burst_time[] = {3, 8, 6, 4, 2};findavg_wait_Time(processes, n, burst_time, arrival_time);return 0;}
Execution order: The program finds the waiting time first for all processes using the given burst and arrival time, and once it's done, it computes the average waiting time by (float)total_wt / (float)size
and displays details along processes.
Line 23–24: If the waiting time for any process is negative, that means it's already in the queue, so before the CPU becomes idle, its waiting time becomes 0, wt[i] = 0
.
The advantages of the FCFS algorithm are the following:
This algorithm is simple to implement and easy to understand.
It uses a very simple data structure queue for maintaining processes.
FCFS does not lead the process to
The disadvantages of the FCFS algorithm are the following:
As it is a non-preemptive algorithm, it doesn't consider any process's priority or burst time.
This algorithm mostly suffers from the
The average waiting time for processes is not optimal.
Parallel utilization of resources for processes is not possible.
Free Resources