forked from michaelschuff/Sum23OpSysProj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSJFprocess.h
173 lines (131 loc) · 3.33 KB
/
SJFprocess.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#ifndef SJFPROCESS_H
#define SJFPROCESS_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);
extern float LAMBDA;
extern float ALPHA;
//ID to char
extern char idtoc(int id );
class SJFProcess {
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 last_burst_time_using_cpu = 0;
int tempburst = 0;
SJFProcess(int id, int arrTime, int numBursts, bool 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;
if (LAMBDA != 0)
priority = ceil(1/LAMBDA);
else
priority = INT_MAX;
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();
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;
}
void incTimeUsingCPU(int t) {
time_using_cpu += t;
last_burst_time_using_cpu += t;
}
int nextFinish() {
return bursts.front();
}
int getPriority() {
return priority;
}
void updatePriority() {
priority = ceil(ALPHA * last_burst_time_using_cpu + ( 1.0 - ALPHA ) * priority);
// printf("last burst: %d\n",last_burst_time_using_cpu);
last_burst_time_using_cpu = 0;
}
private:
};
class SJFCompare {
public:
bool operator()(SJFProcess* l, SJFProcess* r) {
// int ladj = l->tempburst - l->nextFinish();
// int radj = r->tempburst - r->nextFinish();
int ladj = 0;
int radj = 0;
if (l->getPriority()-ladj == r->getPriority()-radj)
return l->ID > r->ID;
return l->getPriority() - ladj > r->getPriority() - radj;
}
};
class SJFArrivalTimeCompare {
public:
bool operator()(SJFProcess* l, SJFProcess* r) {
if (l->arrivalTime == r->arrivalTime)
return l->ID > r->ID;
return l->arrivalTime > r->arrivalTime;
}
};
class SJFIOBurstTimeCompare {
public:
bool operator()(SJFProcess* l, SJFProcess* r) {
if (l->nextFinish() == r->nextFinish())
return l->ID > r->ID;
return l->nextFinish() > r->nextFinish();
}
};
#endif