-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImplementation of SJF CPU scheduling algorithms..cpp
62 lines (49 loc) · 1.87 KB
/
Implementation of SJF CPU scheduling algorithms..cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <stdio.h>
struct Process {
int pid;
int burst_time;
};
void sortProcessesByBurstTime(struct Process processes[], int n) {
struct Process temp;
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (processes[j].burst_time > processes[j+1].burst_time) {
temp = processes[j];
processes[j] = processes[j+1];
processes[j+1] = temp;
}
}
}
}
void findWaitingTime(struct Process processes[], int n, int waiting_time[]) {
waiting_time[0] = 0;
for (int i = 1; i < n; i++) {
waiting_time[i] = processes[i-1].burst_time + waiting_time[i-1];
}
}
void findTurnaroundTime(struct Process processes[], int n, int waiting_time[], int turnaround_time[]) {
for (int i = 0; i < n; i++) {
turnaround_time[i] = processes[i].burst_time + waiting_time[i];
}
}
void findAverageTimes(struct Process processes[], int n) {
int waiting_time[n], turnaround_time[n];
int total_waiting_time = 0, total_turnaround_time = 0;
sortProcessesByBurstTime(processes, n);
findWaitingTime(processes, n, waiting_time);
findTurnaroundTime(processes, n, waiting_time, turnaround_time);
printf("Process\tBurst Time\tWaiting Time\tTurnaround Time\n");
for (int i = 0; i < n; i++) {
total_waiting_time += waiting_time[i];
total_turnaround_time += turnaround_time[i];
printf("%d\t%d\t\t%d\t\t%d\n", processes[i].pid, processes[i].burst_time, waiting_time[i], turnaround_time[i]);
}
printf("\nAverage Waiting Time: %.2f\n", (float)total_waiting_time / n);
printf("Average Turnaround Time: %.2f\n", (float)total_turnaround_time / n);
}
int main() {
struct Process processes[] = {{1, 6}, {2, 8}, {3, 7}, {4, 3}};
int n = sizeof(processes) / sizeof(processes[0]);
findAverageTimes(processes, n);
return 0;
}