22BPS1110 Os4
22BPS1110 Os4
Aim: Implementation of FCFS (First come First Serve) algorithm to find average waiting
time .
Source Code:
#include<stdio.h>
void findAverageWaitingTime(int processes[], int n, int bt[]) {
int wt[n];
wt[0] = 0;
for (int i = 1; i < n; i++)
wt[i] = bt[i - 1] + wt[i - 1];
printf("\nIndividual Waiting Times:\n");
for (int i = 0; i < n; i++)
printf("Process %d: %d\n", processes[i], wt[i]);
float avg_wt = 0;
for (int i = 0; i < n; i++)
avg_wt += wt[i];
avg_wt /= n;
printf("\nAverage Waiting Time: %.2f\n", avg_wt);
}
int main() {
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
int processes[n];
int burst_time[n];
for (int i = 0; i < n; i++) {
printf("Enter burst time for process %d: ", i + 1);
scanf("%d", &burst_time[i]);
processes[i] = i + 1; // Process IDs start from 1
}
findAverageWaitingTime(processes, n, burst_time);
return 0;
}
Output:
The output provides the average waiting time for processes using the FCFS scheduling
algorithm, reflecting the average time processes spend waiting in the ready queue. Lower
average waiting times indicate more efficient process execution. The user input and algorithmic
execution contribute to assessing the scheduling strategy's performance.
Result:
Gantt Charts
Processes Burst Time Waiting time (wt[i] = bt[i - 1]
+ wt[i - 1])
P1 10 0
P2 5 10
P3 8 15
Average Waiting Time: 8.33
Hence we find that the average waiting time of three processes is 8.33 using the FCFS CPU
scheduling.