First Come First Served Process Scheduling FCFS in operating systems

In this tutorial, we will learn about First Come First Served Scheduling (FCFS).

FCFS is a non-preemptive scheduling algorithm so processes priority does not matter.

Disadvantages of FCFS

  • FCFS is a Non-Preemptive CPU scheduling algorithm, so the winner process will not release the CPU and other resources by itself until it finishes its complete execution.
  • Not an ideal technique for time-sharing systems.
  • FCFS is not very efficient as compared to the other scheduling algorithms.
  • If the process first arrived is a big process with a high burst time, then other processes with less burst time need to wait.
  • The average wait time is high.

Advantages of FCFS

  • Easy to implement.
  • Easy to understand.
  • FCFS algorithm is the simplest than all other CPU scheduling algorithms.
  • Very Easy to program.
Process Burst Time Arrival
P1 4 2nd
P2 2 3rd
P3 8 1st
P4 3 4th

Gantt Chart of First Come, First Served (FCFS) Scheduling

FCFS Algo in os
Figure: FCFS.
Process Waiting Time
P1 8
P2 12
P3 0
P4 14

Average Wait Time: (0+8+12+14) / 4 = 8.5

How to calculate turn around time?

TurnAroundTime=BurstTime+WaitingTime.

First Come First Served (FCFS) Program in C++

#include<iostream> 
using namespace std;
int main()
{
    int n,BurstTime[50],WaitingTime[50],TurnAroundTime[50],AverageWaitingTime=0,AverageTurnAroundTime=0,i,j;
    cout<<"Enter total number of processes to demonstrate, Total processes can be from 0 to 50"<<endl;
    cin>>n;
    cout<<"\nEnter Burst Time for the process: \n";
    for(i=0;i<n;i++)
    {
        cout<<"Process["<<i+1<<"]:";
        cin>>BurstTime[i];
    }
    WaitingTime[0]=0;    
    //waiting time of first process is initiliazed with 0
    // waiting time for each process
    for(i=1;i<n;i++)
    {
        WaitingTime[i]=0;
        for(j=0;j<i;j++)
            WaitingTime[i]+=BurstTime[j];
    }
    cout<<"\nProcess\t\tBurst Time\tWaiting Time\tTurnaround Time";
    //turnaround time calculation for each process
    for(i=0;i<n;i++)
    {
        TurnAroundTime[i]=BurstTime[i]+WaitingTime[i]; 
        AverageWaitingTime+=WaitingTime[i];
        AverageTurnAroundTime+=TurnAroundTime[i];         
        cout<<"\nProcess["<<i+1<<"]"<<"\t\t"<<BurstTime[i]<<"\t\t"<<WaitingTime[i]<<"\t\t"<<TurnAroundTime[i];
    }
AverageWaitingTime/=i;
AverageTurnAroundTime/=i; 
    cout<<"\n\nAverageerage Waiting Time:"<<AverageWaitingTime;
    cout<<"\nAverageerage Turnaround Time:"<<AverageTurnAroundTime;
    return 0;
}

FCFS Process Scheduling Implementation in C++ by using Virtual Function

Let’s see the FCFS Process Scheduling Implementation in C++ by using Virtual Function.

#include<iostream>
using namespace std;

class display;
class process
{
protected:
int n,burst_time[15],wait_time[15],total_time[15],i,j;
public:
process()
{
n=0;
i=0;j=0;
}

virtual void processes()=0;

void input_burst_time()
{
cout<<"\nEnter burst Time\n";
for(i=0;i<n;i++)
{
cout<<"Process["<<i+1<<"]:";
cin>>burst_time[i];
}
}

};

class display : public process
{
public:

void processes()
{
cout<<"Enter No. of processes:";
cin>>n;
}

void cal_wait()
{
wait_time[0]=0;

for(i=1;i<n;i++)
{
wait_time[i]=0;
for(j=0;j<i;j++)
wait_time[i]+=burst_time[j];
}
}

void show()
{
for(i=0;i<n;i++)
{
total_time[i]=burst_time[i]+wait_time[i];

cout<<"\nP["<<"Process"<<"\t\t"<<"Burst Time"<<"\t\t"<<"Wait Time"<<"\t\t"<<"Total Time\n";
cout<<"\nP["<<i+1<<"]"<<"\t\t\t"<<burst_time[i]<<"\t\t\t"<<wait_time[i]<<"\t\t\t"<<total_time[i];
}
}

};
int main()
{

display obj;
obj.processes();
obj.input_burst_time();
obj.cal_wait();
obj.show();

return 0;
}

 

Output

FCFS Process Scheduling Implementation in C++ by using Virtual Function
Figure: FCFS Process Scheduling Implementation in C++ by using Virtual Function

FCFS Code using Inheritance of Classes

#include<iostream>
using namespace std;

class base_class // Base Class
{
protected: //To be accessible in derived class
int n,burst[10],wait[10],time[10],i,j;
public:
base_class()
{
n=0;
i=0;
j=0;
}

void input_burst()
{
cout<<"\nburst Time\n";
for(i=0;i<n;i++)
{
cout<<"Process["<<i+1<<"]:";
cin>>burst[i];
}
}
};

class child : public base_class // Derived Class
{
public:

void processes()
{
cout<<"Enter No. of processes:";
cin>>n;
}

void cal_wait()
{
wait[0]=0;

for(i=1;i<n;i++)
{
wait[i]=0;
for(j=0;j<i;j++)
wait[i]+=burst[j];
}
}

void show()
{
for(i=0;i<n;i++)
{
time[i]=burst[i]+wait[i];

cout<<"\nP["<<"Process"<<"\t\t"<<"Burst Time"<<"\t\t"<<"Wait Time"<<"\t\t"<<"Total Time\n";
cout<<"\nP["<<i+1<<"]"<<"\t\t\t"<<burst[i]<<"\t\t\t"<<wait[i]<<"\t\t\t"<<time[i];
}
}
};
int main()
{

child obj;
obj.processes();
obj.input_burst();
obj.cal_wait();
obj.show();

return 0;
}

FCFS Program Using Virtual Function

Let’s see the FCFS Program Using Virtual Function.

#include<iostream>
using namespace std;
class display;
class process
{
	protected:
		int n,burst_time[15],wait_time[15],total_time[15],i,j;
	public:
		 process()
			{
				n=0;
				i=0;j=0;
			}
		
		virtual	void processes()=0; 
			
		void input_burst_time()
			{
				cout<<"\nEnter burst Time\n";
			   	 for(i=0;i<n;i++)
			   		 {
			    	    cout<<"Process["<<i+1<<"]:";
			    	    cin>>burst_time[i];
			  		  }	
			}
		
};

class display : public process
{
	public:
	
	void processes()
			{
				 cout<<"Enter No. of processes:";
  				 cin>>n;
			}
	
		void cal_wait()
			{
				wait_time[0]=0;    
 			
				    for(i=1;i<n;i++)
				    {
				        wait_time[i]=0;
				        for(j=0;j<i;j++)
				            wait_time[i]+=burst_time[j];
				    }	 
			}	
	  void show()
	  	{
		     for(i=0;i<n;i++)
				    {
				        total_time[i]=burst_time[i]+wait_time[i];
				        
				        cout<<"\nP["<<"Process"<<"\t\t"<<"Burst Time"<<"\t\t"<<"Wait Time"<<"\t\t"<<"Total Time\n";
				        cout<<"\nP["<<i+1<<"]"<<"\t\t\t"<<burst_time[i]<<"\t\t\t"<<wait_time[i]<<"\t\t\t"<<total_time[i];
				    }
		 }	 		
};
int main()
{
	display obj;
	obj.processes();
	obj.input_burst_time();
	obj.cal_wait();
	obj.show();

    return 0;
}

FCFS using JAVA

This is the code of FCFS using JAVA.

import java.util.*;
 
public class FCFS {
	public static void main(String args[])
	{
		Scanner sc = new Scanner(System.in);
		System.out.println("enter no of process: ");
		int n = sc.nextInt();
		int processid[] = new int[n];   
		int arrival_time[] = new int[n];     
		int burst_time[] = new int[n];     
		int completion_time[] = new int[n];     
		int trunaround_time[] = new int[n];     
		int waiting_time[] = new int[n];     
		int temp;
		float avgwt=0,avgta=0;
 
		for(int i = 0; i < n; i++)
		{
			System.out.println("enter process " + (i+1) + " arrival time: ");
			arrival_time[i] = sc.nextInt();
			System.out.println("enter process " + (i+1) + " brust time: ");
			burst_time[i] = sc.nextInt();
			processid[i] = i+1;
		}
 
		//process are sorted keeping in view the to arrival times
		for(int i = 0 ; i <n; i++)
		{
			for(int  j=0;  j < n-(i+1) ; j++)
			{
				if( arrival_time[j] > arrival_time[j+1] )
				{
					temp = arrival_time[j];
					arrival_time[j] = arrival_time[j+1];
					arrival_time[j+1] = temp;
					temp = burst_time[j];
					burst_time[j] = burst_time[j+1];
					burst_time[j+1] = temp;
					temp = processid[j];
					processid[j] = processid[j+1];
					processid[j+1] = temp;
				}
			}
		}
		
		// finding completion times for each process
		for(int  i = 0 ; i < n; i++)
		{
			if( i == 0)
			{	
				completion_time[i] = arrival_time[i] + burst_time[i];
			}
			else
			{
				if( arrival_time[i] > completion_time[i-1])
				{
					completion_time[i] = arrival_time[i] + burst_time[i];
				}
				else
					completion_time[i] = completion_time[i-1] + burst_time[i];
			}
			trunaround_time[i] = completion_time[i] - arrival_time[i] ;          // turnaround time= completion time- arrival time
			waiting_time[i] = trunaround_time[i] - burst_time[i] ;          // waiting time= turnaround time- burst time
			avgwt += waiting_time[i] ;               // total waiting time
			avgta += trunaround_time[i] ;               // total turnaround time
		}
		
		System.out.println("\npid  arrival  brust  complete turn waiting");
		for(int  i = 0 ; i< n;  i++)
		{
			System.out.println(processid[i] + "  \t " + arrival_time[i] + "\t" + burst_time[i] + "\t" + completion_time[i] + "\t" + trunaround_time[i] + "\t"  + waiting_time[i] ) ;
		}
		sc.close();
		System.out.println("\naverage waiting time: "+ (avgwt/n));     // printing average waiting time.
		System.out.println("average turnaround time:"+(avgta/n));    // printing average turnaround time.
	}
}

Video Lecture

Frequently Asked Questions (FAQ)

The first-come, first-served (FCFS) algorithm is fine for most ____ systems.
A). batch
B). user initiated
C). interactive
D). multiuser
Best Answer: A. Batch

Explain why ssds often use an fcfs disk-scheduling algorithm?
ssds do not have any any moveable part or rotating disk. This makes the ssds to use the algorithm of fcfs (first come first serve).

What is the average turnaround time for these processes with the FCFS scheduling algorithm?

Gantt Chart for FCFS

Process:   1               2       3

|—————|——-|–|

Time:      0               8       12 13

Average Turnaround Time: ( (8-0)+(12-0.4)+(13-1.0) ) / 3 = 10.53

Is starvation possible in FCFS?
It is possible that starvation may occur in the fcfs disk-scheduling discipline.
First-come, first-served (FCFS) scheduling can cause short processes to wait for a very long time due to big processes. So, we can consider that SJF and priority scheduling may leads to a condition of resources starvation.

What does FCFS mean on Facebook?
FCFS means the buyer who request first to show interest to pay and pick up and to get the item. The seller of the product won’t hold the item at all.

Prof.Fazal Rehman Shamil (Available for Professional Discussions)
1. Message on Facebook page for discussions,
2. Video lectures on Youtube
3. Email is only for Advertisement/business enquiries.