### Ticker

6/recent/ticker-posts

# C program on FCFS(First come first serve) in operating system.

What is first come first serve Scheduling (FCFS):-

In this scheduling algorithm we allocate CPU to process that comes first in the ready queue. That is, the  process that  comes first in the ready queue will  gets the CPU first. So it's called first come first serve algorithm.

It is Non preemptive it means in FCFS Scheduling once the CPU has been allocated to a process  , the process keeps the CPU until it releases the CPU either by terminating or by requesting I/O.

• Easy to implement
• Easy to understand
• The simplest form of a CPU scheduling algorithm

• Average waiting time is often quite longer
• Because of its simplicity, FCFS is not very efficient
• Not an ideal technique for time-sharing systems

C program:-

Here I am going to give to two solution .In the first Code  we  will just use Array and in the second Code we will use structure .
Code 1:-

In this code we simply declare Some array for the arrival time , Burst time ,Waiting time , and turn around time .

First we take a input from the user which is number of process  in the Ready queue.then After we take Arrival time and Burst time of all the process one by one.Aftet that we will calculate the waiting time and Turn Around time of all the process  and store it in the Array WT and TT respectively .

#include<stdio.h>
int main()
{
int AT,BT,WT,TT,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 Turn around 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

Code 2:-

In This Code first we create the structure for the process .Then after we declare an array of structure type .

cmpl_T stands for completion time of the process which we are calculating in the program.

#include<stdio.h>
struct process
{
int AT,WT,TT,BT;
};

struct process a;
int main()
{
int n;
int burst=0,cmpl_T;
float Avg_WT,Avg_TT,Total=0;
printf("Enter number of process\n");
scanf("%d",&n);
printf("Enter Arrival time and Burst time of the process\n");
printf("At BT\n");
for(int i=0;i<n;i++)
{
scanf("%d%d",&a[i].AT,&a[i].BT);
}
// Logic for calculating Waiting time
for(int i=0;i<n;i++)
{
if(i==0)
a[i].WT=a[i].AT;
else
a[i].WT=burst-a[i].AT;
burst+=a[i].BT;
Total+=a[i].WT;
}
Avg_WT=Total/n;
// Logic for calculating Turn around time
cmpl_T=0;
Total=0;
for(int i=0;i<n;i++)
{
cmpl_T+=a[i].BT;
a[i].TT=cmpl_T-a[i].AT;
Total+=a[i].TT;
}
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,a[i].WT,a[i].TT);
}
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

codechef problems:-

Wipro :-

Infytq :-

Key Points;-

Hackerrank:-

C-tutorial:-

See more:-

Keywords:-,fcfs scheduling algorithm in os,
,fcfs code in os,
,sjf in os,
,cpu scheduling algorithms in os,
,priority scheduling program in c,
,first come first served program in c,
,disk scheduling algorithm,

1. 2. 