#include <stdio.h> int main() { int n, tq, time = 0, totalWait = 0, totalTurn = 0; printf("Enter the number of processes and time quantum: "); scanf("%d %d", &n, &tq); int bt[n], remain[n], wt[n], tat[n]; printf("Enter the burst times: "); for (int i = 0; i < n; i++) { scanf("%d", &bt[i]); remain[i] = bt[i]; wt[i] = 0; } while (1) { int done = 1; for (int i = 0; i < n; i++) { if (remain[i] > 0) { done = 0; if (remain[i] > tq) { time += tq; remain[i] -= tq; } else { time += remain[i]; wt[i] = time - bt[i]; remain[i] = 0; } } } if (done) break; } for (int i = 0; i < n; i++) { tat[i] = wt[i] + bt[i]; totalWait += wt[i]; totalTurn += tat[i]; printf("P%d %d %d %d\n", i + 1, bt[i], wt[i], tat[i]); } printf("Average Waiting Time: %.2f\n", (float)totalWait / n); printf("Average Turnaround Time: %.2f\n", (float)totalTurn / 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