-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoundRobin.c
More file actions
88 lines (75 loc) · 2.86 KB
/
RoundRobin.c
File metadata and controls
88 lines (75 loc) · 2.86 KB
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
#include "RoundRobin.h"
void roundRobin(Process *processes[], unsigned int p_len, unsigned int quantum,
unsigned int *finishedTime, unsigned int *responseTime, int *ganttChart)
{
bool status_period_0[p_len];
bool status_period_1[p_len];
bool allocatedOnce[p_len];
bool allFinished = false;
for (int i = 0; i < p_len; i++) {
status_period_0[i] = false;
status_period_1[i] = false;
allocatedOnce[i] = false;
}
unsigned int timeElapsed = minArrivalTime(processes, p_len);
while (!allFinished) {
// Retain the earliest process in the ready queue.
unsigned int currentProcess = findFirstProcessInQueue(processes, p_len, status_period_0, status_period_1);
// Checking if we have some idle CPU time by comparing the time in which the last process gave up the CPU,
// And the new process has arrived.
if (getArrivalTime(processes[currentProcess]) > timeElapsed) {
for (unsigned int t = timeElapsed; t < getArrivalTime(processes[currentProcess]); t++) {
// '-1' identifies idle state.
ganttChart[t] = -1;
}
timeElapsed = getArrivalTime(processes[currentProcess]);
}
if (!allocatedOnce[currentProcess]) {
responseTime[currentProcess] = timeElapsed - getArrivalTime(processes[currentProcess]);
allocatedOnce[currentProcess] = true;
}
// The variable "period" identifies which burst time must happen.
bool period;
if (!status_period_0[currentProcess]) {
period = 0;
}
else if (status_period_0[currentProcess] && !status_period_1[currentProcess]) {
period = 1;
}
//else { exit(1); }
if (getBurstTime(processes[currentProcess], period) > quantum) {
for (unsigned int t = timeElapsed; t < timeElapsed + quantum; t++) {
ganttChart[t] = (int) currentProcess;
}
setBurstTime(processes[currentProcess], period,
getBurstTime(processes[currentProcess], period) - quantum);
timeElapsed += quantum;
setArrivalTime(processes[currentProcess], timeElapsed);
}
else {
for (unsigned int t = timeElapsed; t < timeElapsed + getBurstTime(processes[currentProcess], period); t++) {
ganttChart[t] = (int) currentProcess;
}
timeElapsed += getBurstTime(processes[currentProcess], period);
setBurstTime(processes[currentProcess], period, 0);
}
if (period == 0 && getBurstTime(processes[currentProcess], 0) == 0) {
status_period_0[currentProcess] = true;
setArrivalTime(processes[currentProcess], timeElapsed + getIOTime(processes[currentProcess]));
}
// After the second period is done, the process is finished, and we can store the time
// in which the process ended.
if (period == 1 && getBurstTime(processes[currentProcess], 1) == 0) {
status_period_1[currentProcess] = true;
finishedTime[currentProcess] = timeElapsed;
}
bool flag = false;
for (int i = 0; i < p_len; i++) {
if (!status_period_0[i] || !status_period_1[i]) {
flag = true;
break;
};
}
allFinished = !flag;
}
}