First Come First Serve (FIFO)
First come first serve assigns a CPU to the process which comes first. hence as the name suggests first come first serve. it simply means the process which comes in the ready state first, that process will be allocated to the CPU.The mode of first come first serve algorithm is non-preemptive. now, what does that non-preemptive mean?
There are two types of CPU scheduling algorithm one is preemptive and other is non-preemptive.
Preemptive
In case of preemptive, if the process is running in CPU then that process can be removed from the CPU and CPU can be allocated to some another process that is known as preemptive CPU scheduling algorithm.Non-Preemptive
In this case, once a CPU has been allocated to the one process then that process cannot be removed from the CPU until its termination. if the process voluntarily wants to exit from the CPU that means it has been already finished its execution.First come first serve advantages
- first come first serve is the simplest scheduling algorithm compared to any other scheduling algorithm
- it is simple and easy to understand
- No complexity
- Easy to implement
First come first serve disadvantages
- It is non-preemptive
- due to its non-preemptive, the process with the higher priority does not execute first
How first come first serve algorithm works
Given n processes with their burst times, the errand is to find the average waiting time and an average turn around time using FIFO scheduling calculation. First in, first-out (FIFO), otherwise called first come first serve (FCFS), is the least complex scheduling calculation. FIFO just lines processes in the request that they touch base in the prepared line. In this, the procedure that starts things out will be executed first and the next process begins only after the previous process gets completely executed. Here we are considering that landing time for all processes is 0.
First come first serve program in C++
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
struct process
{
int burst_time;
int wait_time;
int ta_time;
};
int main()
{
int n,total_wait=0,total_ta=0;
cout<<"Enter the number of process : ";
cin>>n;
process p[n];
cout<<"Enter Burst Time"<<endl;
for(int i=0;i<n;i++)
{
cout<<"Process "<<i+1<<" : ";
cin>>p[i].burst_time;
p[i].wait_time = total_wait;
p[i].ta_time = p[i].wait_time + p[i].burst_time;
total_wait += p[i].burst_time;
total_ta += p[i].ta_time;
}
cout<<"\nGANTT CHART"<<endl;
for(int i=0;i<n;i++)
{
cout<<setw(p[i].burst_time + 1);
cout<<left<<i+1;
}
cout<<right;
cout<<endl;
for(int i=0;i<n;i++)
{
cout<<setw(p[i].burst_time + 1)<<setfill('-')<<"|";
}
cout<<endl<<0<<setfill(' ');
for(int i=1;i<n;i++)
{
cout<<setw(p[i-1].burst_time + 1)<<p[i].wait_time;
}
cout<<setw(p[n-1].burst_time + 1)<<total_wait<<endl;
cout<<"\nProcess ID\tBurst Time\tWait Time\tTurnaround Time"<<endl;
for(int i=0;i<n;i++)
{
cout<<i+1<<"\t\t"<<p[i].burst_time<<"\t\t"<<p[i].wait_time<<"\t\t"<<p[i].ta_time<<endl;
}
cout<<"\nTotal Wait Time : "<<total_wait;
cout<<"\nAverage Wait Time : "<<(float)total_wait/n;
cout<<"\nTotal Turnaround Time : "<<total_ta;
cout<<"\nAverage Turnaround Time : "<<(float)total_ta/n;
return 0;
}
OUTPUT:
Enter the number of process : 4
Enter Burst Time
Process 1 : 5
Process 2 : 3
Process 3 : 3
Process 4 : 6
GANTT CHART
1 2 3 4
-----|---|---|------|
0 5 8 11 17
Process ID Burst Time Wait Time Turnaround Time
1 5 0 5
2 3 5 8
3 3 8 11
4 6 11 17
Total Wait Time : 17
Average Wait Time : 4.25
Total Turnaround Time : 41
Average Turnaround Time : 10.25
You must be also searching for these programming languages :