-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCPU Scheduling.c
110 lines (104 loc) · 3.25 KB
/
CPU Scheduling.c
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <stdio.h>
#include <stdlib.h>
int tat = 0, wt = 0;
struct Process {
int pid, at, bt, ct, tat, wt;
_Bool done;
};
void sort(struct Process *p, int n) {
struct Process temp;
for (int i = 1; i < n; i++) {
temp = p[i];
int j = i - 1;
while (j >= 0 && p[j].bt > temp.bt) {
p[j + 1] = p[j];
j--;
}
p[j + 1] = temp;
}
}
void sjf (struct Process *processes, int n) {
int e = 0, doneCount = 0, found, time = 0;
sort(processes, n);
while (doneCount != n) {
found = 0;
for (int i = 0; i < n; i++) {
if (processes[i].at <= time && !processes[i].done) {
e = i;
found = 1;
break;
}
}
if (!found) {
time++;
continue;
}
time += processes[e].bt;
processes[e].ct = time;
processes[e].tat = processes[e].ct - processes[e].at;
processes[e].wt = processes[e].tat - processes[e].bt;
processes[e].done = 1;
doneCount++;
tat += processes[e].tat;
wt += processes[e].wt;
}
}
void roundRobin (struct Process *processes, int n) {
int quantum, e = 0, doneCount = 0, time = 0, rem[n], found;
printf("\nEnter time quantum: ");
scanf("%d", &quantum);
for (int i = 0; i < n; i++)
rem[i] = processes[i].bt;
while (doneCount != n) {
found = 0;
for (int i = 0; i < n; i++) {
if (processes[i].at <= time && rem[i] > 0) {
found = 1;
if (rem[i] <= quantum) {
time += rem[i];
rem[i] = 0;
processes[i].done = 1;
} else {
rem[i] -= quantum;
time += quantum;
}
if (!rem[i] && processes[i].done) {
processes[i].ct = time;
processes[i].tat = processes[i].ct - processes[i].at;
processes[i].wt = processes[i].tat - processes[i].bt;
doneCount++;
tat += processes[i].tat;
wt += processes[i].wt;
}
}
if (!found)
time++;
}
}
}
int main() {
int n, choice;
printf("Enter the number of processes: ");
scanf("%d", &n);
struct Process p[n];
for (int i = 0; i < n; i++) {
printf("Enter the arrival time and burst time of process %d: ", i + 1);
scanf("%d %d", &p[i].at, &p[i].bt);
p[i].pid = i + 1;
p[i].done = 0;
}
printf("\nSchedule to be used:\n1. SJF\n2. Round Robin\nEnter your choice: ");
scanf("%d", &choice);
if (choice == 1) sjf(p, n);
else if (choice == 2) roundRobin(p, n);
else {
printf("Invalid choice!");
exit(0);
}
printf("\nPID\tAT\tBT\tCT\tTAT\tWT");
for (int i = 0; i < n; i++)
printf("\n%d\t%d\t%d\t%d\t%d\t%d", p[i].pid, p[i].at, p[i].bt, p[i].ct, p[i].tat, p[i].wt);
printf("\nAverage Turnaround Time: %.2f", (float) tat / n);
printf("\nAverage Waiting Time: %.2f", (float) wt / n);
return 0;
}