Skip to content

Instantly share code, notes, and snippets.

@debashisbarman
Created August 31, 2014 09:24
Show Gist options
  • Save debashisbarman/63018fceacf1284b51c2 to your computer and use it in GitHub Desktop.
Save debashisbarman/63018fceacf1284b51c2 to your computer and use it in GitHub Desktop.
This is a simple C program to simulate the first come first serve scheduling. The purpose of this program is to calculate the waiting time, average waiting time and turn around time for a user input process queue.
/* =========================================================================
* First Come First Serve Scheduling
*
* DESCRIPTION
* ===========
* This is a simple C program to simulate the first come first serve
* scheduling. The purpose of this program is to calculate the waiting time,
* average waiting time and turn around time for a user input process queue.
*
* =========================================================================
*/
#include <stdio.h>
void fcfs(int ); /* to implement first come first serve scheduling */
void turn_around(int, int [], int []); /* to calculate turn around time */
int main()
{
static int size; /* size of the queue */
printf("Enter the total number of processes: ");
scanf("%d", &size);
if(size == 0) goto empty_queue; /* no process in the queue */
fcfs(size); /* first come first serve */
return 0; /* end of run */
empty_queue: printf("\nProcess queue is empty. End of run.\n");
}
/* fcfs : to implement first come first serve scheduling */
void fcfs(int size)
{
int i, j, result;
int arrival[size], burst[size], waiting[size];
for(i=0; i<size; ++i) waiting[i]=0; /* initialize all values with 0 */
/* input the arrival time and burst time for each process */
for(i=0; i<size; ++i) {
printf("\nEnter arrival time for process %d : ", i+1);
scanf("%d", &arrival[i]);
printf("Enter burst time for process %d : ", i+1);
scanf("%d", &burst[i]);
}
/* first come first serve */
for(i=1; i<size; ++i) {
result=0;
for(j=0; j<i; ++j) result+= burst[j];
waiting[i]=result - arrival[i]; /* waiting time = starting time - arrival time */
}
/* print the waiting time */
printf("\nWaiting Time:\t");
for(i=0; i<size; ++i)
printf("%d\t", waiting[i]);
/* average waiting time */
for(i=0; i<size; ++i) result+=waiting[i];
result/=size;
printf("\nAverage Waiting Time:\t%d", result);
/* turn around time */
turn_around(size, burst, waiting);
printf("\n");
}
/* turn_around : to calculate turn around time for each process */
void turn_around(int size, int burst[], int waiting[])
{
int i;
int turn_around_time[size];
for(i=0; i<size; ++i)
turn_around_time[i]= burst[i] + waiting[i]; /* turn around time = burst time + waiting time */
printf("\nTurn Around Time:\t");
for(i=0; i<size; ++i)
printf("%d\t", turn_around_time[i]);
}
@AdityaJ97
Copy link

For average waiting time .. result should be initialized to 0 and should be a float.

@Parshuu-Hub
Copy link

How we can sort if you give random processes numbers, along with burst time and arrival time?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment