fcfs.c
Fri May 10 2024 02:24:18 GMT+0000 (Coordinated Universal Time)
Saved by @exam123
#include<stdio.h>
struct process {
int pid;
int burst;
int waiting;
int turnaround;
};
void calculateTimes(struct process proc[], int n) {
int total_waiting = 0, total_turnaround = 0;
proc[0].waiting = 0;
proc[0].turnaround = proc[0].burst;
for (int i = 1; i < n; i++) {
proc[i].waiting = proc[i - 1].waiting + proc[i - 1].burst;
proc[i].turnaround = proc[i].waiting + proc[i].burst;
}
}
void displayProcesses(struct process proc[], int n) {
printf("Process ID\tBurst Time\tWaiting Time\tTurnaround Time\n");
for (int i = 0; i < n; i++) {
printf("%d\t\t%d\t\t%d\t\t%d\n", proc[i].pid, proc[i].burst, proc[i].waiting, proc[i].turnaround);
}
}
void calculateAverages(struct process proc[], int n, float *avg_waiting, float *avg_turnaround) {
int total_waiting = 0, total_turnaround = 0;
for (int i = 0; i < n; i++) {
total_waiting += proc[i].waiting;
total_turnaround += proc[i].turnaround;
}
*avg_waiting = (float)total_waiting / n;
*avg_turnaround = (float)total_turnaround / n;
}
int main() {
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
struct process proc[n];
printf("Enter the burst time for each process:\n");
for (int i = 0; i < n; i++) {
proc[i].pid = i + 1;
printf("Process %d: ", i + 1);
scanf("%d", &proc[i].burst);
}
calculateTimes(proc, n);
displayProcesses(proc, n);
float avg_waiting, avg_turnaround;
calculateAverages(proc, n, &avg_waiting, &avg_turnaround);
printf("\nAverage Waiting Time: %.2f\n", avg_waiting);
printf("Average Turnaround Time: %.2f\n", avg_turnaround);
return 0;
}
Output:
Enter the number of processes: 4
Enter the burst time for each process:
Process 1: 1
Process 2: 2
Process 3: 3
Process 4: 4
Process ID Burst Time Waiting Time Turnaround Time
1 1 0 1
2 2 1 3
3 3 3 6
4 4 6 10
Average Waiting Time: 2.50
Average Turnaround Time: 5.00



Comments