Program for FCFS CPU scheduling in C++

In this tutorial, we will learn the implementation of the First Come First Serve(FCFS) CPU scheduling technique using a C++ program. A CPU is responsible for the smooth execution of all the processes. So, to achieve this, every process is scheduled by the CPU scheduler using a scheduling technique. These techniques are applied to increase the efficiency and throughput of the processor. So here, we will learn about FCFS CPU scheduling and a C++ program that implements the same for scheduling a number of processes.

First Come First Serve (FCFS) CPU scheduling in C++

  • The FCFS scheduling technique is the simplest scheduling algorithm.
  • In this scheduling, CPU schedules the processes on the basis of their arrival time.
  • The processes are stored in a queue that follows a First In First Out (FIFO) structure.
  • When a process arrives, it takes the CPU time and runs till it completes.
  • If another process arrives during execution, it waits in the FIFO queue for getting the CPU time.
  • It is a non-preemptive scheduling method i.e. a process cannot be halted during its execution period.
  • When a process completes execution, it is removed from the queue and CPU is given to the next process.

Example of FCFS CPU scheduling

So, let us see an example of FCFS CPU scheduling for better understanding of this technique-

FCFS CPU scheduling in C++

In the above example, there are 4 processes P1, P2, P3, and P4. We know that in FCFS scheduling the processes get the CPU time on the basis of arrival time. So, the execution of processes is – P1, P2, P3, and finally P4.

The Gantt chart for the above example is –

Gantt chart

Waiting time calculations

To calculate the waiting time for all processes, use the formula –

Waiting time = Response time – Arrival time

So, the waiting time for 4 processes is –

P1 = 0 – 0 = 0
P2 = 5 – 2 = 3
P3 = 6 – 3 = 3
 P4 = 10 – 9 = 1

Turn-around time calculations

We can calculate turn-around time for all processes using formula –

Turn-around time = Waiting time + Burst time

So, the turn-around time for 4 processes is –

P1 = 0 + 5 = 5
P2 = 3 + 1 = 4
P3 = 3 + 4 = 7
P4 = 1 + 2 = 3

Calculating average times

Finally, we can calculate the average waiting and turn-around time as –

Average waiting time = { W(P1) + W(P2) + … + W(Pn) } / n
Average turn-around time = { T(P1) + T(P2) + … + T(Pn) } / n

So, the average waiting and turn-around time is –

Average waiting time = (0 + 3 + 3 + 1) / 4 = 7/4 = 1.75
Average turn-around time = (5 + 4 + 7 +3) / 4 = 19/4 = 4.75

C++ program to implement FCFS CPU scheduling

We can implement an FCFS scheduling technique using an array of objects that follows the FIFO scheme. We store all the details related to a process in the object of a class. To make the FCFS Gantt chart, we have to sort the array which stores all the process records on the basis of arrival times. So, let us see the program for FCFS CPU scheduling –

#include<iostream>
#define MAX_PROCESS 10
using namespace std;
class process
{
  public:
  int process_num;
  int burst_time;
  int arrival_time;
  int response_time;
  int waiting_time;
  int turnaround_time;
  void input_process(int);
  int get_at()
  {
    return arrival_time;
  }
};
void process::input_process(int count)
{
  process_num=count+1;
  cout<<"\nENTER BURST TIME FOR PROCESS "<<count+1<<" : ";
  cin>>burst_time;
  cout<<"ENTER ARRIVAL TIME FOR PROCESS "<<count+1<<" : ";
  cin>>arrival_time;
}
void calc_wait_tat(process*,int);
void average(process*,int);
void display(process*,int);
int main()
{
  process p[MAX_PROCESS],temp;
  int num,i,j;
  cout<<"ENTER NUMBER OF PROCESSES : ";
  cin>>num;
  for(i=0;i<num;++i)
    p[i].input_process(i);
  for(i=0;i<num;++i)
  {
    for(j=i+1;j<num;++j)
    {
      if(p[i].get_at()>p[j].get_at())
      {
        temp=p[i];
        p[i]=p[j];
        p[j]=temp;
      }
    }
  }
  calc_wait_tat(p,num);
  display(p,num);
  return 0;
}
void calc_wait_tat(process *p,int n)
{
  int i;
  p[0].response_time=0;
  for(i=1;i<n;++i)
  {
    p[i].response_time=p[i-1].burst_time+p[i-1].response_time;
    if(p[i].response_time<p[i].arrival_time)
      p[i].response_time=p[i].arrival_time;
  }
  p[0].waiting_time=0;
  for(i=1;i<n;++i)
    p[i].waiting_time=p[i].response_time-p[i].arrival_time;
  for(i=0;i<n;++i)
    p[i].turnaround_time=p[i].waiting_time+p[i].burst_time;
}
void average(process *p,int n)
{
  float avg_wt=0,avg_tat=0;
  for(int i=0;i<n;++i)
  {
    avg_wt+=(float)p[i].waiting_time;
    avg_tat+=(float)p[i].turnaround_time;
  }
  avg_wt/=n;
  avg_tat/=n;
  cout<<"\n\nAVERAGE WAITING TIME : "<<avg_wt;
  cout<<"\nAVERAGE TURN AROUND TIME : "<<avg_tat;
}
void display(process *p,int n)
{
        cout<<"Processes "<<"  Burst time  "<<" Waiting time  "<<" Turn around time\n";
        for (int i=0;i<n;i++)
        {
                cout<<"\n   "<<p[i].process_num<<"\t\t"<<p[i].burst_time<<"\t     "<<p[i].waiting_time<<"\t\t      "<<p[i].turnaround_time;
        }
        average(p,n);
}

In the program, the array of object ‘p’ of class ‘process’ stores the information of all the processes. Firstly, we take the number of processes, arrival time, and burst time for each process from the user. Then, the sorting of processes is done on the basis of arrival time. The function calc_wait_tat() calculates the waiting time and turn-around time for each process. Finally, the function display() displays the sequence of execution of processes with their waiting and turn-around times.

C++ program output

So, the above program displays the processes sequentially with the waiting and turn-around time. And finally, it displays the average waiting time and turn-around time. Obviously, the smaller the value of the average waiting time, the more efficient the scheduling technique will be. So, the output of the above program is –

[email protected]:~/cpp$ g++ fcfs.cpp
[email protected]:~/cpp$ ./a.out
ENTER NUMBER OF PROCESSES : 6

ENTER BURST TIME FOR PROCESS 1 : 3
ENTER ARRIVAL TIME FOR PROCESS 1 : 5

ENTER BURST TIME FOR PROCESS 2 : 4
ENTER ARRIVAL TIME FOR PROCESS 2 : 0

ENTER BURST TIME FOR PROCESS 3 : 1
ENTER ARRIVAL TIME FOR PROCESS 3 : 6

ENTER BURST TIME FOR PROCESS 4 : 4
ENTER ARRIVAL TIME FOR PROCESS 4 : 8

ENTER BURST TIME FOR PROCESS 5 : 2
ENTER ARRIVAL TIME FOR PROCESS 5 : 8

ENTER BURST TIME FOR PROCESS 6 : 1
ENTER ARRIVAL TIME FOR PROCESS 6 : 10
Processes   Burst time   Waiting time   Turn around time

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

AVERAGE WAITING TIME : 2.16667
AVERAGE TURN AROUND TIME : 4.66667
[email protected]:~/cpp$

Here, there are 6 processes entered by the user. Then, the users enter the arrival and burst time for each process. Finally, the program calculates and displays the average waiting time and average turn-around time.

Thank you for reading this tutorial. I hope it is helpful to you.

Also read: Inline function in C++

Leave a Reply

Your email address will not be published. Required fields are marked *