Search This Blog

Simulation of FCFS CPU Scheduling Algorithm in C

This is a C program to simulate First come first served (FCFS) CPU scheduling algorithm. First come first served algorithm serves each processes (or jobs) in the order of arrival. The process with lowest arrival time is executed first.

Program:
#include<stdio.h>
struct process
{
int burst,wait;
}p[20]={0,0};



int main()
{
int n,i,totalwait=0,totalturn=0;
printf("\nEnter The No Of Process :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter The Burst Time (in ms) For Process #%2d :",i+1);
scanf("%d",&p[i].burst);
}
printf("\nProcess \t Waiting Time\tTurnAround Time ");
printf("\n\t (in ms)\t(in ms)");
for(i=0;i<n;i++)
{
printf("\nProcess # %-12d%-15d%-15d",i+1,p[i].wait,p[i].wait+p[i].burst);
p[i+1].wait=p[i].wait+p[i].burst;
totalwait=totalwait+p[i].wait;
totalturn=totalturn+p[i].wait+p[i].burst;
}
printf("\n\nAVERAGE\n--------- ");
printf("\nWaiting Time : %f ms",totalwait/(float)n);
printf("\nTurnAround Time : %f ms\n\n",totalturn/(float)n);
return 0;
}

Output:
Enter The No Of Process :3
Enter The Burst Time (in ms) For Process # 1 :10
Enter The Burst Time (in ms) For Process # 2 :30
Enter The Burst Time (in ms) For Process # 3 :20
Process    Waiting Time     Turn Around Time
                     (in ms)                      (in ms)
Process # 1     0                               10
Process # 2    10                              40
Process # 3    40                              60

AVERAGE
---------
Waiting Time : 16.666667 ms
TurnAround Time : 36.666667 ms

No comments: