#include <stdio.h>
#define MAX_PROCESSES 100
#define INF 100000 // A large number to represent infinity
int main() {
int n;
int pid[MAX_PROCESSES], arrival[MAX_PROCESSES], burst[MAX_PROCESSES], remaining[MAX_PROCESSES];
int wait[MAX_PROCESSES], turn[MAX_PROCESSES], completion[MAX_PROCESSES];
// Input number of processes
printf("Enter number of processes: ");
scanf("%d", &n);
// Input arrival time and burst 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]);
remaining[i] = burst[i]; // Initially, remaining time is equal to burst time
}
int currentTime = 0, completed = 0;
int shortest, minRemaining;
while (completed != n) {
shortest = -1;
minRemaining = INF;
// Find the process with the shortest remaining time among the processes that have arrived
for (int i = 0; i < n; i++) {
if (arrival[i] <= currentTime && remaining[i] > 0 && remaining[i] < minRemaining) {
minRemaining = remaining[i];
shortest = i;
}
}
if (shortest == -1) {
currentTime++;
} else {
remaining[shortest]--;
currentTime++;
if (remaining[shortest] == 0) {
completed++;
completion[shortest] = currentTime;
turn[shortest] = completion[shortest] - arrival[shortest];
wait[shortest] = turn[shortest] - burst[shortest];
}
}
}
// 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