0% found this document useful (0 votes)
7 views

Example of First Come First Serve Algorithm

Uploaded by

panjabimisba
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Example of First Come First Serve Algorithm

Uploaded by

panjabimisba
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Example of First Come First Serve Algorithm :

Q. Consider the four jobs are scheduled for execution (all jobs arrived at same time). Find
the average waiting time and turnaround time.

Gantt Chart:
Q.Program of First Come First Serve Algorithm :
// Print totals and averages
#include <stdio.h> printf("\nTotal Waiting Time: %d",
struct Process totalWaitingTime);
{
int pid; // Process ID printf("\nTotal Turnaround Time: %d",
int burstTime; // Burst Time totalTurnaroundTime);
int waitingTime; // Waiting Time
int turnAroundTime; // Turnaround Time printf("\nAverage Waiting Time: %.2f",
}; (float)totalWaitingTime / n);

void calculateTimes(struct Process printf("\nAverage Turnaround Time: %.2f\n",


processes[], int n) (float)totalTurnaroundTime / n);
{ }
int totalWaitingTime = 0,
totalTurnaroundTime = 0; int main()
{
// Waiting time for the first process is int n;
always 0 // Input number of processes
processes[0].waitingTime = 0; printf("Enter number of processes: ");
scanf("%d", &n);
// Calculate waiting and turnaround time for
each process struct Process processes[n]; // Array of
for (int i = 1; i < n; i++) processes
{
processes[i].waitingTime = processes[i - // Input burst times for each process
1].waitingTime + processes[i - 1].burstTime; for (int i = 0; i < n; i++)
} {
processes[i].pid = i + 1;
for (int i = 0; i < n; i++) printf("Enter burst time for process %d: ",
{ processes[i].pid);
processes[i].turnAroundTime = scanf("%d", &processes[i].burstTime);
processes[i].waitingTime + }
processes[i].burstTime;
} // Calculate times
calculateTimes(processes, n);
// Display results
printf("\nProcess\tBurst\tWait\tTurnaround\ return 0;
n"); }
for (int i = 0; i < n; i++)
{
totalWaitingTime += processes[i].waitingTime;
totalTurnaroundTime +=
processes[i].turnAroundTime;
printf("%d\t%d\t%d\t%d\n", processes[i].pid,
processes[i].burstTime,
processes[i].waitingTime,
processes[i].turnAroundTime);
}

You might also like