-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_schedule.cpp
147 lines (128 loc) · 2.65 KB
/
process_schedule.cpp
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
#include <iostream>
#include <string>
#include <algorithm>
#include <cstdio>
#include <list>
#include <vector>
using namespace std;
enum STATUS {
finish = 1,
working = 2,
ready = 3
};
struct process {
string process_name;
int need_time;
int cpu_time;
int priority;
STATUS status;
bool started;
process(
string _name,
int _need_time,
int _cpu_time,
int _priority,
STATUS _status
):
process_name(_name),
need_time(_need_time),
cpu_time(_cpu_time),
priority(_priority),
status(_status){
started = false;
}
friend ostream& operator << (ostream& os,const process& p) {
os << p.process_name << "\t" << p.cpu_time << "\t\t" << p.need_time << "\t\t" << p.priority << "\t\t" ;
switch (p.status) {
case 1:
os << "finish";
break;
case 2:
os << "working";
break;
case 3:
os << "ready";
break;
default:
break;
}
return os;
}
};
bool operator < (const process& p1,const process& p2) {
if(p1.status!=p2.status) {
return p1.status > p2.status;
}
else {
return p1.priority > p2.priority;
}
}
vector<process*> pcb_monitor;
list<process> pcb_list;
int cpu_time = 1;
void display_status() {
cout << "NAME\tCPUTIME\t\tNEEDTIME\tPRIORITY\tSTATE" << endl;
for(int i=0;i<pcb_monitor.size();i++) {
process &p = *pcb_monitor[i];
if(p.status==working) {
printf("\033[32m%s\t%d\t\t%d\t\t%d\t\tworking \n\033[0m",p.process_name.c_str(),p.cpu_time,p.need_time,p.priority);
}
else cout << *pcb_monitor[i] << endl;
}
cout << endl;
}
void excute() {
cout << "CPUTIME:" << cpu_time << endl;
process& p = *pcb_list.begin();
p.priority--;
p.need_time--;
p.status = working;
p.started = true;
list<process>::iterator itor;
itor = pcb_list.begin();
while(itor!=pcb_list.end()) {
process &p = *itor;
if(p.status!=finish && p.started) p.cpu_time++;
itor++;
}
cpu_time++;
display_status();
if(p.need_time==0) {
p.status = finish;
pcb_list.sort();
}
else {
pcb_list.sort();
p.status = ready;
}
}
int main(int argc, char *argv[]) {
process p1("P1",2,0,1,ready);
process p2("P2",3,0,5,ready);
process p3("P3",1,0,3,ready);
process p4("P4",2,0,4,ready);
process p5("P5",4,0,2,ready);
pcb_list.push_back(p1);
pcb_list.push_back(p2);
pcb_list.push_back(p3);
pcb_list.push_back(p4);
pcb_list.push_back(p5);;
list<process>::iterator itor;
itor = pcb_list.begin();
while(itor!=pcb_list.end()) {
pcb_monitor.push_back(&*itor);
itor++;
}
cout << "CPUTIME:" << 0 << endl;
display_status();
pcb_list.sort();
//init
while ((*pcb_list.begin()).status!=finish) {
excute();
};
//excute
cout << "CPUTIME:" << cpu_time << endl;
display_status();
//stop
return 0;
}