forked from michaelschuff/Sum23OpSysProj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FCFSprocess.h
135 lines (108 loc) · 2.62 KB
/
FCFSprocess.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
#ifndef FCFSPROCESS_H
#define FCFSPROCESS_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 FCFSProcess {
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;
FCFSProcess(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();
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();
}
private:
};
class FCFSCompare {
public:
bool operator()(FCFSProcess* l, FCFSProcess* r) {
if (l->priority == r->priority)
return l->ID > r->ID;
return l->priority > r->priority;
}
};
class FCFSArrivalTimeCompare {
public:
bool operator()(FCFSProcess* l, FCFSProcess* r) {
if (l->arrivalTime == r->arrivalTime)
return l->ID > r->ID;
return l->arrivalTime > r->arrivalTime;
}
};
class FCFSIOBurstTimeCompare {
public:
bool operator()(FCFSProcess* l, FCFSProcess* r) {
if (l->nextFinish() == r->nextFinish())
return l->ID > r->ID;
return l->nextFinish() > r->nextFinish();
}
};
#endif