#include <stdio.h>
#define MAX_PROCESSES 100
int main() {
int n;
int burst[MAX_PROCESSES], wait[MAX_PROCESSES], turn[MAX_PROCESSES], arrival[MAX_PROCESSES], pid[MAX_PROCESSES];
int completion[MAX_PROCESSES], isCompleted[MAX_PROCESSES] = {0};
// Input number of processes
printf("Enter number of processes: ");
scanf("%d", &n);
// Input burst time and arrival time for each process
for (int i = 0; i < n; i++) {
pid[i] = i + 1;
printf("Enter arrival time for process %d: ", pid[i]);
scanf("%d", &arrival[i]);
printf("Enter burst time for process %d: ", pid[i]);
scanf("%d", &burst[i]);
}
int currentTime = 0, completed = 0;
while (completed != n) {
int shortest = -1;
int minBurst = 100000; // A large number to represent infinity
// Find process with shortest burst time among the processes that have arrived
for (int i = 0; i < n; i++) {
if (arrival[i] <= currentTime && !isCompleted[i] && burst[i] < minBurst) {
minBurst = burst[i];
shortest = i;
}
}
if (shortest == -1) {
currentTime++;
} else {
currentTime += burst[shortest];
completion[shortest] = currentTime;
turn[shortest] = completion[shortest] - arrival[shortest];
wait[shortest] = turn[shortest] - burst[shortest];
isCompleted[shortest] = 1;
completed++;
}
}
// Print process information
printf("PID\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time\tCompletion Time\n");
for (int i = 0; i < n; i++) {
printf("%d\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n", pid[i], arrival[i], burst[i], wait[i], turn[i], completion[i]);
}
// Calculate and print average waiting and turnaround times
float totalWait = 0, totalTurnaround = 0;
for (int i = 0; i < n; i++) {
totalWait += wait[i];
totalTurnaround += turn[i];
}
printf("Average Waiting Time: %.2f\n", totalWait / n);
printf("Average Turnaround Time: %.2f\n", totalTurnaround / n);
return 0;
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter