-
Notifications
You must be signed in to change notification settings - Fork 0
/
Process.hpp
76 lines (60 loc) · 1.84 KB
/
Process.hpp
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
#ifndef PROCESS_H
#define PROCESS_H
#include <iostream>
#include <cstring>
#define LOW_PRIORITY 0
#define MEDIUM_PRIORITY 31
#define HIGH_PRIORITY 63
class Process {
private:
int pid;
char name[16];
int priority;
static int S_NEXT_PID;
public:
Process(char *name = NULL, int priority = MEDIUM_PRIORITY);
bool operator==(Process& other_process);
bool operator<=(Process& other_process);
bool operator<(Process& other_process);
bool operator>(Process& other_process);
friend std::ostream& operator<<(std::ostream& os, Process& process);
};
int Process::S_NEXT_PID = 0;
Process::Process(char *name, int priority) {
pid = Process::S_NEXT_PID++;
if (priority < 0) {
this->priority = 0;
} else if (priority > 63) {
this->priority = 63;
} else {
this->priority = priority;
}
if (name == NULL) {
sprintf(this->name, "Process %d", this->pid);
}
else if (strlen(name) > 15) {
std::cout << "Process name length should be less than 16 bytes on Process " << this->pid << std::endl;
sprintf(this->name, "Process %d", this->pid);
} else {
strncpy(this->name, name, 15);
}
}
bool Process::operator==(Process& other_process) {
return this->pid == other_process.pid;
}
bool Process::operator<=(Process& other_process) {
return this->priority <= other_process.priority;
}
bool Process::operator<(Process& other_process) {
return this->priority < other_process.priority;
}
bool Process::operator>(Process& other_process) {
return this->priority > other_process.priority;
}
std::ostream& operator<<(std::ostream& os, Process& process) {
os << "PROCESS NAME: " << process.name << std::endl;
os << "PROCESS ID: " << process.pid << std::endl;
os << "PROCESS PRIORITY: " << process.priority << std::endl;
return os;
}
#endif