First Come, First Serve – CPU Scheduling | (Non-preemptive)

26 Apr, 2022
FCFS Scheduling:
Simplest CPU scheduling algorithm that schedules according to arrival times of processes. First come first serve scheduling algorithm states that the process that requests the CPU first is allocated the CPU first. It is implemented by using the FIFO queue. When a process enters the ready queue, its PCB is linked onto the tail of the queue. When the CPU is free, it is allocated to the process at the head of the queue. The running process is then removed from the queue. FCFS is a non-preemptive scheduling algorithm.
Characteristics of FCFS:
FCFS supports non-preemptive and preemptive CPU scheduling algorithm.
Tasks are always executed on a First-come, First-serve concept.
FCFS is easy to implement and use.
This algorithm is not much efficient in performance, and the wait time is quite high.
The waiting time for first process is 0 as it is executed first.
The waiting time for upcoming process can be calculated by:  
wt[i] =  ( at[i – 1] + bt[i – 1] + wt[i – 1] ) – at[i]
where 
wt[i] = waiting time of current process 
at[i-1] = arrival time of previous process 
bt[i-1] = burst time of previous process 
wt[i-1] = waiting time of previous process 
at[i] = arrival time of current process 
The Average waiting time can be calculated by:  
Average Waiting Time = (sum of all waiting time)/(Number of processes)
Example-1: Consider the following table of arrival time and burst time for five processes P1, P2, P3, P4 and P5. 
The First come First serve CPU Scheduling Algorithm will work on the basis of steps as mentioned below:
Step 0: At time = 0,
The process begins with P1 
As it has arrival time 0
Step 1: At time = 1,
The process P2 arrives
But process P1 still executing, 
Thus, P2 kept in a waiting table and wait for its execution.
Step 3: At time = 2,
The process P3 arrives and kept in a waiting queue 
While process P1 is still executing as its burst time is 4.
Step 4: At time = 3,
The process P4 arrives and kept in the waiting queue 
While process P1 is still executing as its burst time is 4
Step 5: At time = 4,
The process P1 completes its execution 
Process P5 arrives in waiting queue while process P2 starts executing
Step 6: At time = 5,
The process P2 completes its execution 
Step 7: At time = 7,
Process P3 starts executing, it has burst time of 1 thus, it completes execution at time interval 8
Step 8: At time 8,
The process of P3 completes its execution 
Process P4 starts executing, it has burst time of 2 thus, it completes execution at time interval 10.
Step 9: At time 10,
The process P4 completes its execution 
Process P5 starts executing, it has burst time of 5 thus, it completes execution at time interval 15.
Step 10: At time 15,
Process P5 will finish its execution.
The overall execution of the processes will be as shown below:
Gantt chart for above execution:
Gantt chart for First come First serve Scheduling
Waiting Time = Start time – Arrival time
P1 = 0 – 0 = 0P2 = 4 – 1 = 3P3 = 7 – 2 = 5P4 = 8 – 3 = 5P5 = 10 – 4 = 6
Average waiting time = 0 + 3 + 5 + 5+ 6 / 5 = 19 / 5 = 3.8 
Program for First Come First Serve algorithm:
C++
Java
Python3
C#
// C++ program to Calculate Waiting// Time for given Processes#include <iostream>using namespace std; // Function to Calculate waiting time// and average waiting timevoid CalculateWaitingTime(int at[],                          int bt[], int N){     // Declare the array for waiting    // time    int wt[N];     // Waiting time for first process    // is 0    wt[0] = 0;     // Print waiting time process 1    cout << "PN\t\tAT\t\t"         << "BT\t\tWT\n\n";    cout << "1"         << "\t\t" << at[0] << "\t\t"         << bt[0] << "\t\t" << wt[0] << endl;     // Calculating waiting time for    // each process from the given    // formula    for (int i = 1; i < 5; i++) {        wt[i] = (at[i - 1] + bt[i - 1]                 + wt[i - 1]) - at[i];         // Print the waiting time for        // each process        cout << i + 1 << "\t\t" << at[i]             << "\t\t" << bt[i] << "\t\t"             << wt[i] << endl;    }     // Declare variable to calculate    // average    float average;    float sum = 0;     // Loop to calculate sum of all    // waiting time    for (int i = 0; i < 5; i++) {        sum = sum + wt[i];    }     // Find average waiting time    // by dividing it by no. of process    average = sum / 5;     // Print Average Waiting Time    cout << "\nAverage waiting time = "         << average;} // Driver codeint main(){    // Number of process    int N = 5;     // Array for Arrival time    int at[] = { 0, 1, 2, 3, 4 };     // Array for Burst Time    int bt[] = { 4, 3, 1, 2, 5 };     // Function call to find    // waiting time    CalculateWaitingTime(at, bt, N);    return 0;}
// Java program to Calculate Waiting// Time for given Processesclass GFG{  // Function to Calculate waiting time// and average waiting timestatic void CalculateWaitingTime(int at[],                          int bt[], int N){      // Declare the array for waiting    // time    int []wt = new int[N];      // Waiting time for first process    // is 0    wt[0] = 0;      // Print waiting time process 1    System.out.print("P.No.\tArrival Time\t"        + "Burst Time\tWaiting Time\n");    System.out.print("1"        + "\t\t" +  at[0]+ "\t\t"         + bt[0]+ "\t\t" +  wt[0] +"\n");      // Calculating waiting time for    // each process from the given    // formula    for (int i = 1; i < 5; i++) {        wt[i] = (at[i - 1] + bt[i - 1] + wt[i - 1]) - at[i];          // Print the waiting time for        // each process        System.out.print(i + 1+ "\t\t" +  at[i]            + "\t\t" +  bt[i]+ "\t\t"             + wt[i] +"\n");    }      // Declare variable to calculate    // average    float average;    float sum = 0;      // Loop to calculate sum of all    // waiting time    for (int i = 0; i < 5; i++) {        sum = sum + wt[i];    }      // Find average waiting time    // by dividing it by no. of process    average = sum / 5;      // Print Average Waiting Time    System.out.print("Average waiting time = "         + average);}  // Driver codepublic static void main(String[] args){    // Number of process    int N = 5;      // Array for Arrival time    int at[] = { 0, 1, 2, 3, 4 };      // Array for Burst Time    int bt[] = { 4, 3, 1, 2, 5 };      // Function call to find    // waiting time    CalculateWaitingTime(at, bt, N);}} // This code is contributed by 29AjayKumar
# Python3 program to Calculate Waiting# Time for given Processes # Function to Calculate waiting time# and average waiting timedef CalculateWaitingTime(at, bt, N):     # Declare the array for waiting    # time    wt = [0]*N;     # Waiting time for first process    # is 0    wt[0] = 0;     # Print waiting time process 1    print("P.No.\tArrival Time\t" , "Burst Time\tWaiting Time");    print("1" , "\t\t" , at[0] , "\t\t" , bt[0] , "\t\t" , wt[0]);     # Calculating waiting time for    # each process from the given    # formula    for i in range(1,5):        wt[i] = (at[i - 1] + bt[i - 1] + wt[i - 1]) - at[i];         # Print the waiting time for        # each process        print(i + 1 , "\t\t" , at[i] , "\t\t" , bt[i] , "\t\t" , wt[i]);          # Declare variable to calculate    # average    average = 0.0;    sum = 0;     # Loop to calculate sum of all    # waiting time    for i in range(5):        sum = sum + wt[i];         # Find average waiting time    # by dividing it by no. of process    average = sum / 5;     # Print Average Waiting Time    print("Average waiting time = " , average);  # Driver codeif __name__ == '__main__':    # Number of process    N = 5;     # Array for Arrival time    at = [ 0, 1, 2, 3, 4 ];     # Array for Burst Time    bt = [ 4, 3, 1, 2, 5 ];     # Function call to find    # waiting time    CalculateWaitingTime(at, bt, N); # This code is contributed by 29AjayKumar
// C# program to Calculate Waiting// Time for given Processesusing System; class GFG{   // Function to Calculate waiting time// and average waiting timestatic void CalculateWaitingTime(int []at,                          int []bt, int N){       // Declare the array for waiting    // time    int []wt = new int[N];       // Waiting time for first process    // is 0    wt[0] = 0;       // Print waiting time process 1    Console.Write("P.No.\tArrival Time\t"        + "Burst Time\tWaiting Time\n");    Console.Write("1"        + "\t\t" +  at[0]+ "\t\t"         + bt[0]+ "\t\t" +  wt[0] +"\n");       // Calculating waiting time for    // each process from the given    // formula    for (int i = 1; i < 5; i++) {        wt[i] = (at[i - 1] + bt[i - 1] + wt[i - 1]) - at[i];           // Print the waiting time for        // each process        Console.Write(i + 1+ "\t\t" +  at[i]            + "\t\t" +  bt[i]+ "\t\t"             + wt[i] +"\n");    }       // Declare variable to calculate    // average    float average;    float sum = 0;       // Loop to calculate sum of all    // waiting time    for (int i = 0; i < 5; i++) {        sum = sum + wt[i];    }       // Find average waiting time    // by dividing it by no. of process    average = sum / 5;       // Print Average Waiting Time    Console.Write("Average waiting time = "         + average);}   // Driver codepublic static void Main(String[] args){    // Number of process    int N = 5;       // Array for Arrival time    int []at = { 0, 1, 2, 3, 4 };       // Array for Burst Time    int []bt = { 4, 3, 1, 2, 5 };       // Function call to find    // waiting time    CalculateWaitingTime(at, bt, N);}} // This code is contributed by 29AjayKumar
PN        AT        BT        WT

1        0        4        0
2        1        3        3
3        2        1        5
4        3        2        5
5        4        5        6

Average waiting time = 3.8
Complexity Analysis:
Time Complexity: O(N)
Auxiliary Space: O(N)
Advantages of FCFS:
The simplest and basic form of CPU Scheduling algorithm
Easy to implement
First come first serve method
Disadvantages of FCFS:
As it is a Non-preemptive CPU Scheduling Algorithm, hence it will run till it finishes the execution.
Average waiting time in the FCFS is much higher than the others
Not very efficient due to its simplicity
Processes which are at the end of the queue, have to wait longer to finish.
29AjayKumar
ldsouza2504
pankajsharmagfg
simmytarika5
surinderdawra388
kashishkumar2
Algorithms
Operating Systems
Operating Systems
Algorithms
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
DSA Sheet by Love Babbar
SDE SHEET - A Complete Guide for SDE Preparation
Top 50 Array Coding Problems for Interviews
Difference between BFS and DFS
Recursion
Cache Memory in Computer Organization
LRU Cache Implementation
'crontab' in Linux with Examples
Cd cmd command
Memory Management in Operating System