-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_management.cpp
More file actions
170 lines (160 loc) · 5.28 KB
/
process_management.cpp
File metadata and controls
170 lines (160 loc) · 5.28 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <iostream>
#include <vector>
#include <string>
enum ProcessState {
NEW,
RUNNING,
PAUSED,
STOPPED,
COMPLETED
};
std::string stateToString(ProcessState state) {
switch (state) {
case NEW: return "NEW";
case RUNNING: return "RUNNING";
case PAUSED: return "PAUSED";
case STOPPED: return "STOPPED";
case COMPLETED: return "COMPLETED";
default: return "UNKNOWN";
}
}
struct Process {
int pid;
std::string name;
int priority;
ProcessState state;
Process(int id, std::string n, int prio)
: pid(id), name(n), priority(prio), state(NEW) {}
};
std::vector<Process> processes;
int next_pid = 1;
void createProcess() {
std::string name;
int priority;
std::cout << "Enter process name: ";
std::cin >> name;
std::cout << "Enter priority (1-10): ";
while (!(std::cin >> priority) || priority < 1 || priority > 10) {
std::cout << "Please enter a value between 1 and 10: ";
std::cin.clear();
std::cin.ignore(10000, '\n');
}
processes.emplace_back(next_pid++, name, priority);
std::cout << "Process '" << name << "' created with PID " << (next_pid-1) << "\n";
}
bool start_process(int pid, std::string& error_message) {
for (auto& process : processes) {
if (process.pid == pid) {
if (process.state == NEW || process.state == PAUSED) {
process.state = RUNNING;
return true;
} else {
error_message = "Error: Process is already " + stateToString(process.state);
return false;
}
}
}
error_message = "Error: Process with PID " + std::to_string(pid) + " not found";
return false;
}
bool stop_process(int pid, std::string& error_message) {
for (auto& process : processes) {
if (process.pid == pid) {
if (process.state == RUNNING) {
process.state = STOPPED;
return true;
} else {
error_message = "Error: Cannot stop a process that is " + stateToString(process.state);
return false;
}
}
}
error_message = "Error: Process with PID " + std::to_string(pid) + " not found";
return false;
}
bool pause_process(int pid, std::string& error_message) {
for (auto& process : processes) {
if (process.pid == pid) {
if (process.state == RUNNING) {
process.state = PAUSED;
return true;
} else {
error_message = "Error: Cannot pause a process that is " + stateToString(process.state);
return false;
}
}
}
error_message = "Error: Process with PID " + std::to_string(pid) + " not found";
return false;
}
void listProcesses() {
if (processes.empty()) {
std::cout << "No processes created yet." << std::endl;
return;
}
std::cout << "\nCurrent Processes:" << std::endl;
for (const auto& p : processes) {
std::cout << "PID: " << p.pid << ", Name: " << p.name
<< ", Priority: " << p.priority << ", State: " << stateToString(p.state) << std::endl;
}
}
int main() {
while (true) {
std::cout << "\nProcess Management CLI" << std::endl;
std::cout << "1. Create Process" << std::endl;
std::cout << "2. List Processes" << std::endl;
std::cout << "3. Start Process" << std::endl;
std::cout << "4. Pause Process" << std::endl;
std::cout << "5. Stop Process" << std::endl;
std::cout << "6. Exit" << std::endl;
std::cout << "Enter your choice: ";
int choice;
std::cin >> choice;
if (std::cin.fail()) {
std::cin.clear();
std::cin.ignore(10000, '\n');
std::cout << "Invalid input. Please try again." << std::endl;
continue;
}
if (choice == 1) {
createProcess();
} else if (choice == 2) {
listProcesses();
} else if (choice == 3) {
int pid;
std::cout << "Enter PID to start: ";
std::cin >> pid;
std::string error;
if (start_process(pid, error)) {
std::cout << "Successfully started process with PID " << pid << std::endl;
} else {
std::cout << error << std::endl;
}
} else if (choice == 4) {
int pid;
std::cout << "Enter PID to pause: ";
std::cin >> pid;
std::string error;
if (pause_process(pid, error)) {
std::cout << "Successfully paused process with PID " << pid << std::endl;
} else {
std::cout << error << std::endl;
}
} else if (choice == 5) {
int pid;
std::cout << "Enter PID to stop: ";
std::cin >> pid;
std::string error;
if (stop_process(pid, error)) {
std::cout << "Successfully stopped process with PID " << pid << std::endl;
} else {
std::cout << error << std::endl;
}
} else if (choice == 6) {
return 0;
} else {
std::cout << "Invalid choice. Please try again." << std::endl;
}
}
return 0;
}