-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pcb.h
101 lines (81 loc) · 2.32 KB
/
Pcb.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
// Kevin Granados
#ifndef PCB_H
#define PCB_H
#include <string>
#include <vector>
constexpr int NO_PARENT{ 0 };
class PCB {
public:
int PID;
std::string state;
int programCounter;
std::vector<PCB> children;
int parentPID = NO_PARENT;
/**
Default constructor.
@post : A PCB object is created with PID set to 0 and state set to "New"
*/
PCB() : PID(0), state("New") {}
/**
Parameterized constructor.
@param : the id of the PCB (a int)
@post : A PCB object is created with the given id, state is set to "New"
*/
PCB(int id) : PID(id), state("New") {}
/**
@return : the children vector of the current PCB object
*/
std::vector<PCB>& getChildren() {
return children;
}
/**
@param : the id of the child process (a int)
@post : deletes the child process with the given id from the children vector
*/
void deleteChildProcess(int PID) {
for (size_t i = 0; i < children.size(); i++) {
if (children[i].PID == PID) {
children.erase(children.begin() + i);
break;
}
}
}
/**
@param : the id of the forked child process (a int)
@post : creates a new PCB object with the given id and adds it to the children vector
*/
PCB forkProcess(int newPID) {
PCB child = *this;
child.PID = newPID;
child.parentPID = this->PID;
child.state = "New";
children.push_back(child);
return child;
}
/**
@return : the id of the parent process
*/
int getParentID() const {
return parentPID;
}
/**
@return : the state of the PCB object
*/
std::string getState() const {
return state;
}
/**
@param : the child PID (a int)
@param : the new state of the PCB object (a string)
@post : sets the state of the PCB object to the given state
*/
void changeChildState(int childPID, const std::string newState) {
for (PCB &child : children) {
if (child.PID == childPID) {
child.state = newState;
break;
}
}
}
};
#endif