c program to simulate the cpu scheduling algorithm First Come First Serve(FCFS)

Concept:To calculate the average waiting time using the FCFS algorithm first the waiting time of the first process is kept zero and the waiting time of the second process is the burst time of the first process and the waiting time of the third process is the sum of the burst times of the first and the second process and so on . after calculating all the waiting times the average waiting time is calculated as the average of all the waiting times. FCFS mainly says first come first serve the algorithm which came first will be served first.

ALGORITHM:
Step 1: Start 
Step 2: Accept the number of processes in the ready Queue
Step 3: For each process in the ready Q, assign the process name and the burst time
Step 4: Set the waiting of the first process as ‗0‘and its burst time as its turnaround time Step 5: for each process in the Ready Q calculate
        a). Waiting time(n)= waiting time (n-1) + Burst time (n-1)
        b). Turnaround time (n)= waiting time(n)+Burst time(n)
Step 6: Calculate
       a) Average waiting time = Total waiting Time / Number of process
       b) Average Turnaround time = Total Turnaround Time / Number of process

Step 7: Stop 

				
					// First Come First Serve ( FCFS Program in C ).
#include<stdio.h>
int arr[10], ser[10], pro[10];
float avgtime=0,avgwait;
void main()
{
int n=0,i=0,j=0,sum;
printf("enter number of process ");
scanf("%d",&n);
for(i=0;i
{
printf("enter arrival time and service time of %d process",i+1);
scanf("%d%d",&arr[i],&ser[i]);
}
sum=0;
avgtime=0;
for(i=0;i
{
pro[i]=sum;
sum=sum+ser[i];
avgtime=avgtime+sum-i;
}
avgwait=avgtime/n;
printf("average waiting time for fcfs is: %f",avgwait);
}

				
			
OUTPUT:
enter number of process: 3
enter arrival time and service time of 1 process 15
enter arrival time and service time of 2 process 21
enter arrival time and service time of 3 process 6
average waiting time for fcfs is: 17

Leave a Reply