FCFS Program in C with Output – Algorithm Explained

FCFS : First Come First Serve is a scheduling algorithm that is used by the CPU to schedule tasks. This algorithm is non-preemptive. In this scheduling algorithm we allocate CPU to the process that comes first in the ready queue. The priority order is decided by the order in which they are requested by the processor. The process which requests the services of the CPU first will get it first. As the name of the algorithm is self explanatory that the first comers will be served first. This is how the algorithm works.

To understand the algorithm we should first know some terms , that are:

1. Completion time: Time when the execution of the service is completed.
2. Turn around time : The summation of the burst time and the waiting time will give you the turn-around time.
3. Waiting time: The time duration during which the process waits to get the CPU.

Some advantages of FCFS algorithm are:

• It’s easy to understand and implement
• It’s the simplest form of CPU scheduling algorithm

Some of its disadvantages are:

• The waiting time is quite longer comparatively.
• Sometimes, this algorithm is not regarded as very efficient
• It’s not ideal for time sharing systems.

FCFS Algorithm Explained

1. Begin the process and declare the array size.
2. Get the number of processes to be inserted.
3. Get the value.
4. Start the first process and let the others be in the queue.
5. Calculate total number of burst time.
6. Display the value and stop the process.

FCFS Program in C

```#include<stdio.h>
int main()
{
int AT[10],BT[10],WT[10],TT[10],n;
int burst=0,cmpl_T;
float Avg_WT,Avg_TT,Total=0;
printf("Enter number of the process\n");
scanf("%d",&n);
printf("Enter Arrival time and Burst time of the process\n");
printf("AT\tBT\n");
for(int i=0;i<n;i++)
{
scanf("%d%d",&AT[i],&BT[i]);
}

// Logic for calculating Waiting time
for(int i=0;i<n;i++)
{
if(i==0)
WT[i]=AT[i];
else
WT[i]=burst-AT[i];
burst+=BT[i];
Total+=WT[i];
}
Avg_WT=Total/n;

// Logic for calculating Turnaround time
cmpl_T=0;
Total=0;
for(int i=0;i<n;i++)
{
cmpl_T+=BT[i];
TT[i]=cmpl_T-AT[i];
Total+=TT[i];
}
Avg_TT=Total/n;

// printing of outputs

printf("Process ,Waiting_time ,TurnA_time\n");
for(int i=0;i<n;i++)
{
printf("%d\t\t%d\t\t%d\n",i+1,WT[i],TT[i]);
}
printf("Average waiting time is : %f\n",Avg_WT);
printf("Average turn around time is : %f\n",Avg_TT);
return 0;
}
```

Output

``````Enter number of process
3
Enter Arrival time and Burst time of the process
At BT
0 2
1 4
2 3
Process ,  Waiting_time ,  TurnA_time
1    	  0        2
2    	  1        5
3    	  4        7
Average waiting time is : 1.666667
Average turn around time is : 4.666667
``````

Here we declare an array for the Arrival time, Burst time, Waiting time and turn around time. First we take an input from the user for the number of process. Then we take arrival time and burst time of all the processes. Later the waiting time and turn around is calculated and stored in the Array WT and TT respectively.

Hope the tutorial was informative and useful to you. Hit the comment section for any doubt or query.

Categories C