-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTaskManager.cpp
80 lines (78 loc) · 1.96 KB
/
TaskManager.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
#include"ProcessManager.h"
#include"MemoryManager.h"
#include"DeviceManager.h"
using namespace std;
class TaskManager {
private:
MemoryManager MM;
ProcessManager PM;
DeviceManager DM;
struct PCB* CPU;
double timeslice;
public:
TaskManager() {
CPU = NULL;
timeslice = 0;
}
void input(int PID, std::string PName, std::string UserID, int Priority, struct RunInfo PRunInfo ,int size) {
//输入进程并创建
PM.createProcess(PID, PName, UserID, Priority, PRunInfo,size);
}
void allocateMemory() {
//尝试为新建进程中的每一个进程分配一次内存
struct PCB* tryAllocPCB = NULL;
while ((tryAllocPCB = PM.getCreatedProcess(tryAllocPCB)) != NULL) {
if ((tryAllocPCB->PBlock = MM.alloc(tryAllocPCB->size)) != NULL) {
PM.popCreatedProcess(tryAllocPCB);
PM.putProcessReady(tryAllocPCB, true);
}
}
}
void run() {
//运行进程和设备
if (CPU == NULL) {
CPU = PM.dispatchProcess();
timeslice = 0;
}
if (CPU != NULL) {
struct interrupt intSemaphore = PM.runProcess(CPU);
vector<int>doneDeviceNo = DM.runDevice();
timeslice += 0.1;
if (intSemaphore.semaphore == 1) {
PM.putProcessObstruct(CPU, intSemaphore.DeviceNo);
DM.occupyDevice(intSemaphore.DeviceNo, intSemaphore.OccupyTime);
CPU = NULL;
}
else if (intSemaphore.semaphore == 2) {
MM.release(CPU->PBlock);
PM.deleteProcess(CPU);
CPU = NULL;
}
else {
if (timeslice == 1) {
PM.putProcessReady(CPU, false);
CPU = NULL;
}
}
for (int i = 0; i < doneDeviceNo.size(); i++) {
PM.popProcessObstruct(doneDeviceNo[i]);
}
}
}
~TaskManager() {
CPU = NULL;
}
};
int main() {
TaskManager TM;
RunInfo ri;
ri.RequireTime = 10;
ri.OccupyTime = 0;
for (int i = 0; i < DEVICENUM; i++) {
ri.DeviceRunInfo[0][i] = ri.DeviceRunInfo[1][i] = 0;
}
TM.input(2, "aaa", "xbj", 3, ri,100);
TM.allocateMemory();
system("pause");
return 0;
}