-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathos2.cpp
More file actions
114 lines (98 loc) · 2.82 KB
/
Copy pathos2.cpp
File metadata and controls
114 lines (98 loc) · 2.82 KB
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
#include <iostream>
#include <list>
#include <cstdlib>
#include <ctime>
using namespace std;
#define free 0
#define ready 1
#define running 2
#define ts 2 /* time slice */
struct PCB {
int pid; /* 进程ID */
int pstate; /* 进程状态 */
char *pname; /* 映象名称 */
int ptime; /* 剩余运行时间 */
};
// 全局队列
list<PCB> freeQueue, readyQueue, runningQueue;
// 函数声明
void initFreeQueue(int N);
void createProcess(int n, int times[]);
void schedule();
void printQueues();
void initFreeQueue(int N) { // 初始化空闲队列
for (int i = 0; i < N; ++i) {
PCB pcb;
pcb.pid = i;
pcb.pstate = free;
pcb.pname = new char[3];
sprintf(pcb.pname, "P%d", i);
pcb.ptime = 0;
freeQueue.push_back(pcb);
}
}
void createProcess(int n, int times[]) { // 创建进程
for (int i = 0; i < n; ++i) {
PCB pcb = freeQueue.front();
freeQueue.pop_front();
pcb.pstate = ready;
pcb.ptime = times[i];
readyQueue.push_back(pcb);
}
}
void schedule() { // 调度
if (!runningQueue.empty()) {
PCB &runningPcb = runningQueue.front();
runningPcb.ptime -= ts;
if (runningPcb.ptime <= 0) {
cout << "Sched: " << runningPcb.pname << "(Running -> Finished, T: 0)" << endl;
runningPcb.pstate = free;
freeQueue.push_back(runningPcb);
runningQueue.pop_front();
} else {
cout << "Sched: " << runningPcb.pname << "(Running -> Ready, T: " << runningPcb.ptime << ")" << endl;
runningPcb.pstate = ready;
readyQueue.push_back(runningPcb);
runningQueue.pop_front();
}
}
if (!readyQueue.empty()) {
PCB &readyPcb = readyQueue.front();
readyQueue.pop_front();
cout << "Sched: " << readyPcb.pname << "(Ready -> Running, T: " << readyPcb.ptime << ")" << endl;
readyPcb.pstate = running;
runningQueue.push_back(readyPcb);
}
printQueues();
}
void printQueues() { // 打印队列
cout << "Running: ";
for (const PCB &pcb : runningQueue) {
cout << pcb.pname << "(" << pcb.ptime << ") ";
}
cout << endl;
cout << "Ready: ";
for (const PCB &pcb : readyQueue) {
cout << pcb.pname << "(" << pcb.ptime << ")->";
}
if (!readyQueue.empty()) cout << "END";
cout << endl << endl;
}
int main() {
int N, n;
cout << "Enter the number of PCBs in freeQueue: ";
cin >> N;
initFreeQueue(N);
cout << "Enter the number of processes to create: ";
cin >> n;
int times[n];
for (int i = 0; i < n; ++i) {
cin >> times[i];
}
createProcess(n, times);
while (!readyQueue.empty() || !runningQueue.empty()) {
schedule();
}
cout << "All processes finished." << endl;
return 0;
}