-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
601 lines (554 loc) · 17 KB
/
main.cpp
File metadata and controls
601 lines (554 loc) · 17 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
#include <iostream>
#include <thread>
#include <chrono>
#include <mutex>
#include <pthread.h>
#include <unistd.h>
#include <sys/wait.h>
#include <fstream>
#include <vector>
#include <algorithm>
#include <unistd.h>
#include <dirent.h>
#include <sys/types.h>
#include <signal.h>
#include <string.h>
using namespace std;
double ram = 0, cores = 0, storage = 0;
mutex mtx;
struct Process {
int pid;
double arrival_time;
double priority;
double burst_time;
};
bool compare_arrival_time(const Process& a, const Process& b) {
return a.arrival_time < b.arrival_time;
}
bool compare_priority(const Process& a, const Process& b) {
return a.priority < b.priority;
}
void* calculator(void* p) {
system("gnome-terminal --disable-factory -- ./calculator"); // Replace "calc" with the name of your calculator program executable
return NULL;
}
void* calendar(void* p) {
system("gnome-terminal --disable-factory -- ./calender"); // Replace "calendar" with the name of your calendar program executable
return NULL;
}
void* clock(void* p) {
system("gnome-terminal --disable-factory -- ./clock"); // Replace "clock" with the name of your clock program executable
return NULL;
}
void* copyFile(void* p) {
system("gnome-terminal --disable-factory -- ./copyFile"); // Replace "copyFile" with the name of your copyFile program executable
return NULL;
}
void* createFile(void* p) {
system("gnome-terminal --disable-factory -- ./createFile"); // Replace "createFile" with the name of your createFile program executable
return NULL;
}
void* deletefile(void* p) {
system("gnome-terminal --disable-factory -- ./deletefile"); // Replace "Deletefile" with the name of your Deletefile program executable
return NULL;
}
void* Fileproperties(void* p) {
system("gnome-terminal --disable-factory -- ./Fileproperties"); // Replace "Fileproperties" with the name of your Fileproperties program executable
return NULL;
}
void* moveFile(void* p) {
system("gnome-terminal --disable-factory -- ./moveFile"); // Replace "moveFile" with the name of your moveFile program executable
return NULL;
}
void* notepad(void* p) {
system("gnome-terminal --disable-factory -- ./notepad"); // Replace "notepad" with the name of your notepad program executable
return NULL;
}
void* rename(void* p) {
system("gnome-terminal --disable-factory -- ./rename"); // Replace "rename" with the name of your rename program executable
return NULL;
}
void* song(void* p) {
system("gnome-terminal --disable-factory -- ./song"); // Replace "song" with the name of your song program executable
return NULL;
}
void* tictactoe(void* p) {
system("gnome-terminal --disable-factory -- ./tictactoe"); // Replace "tictactoe" with the name of your tictactoe program executable
return NULL;
}
void* towerOfHonoi(void* p) {
system("gnome-terminal --disable-factory -- ./towerOfHonoi"); // Replace "towerOfHonoi" with the name of your towerOfHonoi program executable
return NULL;
}
void* video(void* p) {
system("gnome-terminal --disable-factory -- ./video"); // Replace "video" with the name of your video program executable
return NULL;
}
void dis() {
DIR* dir = opendir("/proc");
if (!dir) {
cerr << "Failed to open directory /proc.\n";
return;
}
struct dirent* ent;
vector<pid_t> pids;
while ((ent = readdir(dir)) != nullptr) {
if (ent->d_type != DT_DIR) continue;
const string pid_str = ent->d_name;
if (pid_str.find_first_not_of("0123456789") != string::npos) continue;
const pid_t pid = stoi(pid_str);
char cmdline_path[64];
sprintf(cmdline_path, "/proc/%d/cmdline", pid);
FILE* cmdline_file = fopen(cmdline_path, "r");
if (!cmdline_file) continue;
char cmdline[1024];
const size_t len = fread(cmdline, 1, sizeof(cmdline), cmdline_file);
fclose(cmdline_file);
if (len == 0) continue;
cmdline[len] = '\0';
if (strstr(cmdline, "./") == cmdline) {
pids.push_back(pid);
cout << pid << " " << cmdline << endl;
}
}
closedir(dir);
if (pids.empty()) {
cout << "No processes found.\n";
return;
}
int pid;
cout << "Enter the PID of the process you want to terminate: ";
cin >> pid;
int ret = kill(pid, SIGKILL);
if (ret == -1) {
perror("Failed to send signal to process");
return;
}
cout << "Signal sent to process.\n";
}
void display_res() {
int arr2[9];
for(int i=0;i<9;i++)
{
arr2[i]=0;
}
int p=0;
DIR* dir = opendir("/proc");
if (!dir) {
cerr << "Failed to open directory /proc.\n";
return;
}
struct dirent* ent;
vector<pid_t> pids;
while ((ent = readdir(dir)) != nullptr) {
if (ent->d_type != DT_DIR) continue;
const string pid_str = ent->d_name;
if (pid_str.find_first_not_of("0123456789") != string::npos) continue;
const pid_t pid = stoi(pid_str);
char cmdline_path[64];
sprintf(cmdline_path, "/proc/%d/cmdline", pid);
FILE* cmdline_file = fopen(cmdline_path, "r");
if (!cmdline_file) continue;
char cmdline[1024];
const size_t len = fread(cmdline, 1, sizeof(cmdline), cmdline_file);
fclose(cmdline_file);
if (len == 0) continue;
cmdline[len] = '\0';
if (strstr(cmdline, "./") == cmdline) {
pids.push_back(pid);
arr2[p]=pid;
p++;
}
}
closedir(dir);
if((ram - 0.5*p) <= 0)
{
cout<<"No More Memmory\n Delete Some process\n";
kill(arr2[1], SIGKILL);
}
cout << "RAM : " << ram - 0.5*p << endl;
//cout << "Cores : " << cores << endl;
cout << "Storage : " << storage - 0.10*p << endl;
}
int main() {
int choice;
int hightime = 5;
pthread_t calc_thread, calendar_thread,clock_thread,copyFile_thread,createFile_thread,deletefile_thread,Fileproperties_thread,moveFile_thread,notepad_thread,rename_thread,song_thread,video_thread,tictactoe_thread,towerOfHonoi_thread;
cout << "Enter RAM: ";
cin >> ram;
cout << "Enter storage: ";
cin >> storage;
// Initialize the queues
int high[9];
for (int i = 0; i < 9; i++)
{
high[i] = 0;
}
while (choice != -1) {
cout << "Choose an option:\n";
cout << "01. Calculator\n";
cout << "02. Calendar\n";
cout << "03. Clock\n";
cout << "04. Copy File\n";
cout << "05. Create File\n";
cout << "06. Delete File\n";
cout << "07. File Properties\n";
cout << "08. Move File\n";
cout << "09. Notepad\n";
cout << "10. Rename File\n";
cout << "11. Play Song\n";
cout << "12. Play Video\n";
cout << "13. Run Tic Tac Toe\n";
cout << "14. Run Tower of Honoi\n";
cout << "15. Run Game 3\n";
cout << "16. Run Game 4\n";
cout << "17. Dis Function\n";
cout << "18. Disp_res Function\n";
cout << "Default. Terminate Functions\n";
cout << "-1. Exit\n";
cin >> choice;
switch (choice) {
case 1:
{
pid_t pid = fork();
if (pid == 0) {
// Child process
pthread_create(&calc_thread, NULL, calculator, NULL);
//pthread_join(calc_thread, NULL);
exit(0);
}
else if (pid > 0) {
// Parent process
wait(NULL);
display_res();
break;
}
else {
cerr << "Error creating process.\n";
}
break;
}
case 2:
{
pid_t pid = fork();
if (pid == 0) {
// Child process
pthread_create(&calendar_thread, NULL, calendar, NULL);
// pthread_join(calendar_thread, NULL);
exit(0);
}
else if (pid > 0) {
// Parent process
wait(NULL);
break;
}
else {
cerr << "Error creating process.\n";
}
break;
}
case 3:
{
pid_t pid = fork();
if (pid == 0) {
// Child process
pthread_create(&clock_thread, NULL, clock, NULL);
// pthread_join(clock_thread, NULL);
exit(0);
}
else if (pid > 0) {
// Parent process
wait(NULL);
break;
}
else {
cerr << "Error creating process.\n";
}
break;
}
case 4:
{
pid_t pid = fork();
if (pid == 0) {
// Child process
pthread_create(©File_thread, NULL, copyFile, NULL);
// pthread_join(copyFile_thread, NULL);
exit(0);
}
else if (pid > 0) {
// Parent process
wait(NULL);
break;
}
else {
cerr << "Error creating process.\n";
}
break;
}
case 5:
{
pid_t pid = fork();
if (pid == 0) {
// Child process
pthread_create(&createFile_thread, NULL, createFile, NULL);
// pthread_join(createFile_thread, NULL);
exit(0);
}
else if (pid > 0) {
// Parent process
wait(NULL);
break;
}
else {
cerr << "Error creating process.\n";
}
break;
}
case 6:
{
pid_t pid = fork();
if (pid == 0) {
// Child process
pthread_create(&deletefile_thread, NULL, deletefile, NULL);
// pthread_join(deletefile_thread, NULL);
exit(0);
}
else if (pid > 0) {
// Parent process
wait(NULL);
break;
}
else {
cerr << "Error creating process.\n";
}
break;
}
case 7:
{
pid_t pid = fork();
if (pid == 0) {
// Child process
pthread_create(&Fileproperties_thread, NULL, Fileproperties, NULL);
// pthread_join(Fileproperties_thread, NULL);
exit(0);
}
else if (pid > 0) {
// Parent process
wait(NULL);
break;
}
else {
cerr << "Error creating process.\n";
}
break;
}
case 8:
{
pid_t pid = fork();
if (pid == 0) {
// Child process
pthread_create(&moveFile_thread, NULL, moveFile, NULL);
// pthread_join(moveFile_thread, NULL);
exit(0);
}
else if (pid > 0) {
// Parent process
wait(NULL);
break;
}
else {
cerr << "Error creating process.\n";
}
break;
}
case 9:
{
pid_t pid = fork();
if (pid == 0) {
// Child process
pthread_create(¬epad_thread, NULL, notepad, NULL);
// pthread_join(notepad_thread, NULL);
exit(0);
}
else if (pid > 0) {
// Parent process
wait(NULL);
break;
}
else {
cerr << "Error creating process.\n";
}
break;
}
case 10:
{
pid_t pid = fork();
if (pid == 0) {
// Child process
pthread_create(&rename_thread, NULL, rename, NULL);
// pthread_join(rename_thread, NULL);
exit(0);
}
else if (pid > 0) {
// Parent process
wait(NULL);
break;
}
else {
cerr << "Error creating process.\n";
}
break;
}
case 11:
{
pid_t pid = fork();
if (pid == 0) {
// Child process
pthread_create(&song_thread, NULL, song, NULL);
// pthread_join(song_thread, NULL);
exit(0);
}
else if (pid > 0) {
// Parent process
wait(NULL);
break;
}
else {
cerr << "Error creating process.\n";
}
break;
}
case 12:
{
pid_t pid = fork();
if (pid == 0) {
// Child process
pthread_create(&video_thread, NULL, video, NULL);
// pthread_join(video_thread, NULL);
exit(0);
}
else if (pid > 0) {
// Parent process
wait(NULL);
break;
}
else {
cerr << "Error creating process.\n";
}
break;
}
case 13:
{
pid_t pid = fork();
if (pid == 0) {
// Child process
pthread_create(&tictactoe_thread, NULL, tictactoe, NULL);
// pthread_join(tictactoe_thread, NULL);
exit(0);
}
else if (pid > 0) {
// Parent process
wait(NULL);
break;
}
else {
cerr << "Error creating process.\n";
}
break;
}
case 14:
{
pid_t pid = fork();
if (pid == 0) {
// Child process
pthread_create(&towerOfHonoi_thread, NULL, towerOfHonoi, NULL);
// pthread_join(towerOfHonoi_thread, NULL);
exit(0);
}
else if (pid > 0) {
// Parent process
wait(NULL);
break;
}
else {
cerr << "Error creating process.\n";
}
break;
}
case 17:
dis();
break;
case 18:
display_res();
break;
default:
{
//
DIR* dir = opendir("/proc");
if (!dir) {
cerr << "Failed to open directory /proc.\n";
break;
}
int arr2[9], p = 0;
for (int i = 0; i < 9; i++)
{
arr2[i] = 0;
}
struct dirent* ent;
vector<pid_t> pids;
while ((ent = readdir(dir)) != nullptr) {
if (ent->d_type != DT_DIR) continue;
const string pid_str = ent->d_name;
if (pid_str.find_first_not_of("0123456789") != string::npos) continue;
const pid_t pid = stoi(pid_str);
char cmdline_path[64];
sprintf(cmdline_path, "/proc/%d/cmdline", pid);
FILE* cmdline_file = fopen(cmdline_path, "r");
if (!cmdline_file) continue;
char cmdline[1024];
const size_t len = fread(cmdline, 1, sizeof(cmdline), cmdline_file);
fclose(cmdline_file);
if (len == 0) continue;
cmdline[len] = '\0';
if (strstr(cmdline, "./") == cmdline) {
pids.push_back(pid);
arr2[p] = pid;
p++;
cout << pid << " " << cmdline << endl;
}
}
cout << "a-------------" << endl;
for (int i = 0; i < p; i++)
{
cout << arr2[i] << endl;
}
cout << "a-------------" << endl;
for (int i = 0; i < 9; i++)
{
high[i] = arr2[i];
}
//
// Schedule the processes in the queues using different scheduling techniques on each level
// dis();
// Execute the processes in the order of the queues
int counth = 1;
while (counth < 9) {
int pid = high[counth];
cout << pid << endl;
string cmd = "xdotool search --pid " + to_string(pid) + " --all windowactivate";
system(cmd.c_str());
sleep(5);
int ret = kill(pid, SIGKILL);
if (ret == -1) {
perror("Failed to send signal to process");
}
else {
cout << "Terminated process " << high[counth] << " with high priority.\n";
}
counth++;
}
}
}
}
return 0;
}