-
Notifications
You must be signed in to change notification settings - Fork 0
/
RR.c
98 lines (80 loc) · 1.97 KB
/
RR.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
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sched.h>
#include <sys/syscall.h>
#include <sys/wait.h>
#include <stdbool.h>
#include <sys/time.h>
#include <sys/resource.h>
#include "policy.h"
#define QUANTUM 500
void RR(int procNum) {
initQueue();
sortReady(procNum);
#ifdef DEBUG
for(int i = 0; i < procNum; i++)
printf("--%s %d %d\n", proc[i]->name, proc[i]->ready, proc[i]->exec);
#endif
int createdNum = 0;
int accumQuantum = 0;
for (int time = 0; finishNum < procNum; ++time) {
while (createdNum < procNum && time == proc[createdNum]->ready) {
createChild(createdNum, LOW_PRIORITY);
inQueue(createdNum);
adjustHeadPriority();
createdNum++;
#ifdef DEBUG
printQueue();
#endif
}
if (runningID != -1) {
--(proc[runningID]->exec);
if(proc[runningID]->exec <= 0) {
waitpid(proc[runningID]->pid, NULL, 0);
#ifdef DEBUG
fprintf(stderr, "wait for %d success!\n", runningID);
#endif
++finishNum;
runningID = -1;
}
}
if (!emptyQueue() && runningID == -1) { // we can run new process in CPU
accumQuantum = 0;
runningID = deQueue();
setPriority(proc[runningID]->pid, HIGH_PRIORITY);
adjustHeadPriority();
#ifdef DEBUG
fprintf(stderr, "runningpid = %d\n", proc[runningID]->pid);
printQueue();
#endif
}
if (time != 0 && accumQuantum == QUANTUM) {
accumQuantum = 0;
if (runningID != -1) {
setPriority(proc[runningID]->pid, LOW_PRIORITY);
inQueue(runningID);
}
if (!emptyQueue()) {
runningID = deQueue();
setPriority(proc[runningID]->pid, HIGH_PRIORITY);
adjustHeadPriority();
}
#ifdef DEBUG
if (runningID != -1)
fprintf(stderr, "runningpid = %d\n", proc[runningID]->pid);
printQueue();
#endif
}
unitTime();
#ifdef DEBUG
if (time % 100 == 0)
fprintf(stderr, "[%d] time = %d, accumT = %d, finish = %d\n", getpid(), time, accumQuantum, finishNum);
#endif
if (runningID != -1)
++accumQuantum;
}
printInfo(procNum);
return;
}