-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathos.cpp
More file actions
289 lines (255 loc) · 8.73 KB
/
os.cpp
File metadata and controls
289 lines (255 loc) · 8.73 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <exception>
#include <fstream>
#include <string>
#include <thread>
#include <sys/wait.h>
#include <queue>
#include <semaphore.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <sys/syscall.h>
#define MY_SYSCALL_NUMBER 399
using namespace std;
// ANSI color codes
#define RESET "\033[0m"
#define RED "\033[31m"
#define GREEN "\033[32m"
#define YELLOW "\033[33m"
#define BLUE "\033[34m"
#define MAGENTA "\033[35m"
#define CYAN "\033[36m"
#define WHITE "\033[37m"
#define BOLDWHITE "\033[1m\033[37m"
struct cmd {
const char* command;
int process;
};
int cores;
int hard_drive;
int *process_on_core;
long long int ram;
int ram_check;
pthread_mutex_t mutex_hp;
pthread_mutex_t mutex_lp;
int process_size[17] = {60,1,1,1,1,3,7,2,4,2,7,2,2,3,1,1};
sem_t my_semaphore;
void* execute(void* comm) {
struct cmd *input = reinterpret_cast<struct cmd*>(comm);
if (process_size[input->process] > ram) {
cerr << RED << "Failed to execute command: " << input->command << ", Error: Not enough available RAM" << RESET << endl;
return NULL;
}
pid_t pid = fork();
if (pid == 0) {
int x = -1;
int p = 16 / cores;
for (int i = 0; i < cores; i++) {
if (process_on_core[i] <= p) {
process_on_core[i]++;
x = i;
break;
}
}
if (x == -1) {
cerr << RED << " SYSTEM CRASH..... PROCESSES OVERLOADED\n NOT ENOUGH CORES" << RESET << endl;
return NULL;
} else {
int ret = execlp("gnome-terminal", "gnome-terminal", "--", input->command, NULL);
if (ret == -1) {
cerr << RED << "Failed to execute command: " << input->command << ", Error: " << strerror(errno) << RESET << endl;
} else {
ram += process_size[input->process];
}
}
} else if (pid < 0) {
cerr << RED << "Failed to execute command: " << input->command << ", Error: " << strerror(errno) << RESET << endl;
return NULL;
} else {
wait(NULL);
if (input->process == 17) {
int value;
sem_getvalue(&my_semaphore, &value);
cout << CYAN << "Semaphore before post: " << value << RESET << endl;
sem_post(&my_semaphore);
sem_getvalue(&my_semaphore, &value);
cout << CYAN << "Semaphore after post: " << value << RESET << endl;
}
ram -= process_size[input->process];
}
return NULL;
}
void switch_to_kernel_mode() {
system("clear");
cout << YELLOW << "\n\n----- IN KERNEL MODE ------\n\n" << RESET;
sleep(2);
pthread_mutex_destroy(&mutex_hp);
pthread_mutex_destroy(&mutex_lp);
delete[] process_on_core;
process_on_core = NULL;
sem_destroy(&my_semaphore);
syscall(MY_SYSCALL_NUMBER);
}
void* func(void* arg) {
pthread_t tid[17];
int n;
system("/home/Desktop/OS-Proj/s.sh");
queue<int> highPriority;
queue<int> lowPriority;
sem_init(&my_semaphore, 0, 1);
int counter = 0;
key_t key = ftok(".", 'R');
int shm_id = shmget(key, 1024, 0666 | IPC_CREAT);
if (shm_id == -1) {
perror("shmget failed");
exit(EXIT_FAILURE);
}
int *shared_memory = (int *)shmat(shm_id, NULL, 0);
if (shared_memory == (int *)-1) {
perror("shmat failed");
exit(EXIT_FAILURE);
}
while (true) {
cout << YELLOW
<< "\n===================== SMASH OS CLI =====================\n"
<< "Press 1 for TIC TAC TOE\n"
<< "Press 2 to make file\n"
<< "Press 3 to move file\n"
<< "Press 4 to copy file content\n"
<< "Press 5 to delete a file\n"
<< "Press 6 to read a file\n"
<< "Press 7 to play battleship game\n"
<< "Press 8 to play music\n"
<< "Press 9 to open calendar\n"
<< "Press 10 to open calculator\n"
<< "Press 11 to play pacman game\n"
<< "Press 12 to sort numbers\n"
<< "Press 13 for binary to decimal converter\n"
<< "Press 14 to open stopwatch\n"
<< "Press 15 to make directory\n"
<< "Press 16 to remove directory\n"
<< "Press 17 to edit a file\n"
<< "Press 18 to shutdown\n"
<< "========================================================\n"
<< RESET;
cin >> n;
if (n == 18) {
system("clear");
cout << RED << "\n\n----------- Switching to kernel mode to free RAM --------" << RESET << endl;
sleep(2);
switch_to_kernel_mode();
sleep(5);
system("pkill -TERM -f gnome-terminal");
break;
}
if (n == 1 || n == 7 || n == 11) {
pthread_mutex_lock(&mutex_hp);
highPriority.push(n);
pthread_mutex_unlock(&mutex_hp);
} else {
pthread_mutex_lock(&mutex_lp);
lowPriority.push(n);
pthread_mutex_unlock(&mutex_lp);
}
while (!highPriority.empty()) {
pthread_mutex_lock(&mutex_hp);
n = highPriority.front();
highPriority.pop();
pthread_mutex_unlock(&mutex_hp);
struct cmd *input = new cmd;
switch (n) {
case 1: input->command = "./tictactoe"; break;
case 7: input->command = "./battleship"; break;
case 11: input->command = "./pacman"; break;
}
input->process = n;
pthread_create(&tid[counter++], NULL, execute, (void*)input);
}
while (!lowPriority.empty()) {
pthread_mutex_lock(&mutex_lp);
n = lowPriority.front();
lowPriority.pop();
pthread_mutex_unlock(&mutex_lp);
struct cmd *input = new cmd;
switch (n) {
case 2:
case 4:
case 5:
*shared_memory = hard_drive;
break;
}
switch (n) {
case 2: input->command = "./makefile"; break;
case 3: input->command = "./movefile"; break;
case 4: input->command = "./copyfile"; break;
case 5: input->command = "./deletefile"; break;
case 6: input->command = "./readfile"; break;
case 8: input->command = "./music"; break;
case 9: input->command = "./calendar"; break;
case 10: input->command = "./cal"; break;
case 12: input->command = "./bubblesort"; break;
case 13: input->command = "./converter"; break;
case 14: input->command = "./stopwatch"; break;
case 15: input->command = "./mdir"; break;
case 16: input->command = "./rdir"; break;
case 17:
sem_wait(&my_semaphore);
input->command = "./my_program";
break;
}
input->process = n;
pthread_create(&tid[counter++], NULL, execute, (void*)input);
}
}
for (int i = 0; i < 11; i++) {
pthread_join(tid[i], NULL);
}
shmdt(shared_memory);
shmctl(shm_id, IPC_RMID, NULL);
pthread_exit(NULL);
}
int main(int argc, char* argv[]) {
if (argc != 4) {
cout << RED << " NOT ENOUGH ARGUMENTS \nERROR DETAIL: 3 arguments required" << RESET << endl;
return 1;
}
pthread_mutex_init(&mutex_hp, NULL);
pthread_mutex_init(&mutex_lp, NULL);
ram = stoi(argv[1]) * 1024 * 1024; // GB to KB
hard_drive = stoi(argv[2]);
cores = stoi(argv[3]);
ram_check = ram;
process_on_core = new int[cores];
fill(process_on_core, process_on_core + cores, 0);
system("clear");
cout << BOLDWHITE;
cout << " _____ __ __ _ ____ _ _ \n";
cout << " / ___/ | \\/ | / \\ / ___|| | | | \n";
cout << " \\___ \\ | |\\/| | / _ \\ \\___ \\| |_| | \n";
cout << " ___) || | | |/ ___ \\ ___) | _ | \n";
cout << " |____/ |_| |_/_/ \\_\\____/|_| |_| \n";
cout << RESET << endl;
cout << "\n PLEASE WAIT, LOADING..." << endl;
sleep(2);
system("clear");
int n;
cout << YELLOW << "\n\n\t\t\t Welcome back to SMASH OS\n\n" << RESET;
sleep(2);
cout << CYAN << "Press 1 to continue\nPress any other key to exit\n" << RESET;
cin >> n;
if (n == 1) {
pthread_t ptid;
pthread_create(&ptid, NULL, &func, NULL);
pthread_join(ptid, NULL);
}
pthread_exit(NULL);
}