Signup/Sign In

C++ Program For (FCFS) FIRST COME FIRST SERVE Scheduling Algorithm

In this tutorial we are required to generate the scheduling algorithm i.e. whichever command will come first will be executed first irrespective of the other factors. Let's first understand some of the basic terms before implementing the algorithm for FCFS.

What is FCFS (FIRST COME FIRST SERVE) ?

First come First serve, Also known as First in, First out is the simplest scheduling algorithm.

In this process that comes first is used and executed first And the next one will start only when the previous one is completed and fully executed. First Come First Served (FCFS) is a Non-Preemptive scheduling algorithm. FIFO (First In First Out) strategy assigns priority to process in the order in which they request the processor. The process that requests the CPU first is allocated the CPU first. This is easily implemented with a FIFO queue for managing the tasks. As the process comes in, they are put at the end of the queue. As the CPU finishes each task, it removes it from the start of the queue and heads on to the next task.


Terms Used in FCFS Algorithm:-

1. Completion time:-time taken to complete execution of the program.

2. Turn around time:- Time gap of completion time and arrival(starting) time.

3. Waiting time:-Time gap of turn around time and burst time.

C++ Program for FCFS Algorithm

#include<iostream>
using namespace std;
int main()
{   int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;
    cout<<"Enter total number of processes(maximum 20):";
    cin>>n;
 
    cout<<"\nEnter Process Burst Time aka DURATION \n";
    for(i=0;i<n;i++)
    {
        cout<<"P["<<i+1<<"]:";
        cin>>bt[i];
    }
    wt[0]=0;    //waiting time for first process is 0
    //calculating waiting time
    for(i=1;i<n;i++)
    {
        wt[i]=0;
        for(j=0;j<i;j++)
            wt[i]+=bt[j];
    }
    cout<<"\nProcess\t\tBurst Time\tWaiting Time\tTurnaround Time";
    //calculating turnaround time
    for(i=0;i<n;i++)
    {
        tat[i]=bt[i]+wt[i];
        avwt+=wt[i];
        avtat+=tat[i];
        cout<<"\nP["<<i+1<<"]"<<"\t\t"<<bt[i]<<"\t\t"<<wt[i]<<"\t\t"<<tat[i];
    }
    avwt/=i;
    avtat/=i;
    cout<<"\n\nAverage Waiting Time:"<<avwt;
    cout<<"\nAverage Turnaround Time:"<<avtat;
 
    return 0;
}


Enter total number of processes(maximum 20):6

Enter Process Burst Time aka DURATION
P[1]:1
P[2]:2
P[3]:3
P[4]:4
P[5]:5
P[6]:6

Process Burst Time Waiting Time Turnaround Time
P[1] 1 0 1
P[2] 2 1 3
P[3] 3 3 6
P[4] 4 6 10
P[5] 5 10 15
P[6] 6 15 21

Average Waiting Time:5
Average Turnaround Time:9

Conclusion

In this tutorial, we learned how to program for a First come first serve (FCFS) scheduling algorithm that simply schedules the jobs according to their arrival time. The job which comes first in the ready queue will get the CPU first. The lesser the arrival time of the job, the sooner will the job gets the CPU.



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.