#include <stdio.h>
int main() {
int n, i, j, min_bt, time = 0, completed = 0, sum_wait = 0, sum_turnaround = 0;
int at[10], bt[10], rt[10], p[10]; // Arrays for arrival times, burst times, remaining times, and process numbers
int wait[10], turnaround[10]; // Arrays for waiting times and turnaround times
float avg_wait, avg_turnaround;
printf("Enter the number of processes: ");
scanf("%d", &n);
// Input arrival times and burst times for each process
for (i = 0; i < n; i++) {
printf("Enter the arrival time and burst time for process %d: ", i + 1);
scanf("%d%d", &at[i], &bt[i]);
rt[i] = bt[i]; // Initialize remaining time as burst time
p[i] = i + 1; // Process numbers
}
// Initialize arrays for waiting and turnaround times
for (i = 0; i < n; i++) {
wait[i] = 0;
turnaround[i] = 0;
}
printf("\nProcess\tAT\tBT\tWT\tTAT\n");
while (completed != n) {
// Find process with minimum remaining time at each time unit
min_bt = 9999; // A large number to find the minimum burst time
int shortest = -1; // Index of the process with the shortest remaining time
for (i = 0; i < n; i++) {
if (at[i] <= time && rt[i] > 0 && rt[i] < min_bt) {
min_bt = rt[i];
shortest = i;
}
}
if (shortest == -1) {
// No process is currently available to execute
time++;
continue;
}
rt[shortest]--; // Decrement the remaining time of the shortest process
if (rt[shortest] == 0) {
// Process is completed
completed++;
int end_time = time + 1; // Time at which the process completes
turnaround[shortest] = end_time - at[shortest]; // Turnaround time
wait[shortest] = turnaround[shortest] - bt[shortest]; // Waiting time
sum_wait += wait[shortest];
sum_turnaround += turnaround[shortest];
printf("P[%d]\t%d\t%d\t%d\t%d\n", p[shortest], at[shortest], bt[shortest], wait[shortest], turnaround[shortest]);
}
time++; // Increment time
}
avg_wait = (float)sum_wait / (float)n;
avg_turnaround = (float)sum_turnaround / (float)n;
printf("Average waiting time is %.2f\n", avg_wait);
printf("Average turnaround time is %.2f\n", avg_turnaround);
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