sjf
Sat May 11 2024 05:06:11 GMT+0000 (Coordinated Universal Time)
Saved by
@dsce
#include <stdio.h>
struct Process {
int pid;
int burst;
int turn;
int waiting;
int completion;
};
void calculate(struct Process pro[], int n) {
// Sort processes based on burst time
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (pro[j].burst > pro[j + 1].burst) {
struct Process temp = pro[j];
pro[j] = pro[j + 1];
pro[j + 1] = temp;
}
}
}
// Calculate waiting time, turnaround time, and completion time
pro[0].waiting = 0;
pro[0].completion = pro[0].burst;
for (int i = 1; i < n; i++) {
pro[i].waiting = pro[i - 1].completion;
pro[i].completion = pro[i].waiting + pro[i].burst;
pro[i].turn = pro[i].completion; // Turnaround time is the completion time itself
}
// Print process details
printf("Process\tBurst\tTurnaround\tWaiting\tCompletion\n");
int totwt = 0, tottat = 0;
for (int i = 0; i < n; i++) {
pro[i].turn -= 0; // Calculate turnaround time as completion time - burst time
printf("%d\t%d\t%d\t\t%d\t%d\n", pro[i].pid, pro[i].burst, pro[i].turn, pro[i].waiting, pro[i].completion);
tottat += pro[i].turn;
totwt += pro[i].waiting;
}
// Calculate average turnaround time and average waiting time
float avgtat = (float)tottat / n;
float avgwt = (float)totwt / n;
printf("Average Turnaround: %.2f\nAverage Waiting: %.2f\n", avgtat, avgwt);
}
int main() {
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
struct Process pro[n];
for (int i = 0; i < n; i++) {
pro[i].pid = i + 1;
printf("Enter burst time for process %d: ", pro[i].pid);
scanf("%d", &pro[i].burst);
}
calculate(pro, n);
return 0;
}
content_copyCOPY
Comments