Written by Sai Sandeep Thota on January 21, 2013 in C > Programming

C Program to Implement First Come First Serve (FCFS)

GET ALERTS:

Get our Latest updates delivered to your mailbox!

First Come First Serve is a method of load balancing in which the Process which comes first is served first. For example there are three processes P1, P2 and P3, the process which comes first in the queue is given the highest priority regardless of its burst time.

How is it done?
We are declaring 6 variables. process is used to store the process ID’s, wtime is used to store wait time of a process, ptime is used to store the process time or burst time, total is used to store the total wait time and avg is used to store the average wait time. We are calculating the process wait time by using the formula wtime[i]=wtime[i-1]+ptime[i-1]; and calculating the Total wait time by iteration.

First Come First Serve C Program

#include<conio.h>
#include<stdio.h>
#include<process.h>
void main()
{
	int i, n, process[10], total=0, wtime[10], ptime[10];
	float avg=0;
	clrscr();
	printf("\nEnter number of Processes:");
	scanf("%d", &n);
	for(i=0;i<n;i++)
	{
		printf("\nEnter Process %d ID:",i+1);
		scanf("%d", &process[i]);
		printf("\nEnter Process %d Wait Time:",i+1);
		scanf("%d",&ptime[i]);
	}
	wtime[0]=0;
	for(i=1;i<n;i++)
	{
		wtime[i]=wtime[i-1]+ptime[i-1];
		total=total+wtime[i];
	}
	avg=(float)total/n;
	printf("\nP_ID\t P_TIME\t W_TIME\n");
	for(i=0;i<n;i++)
		printf("%d\t %d\t %d\n",process[i],ptime[i],wtime[i]);
	printf("\nTotal Waiting Time: %d \nAverage Waiting Time: %f", total, avg);
	getch();
}

Output:

FCFS-C-Program

{ 0 comments… add one now }

Leave a Comment

Previous post:

Next post: