-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.h
114 lines (103 loc) · 2.96 KB
/
process.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
#include <iostream>
#include <string>
#include <queue>
#include <fstream>
#include <ctime>
#include <iomanip>
using namespace std;
class Process
{
string name;
int atime; // arrival time
int ptime; // processing time
int pid;
int state;
string stateToString;
/**
*
* Sets pid to the fisrt available pid from pidStore
*
*/
void setpid()
{
for (int i = 0; i < 1000; ++i)
{
if (!pidStore[i])
{
pidStore[i] = 1;
pid = i + 100; // assuming that the first 100 pids are reserved for OS
break;
}
}
}
void setState(int state)
{
switch (state)
{
case 0:
this->state = state;
stateToString = "terminated";
pidStore[pid - 100] = 0;
break;
case 1:
this->state = state;
stateToString = "ready"; // assuming same as creating/new
break;
case 2:
this->state = state;
stateToString = "running";
break;
case 3:
this->state = state;
stateToString = "waiting";
break;
default:
throw std::invalid_argument("received invalid state number; valid numbers [0-3]");
break;
}
}
public:
bool static *pidStore;
void init (string name, int atime, int ptime)
{
this->name = name;
this->atime = atime;
this->ptime = ptime;
setpid();
//In Unix-like operating systems a process gets its PID right after creation
//Therefore, the PID sequence may differ from the actual order of execution
//Check linux/include/linux/pid.h and Wikipedia page on Process identifier
setState(1);
}
/**
*
* print all for debugging
*
*/
friend std::ostream &operator<<(std::ostream &os, const Process &process)
{
os <<"\t" <<right << setw(50) << setfill('_')<<"\n"
<<left << setw(25) << setfill(' ')
<< "\tName: " + process.name << "| PID: " << process.pid
<<endl<<left << setw(25) << setfill(' ')
<< "\tArrival time: " + to_string(process.atime) << "| Processing time: " << process.ptime
<<endl<<left << setw(25) << setfill(' ')
<< "\tpidStore: " + to_string((long long)process.pidStore) << "| Value: " << process.pidStore[pid-100]
<<endl<<right << setw(30) << setfill(' ')
<< "State: " + to_string(process.state) << " " << process.stateToString<<endl;
return os;
}
string getName(){
return this->name;
}
void getTime(int &atime,int &ptime){
atime=this->atime;
ptime=this->ptime;
}
~Process(){
setState(0); // terminated
}
friend struct CompareATime;
friend struct ComparePTime;
};
void scheduler(Process *ps, unsigned int len, int algo);