forked from michaelschuff/Sum23OpSysProj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRRprocess.h
150 lines (118 loc) · 2.91 KB
/
RRprocess.h
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#ifndef RRPROCESS_H
#define RRPROCESS_H
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string>
#include <sys/types.h>
#include <sys/wait.h>
#include <ctype.h>
#include <math.h>
#include <queue>
#include <stdlib.h>
#include <list>
#include <utility>
#include <iostream>
#include <vector>
using namespace std;
extern float next_exp(int);
//ID to char
extern char idtoc(int id );
class RRProcess {
public:
int ID;
int arrivalTime;
list<int> bursts;
bool arrived = false;
bool isCPUBound = false;
int priority = 0;
int completedCPUBursts = 0;
int completedIOBursts = 0;
int totalCPUBursts;
int time_using_cpu = 0;
int total_turnaround_time = 0;
int total_wait_time = 0;
int tempburst = 0;
bool got_preempted = false;
RRProcess(int id, int arrTime, int numBursts, int CPUBound) {
ID = id;
arrivalTime = arrTime;
totalCPUBursts = numBursts;
for(int j=0;j<numBursts;j++){
int x = next_exp(1) * (CPUBound ? 4 : 1);
bursts.push_back(x);
if (j!=numBursts-1)
bursts.push_back(next_exp(1)*10 / (CPUBound ? 8 : 1));
}
isCPUBound = CPUBound;
tempburst = bursts.front();
}
void elapseTime(int t, int flag) {
if (arrived) {
bursts.front() -= t;
if (bursts.size() != 0)
if (bursts.front() == 0 &&
((flag == 0 && completedCPUBursts == completedIOBursts) ||
(flag == 3 && completedCPUBursts > completedIOBursts))) {
bursts.pop_front();
tempburst = bursts.front();
if (completedCPUBursts > completedIOBursts) {
completedIOBursts++;
} else {
completedCPUBursts++;
}
}
} else {
arrivalTime -= t;
if (arrivalTime == 0) {
arrived = true;
}
}
}
void elapseTurnaroundTime(int t) {
total_turnaround_time+=t;
}
void elapseWaitTime(int t) {
total_wait_time+=t;
}
bool shouldTerminate() {
return completedCPUBursts == completedIOBursts + 1 && completedCPUBursts == totalCPUBursts;
}
int nextFinish() {
return bursts.front();
}
void incNextFinish(int t) {
bursts.front() += t;
}
int getPriority() {
return priority;
}
private:
};
class RRCompare {
public:
bool operator()(RRProcess* l, RRProcess* r) {
// int ladj = l->tempburst - l->nextFinish();
// int radj = r->tempburst - r->nextFinish();
// if (l->getPriority()-ladj == r->getPriority()-radj)
// return l->ID > r->ID;
return l->getPriority() > r->getPriority();
}
};
class RRArrivalTimeCompare {
public:
bool operator()(RRProcess* l, RRProcess* r) {
if (l->arrivalTime == r->arrivalTime)
return l->ID > r->ID;
return l->arrivalTime > r->arrivalTime;
}
};
class RRIOBurstTimeCompare {
public:
bool operator()(RRProcess* l, RRProcess* r) {
if (l->nextFinish() == r->nextFinish())
return l->ID > r->ID;
return l->nextFinish() > r->nextFinish();
}
};
#endif