You will write a C/C program to implement a simulator with different scheduling algorithms. The simulator selects a task to run from ready queue based on the scheduling algorithm. Since the project intends to simulate a CPU scheduler, so it does not require any actual process creation or execution. The CPU PRIORITY scheduling algorithm is implemented using the C Program. The Scheduling program tested with 3 processes.
![C program to simulate srtf algorithm download C program to simulate srtf algorithm download](/uploads/1/2/4/7/124794771/214078463.png)
Let us learn how to implement first come first serve algorithm in C programming with its explanation, output, advantages, disadvantages and much more.
What is First Come First Serve Disk Scheduling Algorithm?
The first come first serve algorithm is commonly abbreviated as FCFS algorithm. It primarily works on the First In First Out (FIFO) principle.
The incoming requests or jobs in the system queue are executed based on first come first served basis. This is a non-preemptive scheduling algorithm.
Therefore, once the CPU is allocated to a particular job, the job keeps on executing till it gets completed. The CPU cannot leave the current job before it gets completed. So, the CPU cannot move to another job in the queue.
The FCFS algorithm is usually represented using Gantt’s Chart on paper. However, the FCFS scheduling algorithm is not so efficient when it comes to performance optimization.
It is not optimized for time-sharing systems. The average waiting time for the first come first serve scheduling algorithm is highly dependent on the burst time of the jobs.
Advantages
- Simple and easy to implement
- Every process/job gets executed completely
- Lower possibilities of starvation
Disadvantages
- Poor performance due to high average waiting time
- There is no option for pre-emption of a job.
- Higher average turnaround time
- In-efficient for time-sharing systems
Note: This FCFS Algorithm C program is compiled with GNU GCC compiler using Linux terminal on Linux Ubuntu operating system.
First Come First Serve CPU Scheduling Algorithm C Program
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 | { floatburst_time[30],waiting_time[30],turnaround_time[30]; floataverage_waiting_time=0.0,average_turnaround_time=0.0; printf('Enter The Number of Processes To Execute:t'); printf('nEnter The Burst Time of Processes:nn'); { scanf('%f',&burst_time[count]); waiting_time[0]=0; { for(j=0;j<count;j++) waiting_time[count]=waiting_time[count]+burst_time[j]; } printf('nProcessttBurst TimetWaiting TimetTurnaround Timen'); { turnaround_time[count]=burst_time[count]+waiting_time[count]; average_waiting_time=average_waiting_time+waiting_time[count]; average_turnaround_time=average_turnaround_time+turnaround_time[count]; printf('nProcess [%d]tt%.2ftt%.2ftt%.2f',count+1,burst_time[count],waiting_time[count],turnaround_time[count]); printf('n'); average_turnaround_time=average_turnaround_time/count; printf('nAverage Waiting Time = %f',average_waiting_time); printf('nAverage Turnaround Time = %f',average_turnaround_time); return0; |
Output
If you have any doubts about the implementation of FCFS program in C language, let us know about it in the comment section. Find more about it here.
CPU Scheduling Algorithms |
---|
Preemptive Shortest Job First Scheduling Algorithm |
SCAN Disk Scheduling Algorithm |
C SCAN Disk Scheduling Algorithm |
Round Robin Scheduling Algorithm |
Shortest Job First Scheduling Algorithm |
Multi-Level Feedback Queue Scheduling Algorithm |
Preemptive Priority Scheduling Algorithm |
Non-Preemptive Priority Scheduling Scheduling Algorithm |
Shortest Seek Time First Scheduling Algorithm |
Related
#include<stdio.h>
struct proc
{
int pid;
int at,bt,wt,tat,rbt;
int flag,flag1;
};
struct proc p1[10];
int i,j,k,n,no,m;
float atat=0.0,awt=0.0;
int tbt=0;
int minimum1();
int main()
{
int minv,locv,mins,locs;
printf('nenter the number of processes:');
scanf('%d',&n);
printf('nenter the proc information:');
printf('npid at bt');
for(i=0;i<n;i++)
{
p1[i].wt=0;
p1[i].tat=0;
p1[i].flag=0;
p1[i].flag1=0;
scanf('%d%d%d',&p1[i].pid,&p1[i].at,&p1[i].bt);
tbt+=p1[i].bt;
p1[i].rbt=p1[i].bt;
}
printf('nthe proc information:');
printf('npid at bt');
for(i=0;i<n;i++)
{
printf('n%d %d %d',p1[i].pid,p1[i].at,p1[i].bt);
}
minv=p1[0].at;
locv=0;
for(i=1;i<n;i++)
{
if(p1[i].at<minv)
{
locv=i; //tells min at process in locv
minv=p1[i].at;
}
}
for(i=0;i<n;i++)
{
if(p1[i].atminv)
{
p1[i].flag1=1; //processes having same minimum at
}
}
mins=p1[0].bt;
locs=0;
for(i=0;i<n;i++)
{
if(p1[i].flag11&&p1[i].bt<mins)
{
mins=p1[i].bt; //gives process with minimum burst time
locs=i;
}
}
printf('ngantt chart:');
for(i=minv;i<tbt+minv;i++)
{
for(j=0;j<n;j++)
{
if(p1[j].rbt>0&&p1[j].at<=i)
{
p1[j].flag=1;
}
}
no=minimum1();
printf('%d p[%d]',i,p1[no].pid);
p1[no].rbt=p1[no].rbt-1;
for(k=0;k<n;k++)
{
if(p1[k].rbt>0&&p1[k].at<=i&&k!=no)
{
p1[k].wt++;
}
}
}
printf('%d',tbt+minv);
for(i=0;i<n;i++)
{
awt+=p1[i].wt;
}
awt=awt/n;
for(i=0;i<n;i++)
{
p1[i].tat=p1[i].wt+p1[i].bt;
atat+=p1[i].tat;
}
atat=atat/n;
printf('n average wt=%f, average tat=%f',awt,atat);
printf('nthe proc information:');
printf('npid at bt wt tat');
for(i=0;i<n;i++)
{
printf('n%d %d %d %d %d',p1[i].pid,p1[i].at,p1[i].bt,p1[i].wt,p1[i].tat);
}
}
int minimum1()
{
int loc,z;
int mini;
mini=99;
loc=-1;
for(z=0;z<n;z++)
{
if(p1[z].rbt>0&&p1[z].at<=i&&p1[z].rbt<mini)
{
mini=p1[z].rbt;
loc=z;
}
}
return loc;
}
struct proc
{
int pid;
int at,bt,wt,tat,rbt;
int flag,flag1;
};
struct proc p1[10];
int i,j,k,n,no,m;
float atat=0.0,awt=0.0;
int tbt=0;
int minimum1();
int main()
{
int minv,locv,mins,locs;
printf('nenter the number of processes:');
scanf('%d',&n);
printf('nenter the proc information:');
printf('npid at bt');
for(i=0;i<n;i++)
{
p1[i].wt=0;
p1[i].tat=0;
p1[i].flag=0;
p1[i].flag1=0;
scanf('%d%d%d',&p1[i].pid,&p1[i].at,&p1[i].bt);
tbt+=p1[i].bt;
p1[i].rbt=p1[i].bt;
}
printf('nthe proc information:');
printf('npid at bt');
for(i=0;i<n;i++)
{
printf('n%d %d %d',p1[i].pid,p1[i].at,p1[i].bt);
}
minv=p1[0].at;
locv=0;
for(i=1;i<n;i++)
{
if(p1[i].at<minv)
{
locv=i; //tells min at process in locv
minv=p1[i].at;
}
}
for(i=0;i<n;i++)
{
if(p1[i].atminv)
{
p1[i].flag1=1; //processes having same minimum at
}
}
mins=p1[0].bt;
locs=0;
for(i=0;i<n;i++)
{
if(p1[i].flag11&&p1[i].bt<mins)
{
mins=p1[i].bt; //gives process with minimum burst time
locs=i;
}
}
printf('ngantt chart:');
for(i=minv;i<tbt+minv;i++)
{
for(j=0;j<n;j++)
{
if(p1[j].rbt>0&&p1[j].at<=i)
{
p1[j].flag=1;
}
}
no=minimum1();
printf('%d p[%d]',i,p1[no].pid);
p1[no].rbt=p1[no].rbt-1;
for(k=0;k<n;k++)
{
if(p1[k].rbt>0&&p1[k].at<=i&&k!=no)
{
p1[k].wt++;
}
}
}
printf('%d',tbt+minv);
for(i=0;i<n;i++)
{
awt+=p1[i].wt;
}
awt=awt/n;
for(i=0;i<n;i++)
{
p1[i].tat=p1[i].wt+p1[i].bt;
atat+=p1[i].tat;
}
atat=atat/n;
printf('n average wt=%f, average tat=%f',awt,atat);
printf('nthe proc information:');
printf('npid at bt wt tat');
for(i=0;i<n;i++)
{
printf('n%d %d %d %d %d',p1[i].pid,p1[i].at,p1[i].bt,p1[i].wt,p1[i].tat);
}
}
int minimum1()
{
int loc,z;
int mini;
mini=99;
loc=-1;
for(z=0;z<n;z++)
{
if(p1[z].rbt>0&&p1[z].at<=i&&p1[z].rbt<mini)
{
mini=p1[z].rbt;
loc=z;
}
}
return loc;
}