-
Notifications
You must be signed in to change notification settings - Fork 0
/
simpleShell.c
590 lines (551 loc) · 18.6 KB
/
simpleShell.c
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
//header files
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <signal.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <semaphore.h>
//definitions
#define MAX_SIZE 50
#define MAX_HISTORY 75
#define MAX_WORDS 10
#define MAX_COMMANDS 5
//struct to store process info
struct Process{
int pid, priority;
bool submit,queue,completed; // flags
// submit: process have been submitted
// queue: process is in the scheduler's queue
// completed: indicates if process have been completed
char command[MAX_SIZE + 1]; //+1 to accomodate \n or \0
struct timeval start;
unsigned long execution_time, wait_time, vruntime;
};
//history struct used to store the history of process executions
struct history_struct {
int history_count,ncpu,tslice;
sem_t mutex; // semaphore
struct Process history[MAX_HISTORY];
};
//function declarations
static void sigint_handler(int signum);
static void sigchld_handler(int signum, siginfo_t *info, void *context);
void termination_report();
void shell_loop();
char* read_user_input();
int launch(char* command);
int create_process_and_run(char* command);
int create_child_process(char *command, int input_fd, int output_fd);
void start_time(struct timeval *start);
unsigned long end_time(struct timeval *start);
int submit_process(char *command);
//global variables
int shm_fd, scheduler_pid;
struct history_struct *process_table;
int main(int argc, char** argv){
if (argc != 3){
printf("Usage: %s <NCPU> <TIME_QUANTUM>\n",argv[0]);
exit(1);
}
// shared memory initialisation
// creating a new shared memory object using "shm_open"
shm_fd = shm_open("shm", O_CREAT|O_RDWR, 0666);
if (shm_fd == -1){
perror("shm_open");
exit(1);
}
// set desired size for shared memory segment using "ftruncate"
if (ftruncate(shm_fd, sizeof(struct history_struct)) == -1){
perror("ftruncate");
exit(1);
}
// map the shared memory into process's address space using "mmap"
process_table = mmap(NULL, sizeof(struct history_struct), PROT_READ|PROT_WRITE, MAP_SHARED, shm_fd,0);
if (process_table == MAP_FAILED){
perror("mmap");
exit(1);
}
process_table->history_count=0;
process_table->ncpu = atoi(argv[1]);
if (process_table->ncpu == 0){
printf("invalid argument for number of CPU\n");
exit(1);
}
process_table->tslice = atoi(argv[2]);
if (process_table->tslice == 0){
printf("invalid argument for time quantum\n");
exit(1);
}
// initialising a semaphore
if (sem_init(&process_table->mutex, 1, 1) == -1){
perror("sem_init");
exit(1);
}
printf("Initializing simple scheduler...\n");
// forking a child process for the scheduler
pid_t pid;
if ((pid= fork())<0){
printf("fork() failed.\n");
perror("fork");
exit(1);
}
if (pid == 0){
if (execvp("./scheduler",("./scheduler",NULL)) == -1) {
printf("Couldn't initiate scheduler.\n");
exit(1);
}
if (munmap(process_table, sizeof(struct history_struct)) < 0){
printf("Error unmapping\n");
perror("munmap");
exit(1);
}
if (close(shm_fd) == -1){
perror("close");
exit(1);
}
exit(0);
}
else{
scheduler_pid = pid;
}
//signal handling
//sigint handler
struct sigaction s_int, s_chld;
if (memset(&s_int, 0, sizeof(s_int)) == 0){
perror("memset");
exit(1);
}
s_int.sa_handler = sigint_handler;
if (sigaction(SIGINT, &s_int, NULL) == -1){
perror("sigaction");
exit(1);
}
if (memset(&s_chld, 0, sizeof(s_chld)) == 0){
perror("memset");
exit(1);
}
//sigchld handler
s_chld.sa_sigaction = sigchld_handler;
s_chld.sa_flags = SA_SIGINFO|SA_NOCLDSTOP|SA_RESTART;
if (sigaction(SIGCHLD, &s_chld, NULL) == -1){
perror("sigaction");
exit(1);
}
printf("Initializing simple shell...\n");
shell_loop();
printf("Exiting simple shell...\n");
termination_report();
// destroying the semaphore
if (sem_destroy(&process_table->mutex) == -1){
perror("shm_destroy");
exit(1);
}
// unmapping shared memory segment followed by a "close" call
if (munmap(process_table, sizeof(struct history_struct)) < 0){
printf("Error unmapping\n");
perror("munmap");
exit(1);
}
if (close(shm_fd) == -1){
perror("close");
exit(1);
}
// parent deletes the shared memory object by using "shm_unlink"
if (shm_unlink("shm") == -1){
perror("shm_unlink");
exit(1);
}
return 0;
}
//sigint handler
static void sigint_handler(int signum) {
if(signum == SIGINT) {
printf("\nCaught SIGINT signal for termination\n");
printf("Terminating simple scheduler...\n");
//send sigint to scheduler
if (kill(scheduler_pid, SIGINT) == -1){
perror("kill");
exit(1);
}
// clean up and program termination
printf("Exiting simple shell...\n");
termination_report();
if (sem_destroy(&process_table->mutex) == -1){
perror("shm_destroy");
exit(1);
}
if (munmap(process_table, sizeof(struct history_struct)) < 0){
printf("Error unmapping\n");
perror("munmap");
exit(1);
}
if (close(shm_fd) == -1){
perror("close");
exit(1);
}
if (shm_unlink("shm") == -1){
perror("shm_unlink");
exit(1);
}
exit(0);
}
}
// This is a signal handler for SIGCHLD. It handles the termination of child processes,
// updates information in the history table, and synchronizes access using a semaphore.
static void sigchld_handler(int signum, siginfo_t *info, void *context){
if(signum == SIGCHLD){
pid_t sender_pid = info->si_pid;
if (sender_pid != scheduler_pid){
if (sem_wait(&process_table->mutex) == -1){
perror("sem_wait");
exit(1);
}
for (int i=0; i<process_table->history_count; i++){
if (process_table->history[i].pid == sender_pid){
process_table->history[i].execution_time += end_time(&process_table->history[i].start);
process_table->history[i].completed = true;
break;
}
}
if (sem_post(&process_table->mutex) == -1){
perror("sem_post");
exit(1);
}
}
}
}
//the function called upon termination to print command details
//in here we are formatting time and printing iterating over the global array
void termination_report(){
if (sem_wait(&process_table->mutex) == -1){
perror("sem_wait");
exit(1);
}
if (process_table->history_count > 0){
//PID is -1 if a command was not executed through process creation
printf("\nCommand\t\tPID\t\tExecution_time\t\tWaiting_time\n");
for (int i=0; i<process_table->history_count; i++){
printf("%s\t\t%d\t\t%ldms\t\t%ldms\n",process_table->history[i].command,process_table->history[i].pid,process_table->history[i].execution_time,process_table->history[i].wait_time);
}
}
if (sem_post(&process_table->mutex) == -1){
perror("sem_post");
exit(1);
}
}
//infinite loop for the shell
//we take user input, pass it over to launch and wait for execution and update time fields
void shell_loop(){
int status;
do{
//this prints the output in magenta colour
printf("\033[1;35mos@shell:~$\033[0m ");
char* command = read_user_input();
if (sem_wait(&process_table->mutex) == -1){
perror("sem_wait");
exit(1);
}
process_table->history[process_table->history_count].pid = -1;
process_table->history[process_table->history_count].submit = false;
process_table->history[process_table->history_count].wait_time = process_table->history[process_table->history_count].execution_time = process_table->history[process_table->history_count].vruntime = 0;
start_time(&process_table->history[process_table->history_count].start);
if (sem_post(&process_table->mutex) == -1){
perror("sem_post");
exit(1);
}
status = launch(command);
if (sem_wait(&process_table->mutex) == -1){
perror("sem_wait");
exit(1);
}
if(!process_table->history[process_table->history_count].submit){
process_table->history[process_table->history_count].execution_time = end_time(&process_table->history[process_table->history_count].start);
}
process_table->history_count++;
if (sem_post(&process_table->mutex) == -1){
perror("sem_post");
exit(1);
}
} while(status);
}
//here we take input and remove trailing \n and update global array
char* read_user_input(){
static char input[MAX_SIZE+1];
if (fgets(input,MAX_SIZE+1,stdin) == NULL){
perror("fgets");
exit(1);
}
int input_len = strlen(input);
if (input_len>0 && input[input_len-1]=='\n'){
input[input_len-1] = '\0';
}
if (sem_wait(&process_table->mutex) == -1){
perror("sem_wait");
exit(1);
}
strcpy(process_table->history[process_table->history_count].command,input);
if (sem_post(&process_table->mutex) == -1){
perror("sem_post");
exit(1);
}
return input;
}
//here we execute custom commands which dont require process creation and their pids are -1
int launch(char* command){
//removing leading whitespaces
while(*command==' ' || *command=='\t'){
command++;
}
//removing trailing whitespaces
char *end = command + strlen(command)-1;
while (end>=command && (*end==' ' || *end=='\t')){
*end = '\0';
end--;
}
if (strncmp(command, "submit", 6) == 0) {
// Check if the priority is specified
if (sem_wait(&process_table->mutex) == -1){
perror("sem_wait");
exit(1);
}
process_table->history[process_table->history_count].submit = true;
process_table->history[process_table->history_count].completed = false;
process_table->history[process_table->history_count].priority = 1;
process_table->history[process_table->history_count].queue = false;
process_table->history[process_table->history_count].pid = submit_process(command);
start_time(&process_table->history[process_table->history_count].start);
if (sem_post(&process_table->mutex) == -1){
perror("sem_post");
exit(1);
}
return 1;
}
if (strcmp(command,"history") == 0){
if (sem_wait(&process_table->mutex) == -1){
perror("sem_wait");
exit(1);
}
for (int i=0; i<process_table->history_count+1; i++){
printf("%s\n",process_table->history[i].command);
}
if (sem_post(&process_table->mutex) == -1){
perror("sem_post");
exit(1);
}
return 1;
}
if (strcmp(command,"jobs") == 0){
if (sem_wait(&process_table->mutex) == -1){
perror("sem_wait");
exit(1);
}
for (int i=0; i<process_table->history_count; i++){
if (process_table->history[i].submit==true && process_table->history[i].completed==false){
printf("%d\t%d\t%s\n",process_table->history[i].pid,process_table->history[i].priority,process_table->history[i].command);
}
}
if (sem_post(&process_table->mutex) == -1){
perror("sem_post");
exit(1);
}
return 1;
}
if (strcmp(command, "") == 0){
process_table->history_count--;
return 1;
}
if (strcmp(command,"exit") == 0){
return 0;
}
int status;
status = create_process_and_run(command);
return status;
}
//here we check for the pipe and & in the commands and create child process after that in other function
int create_process_and_run(char* command){
//separating pipe commands (|)
int command_count = 0;
char* commands[MAX_COMMANDS];
char* token = strtok(command, "|");
while (token != NULL){
commands[command_count++] = token;
token = strtok(NULL, "|");
}
if (command_count>MAX_COMMANDS){
printf("you have used more than 4 pipes, try again");
return 1;
}
//executing if pipe is present in command input except the last one
int i, prev_read = STDIN_FILENO;
int pipes[2], child_pids[command_count];
//we iterate and execute every command through process creation and keep updating read and write ends of pipe
for (i=0; i < command_count-1; i++){
if (pipe(pipes) == -1){
perror("pipe");
exit(1);
}
if ((child_pids[i]=create_child_process(commands[i], prev_read, pipes[1])) < 0){
perror("create_child_process");
exit(1);
}
if (close(pipes[1]) == -1){
perror("close");
exit(1);
}
prev_read = pipes[0];
}
//the last command whose output is to be displayed on STDOUT
//checking if it a background process
bool background_process = 0;
if (commands[i][strlen(commands[i]) - 1] == '&') {
// Remove the '&' symbol
commands[i][strlen(commands[i]) - 1] = '\0';
background_process = 1;
}
if ((child_pids[i]=create_child_process(commands[i], prev_read, STDOUT_FILENO)) < 0){
perror("create_child_process");
exit(1);
}
//updating global array for pids
if (sem_wait(&process_table->mutex) == -1){
perror("sem_wait");
exit(1);
}
process_table->history[process_table->history_count].pid = child_pids[i];
if (sem_post(&process_table->mutex) == -1){
perror("sem_post");
exit(1);
}
if (!background_process) {
//wait for child process if command is not background
for (i = 0; i < command_count; i++) {
int ret;
int pid = waitpid(child_pids[i], &ret, 0);
if (pid < 0) {
perror("waitpid");
exit(1);
}
if (!WIFEXITED(ret)){
printf("Abnormal termination of %d\n", pid);
}
}
}
else{
//print pid and command if it is being executed in background
printf("%d %s\n", child_pids[command_count-1],command);
}
}
int create_child_process(char *command, int input_fd, int output_fd){
int status = fork();
if (status < 0){
printf("fork() failed.\n");
exit(1);
}
else if (status == 0){
//child process
//updating/copying I/O descriptors
if (input_fd != STDIN_FILENO)
{
if (dup2(input_fd, STDIN_FILENO) == -1){
perror("dup2");
exit(1);
}
if (close(input_fd) == -1){
perror("close");
exit(1);
}
}
if (output_fd != STDOUT_FILENO)
{
if (dup2(output_fd, STDOUT_FILENO) == -1){
perror("dup2");
exit(1);
}
if (close(output_fd) == -1){
perror("close");
exit(1);
}
}
//creating an array of indiviudal command and its arguments
char* arguments[MAX_WORDS+1]; //+1 to accomodate NULL
int argument_count = 0;
char* token = strtok(command, " ");
while (token != NULL){
arguments[argument_count++] = token;
token = strtok(NULL, " ");
}
arguments[argument_count] = NULL;
//exec to execute command (actual part of child process)
if (execvp(arguments[0],arguments) == -1) {
perror("execvp");
printf("Not a valid/supported command.\n");
exit(1);
}
exit(0);
}
else{
//parent process
return status;
}
}
void start_time(struct timeval *start){
gettimeofday(start, 0);
}
unsigned long end_time(struct timeval *start){
struct timeval end;
unsigned long t;
gettimeofday(&end, 0);
t = ((end.tv_sec*1000000) + end.tv_usec) - ((start->tv_sec*1000000) + start->tv_usec);
return t/1000;
}
int submit_process(char *command){
int priority, status;
//creating an array of indiviudal command and its arguments
char* arguments[MAX_WORDS+1]; //+1 to accomodate NULL
int argument_count = 0;
char* token = strtok(command, " "); //remove submit keyword from command
token = strtok(NULL, " ");
while (token != NULL){
arguments[argument_count++] = token;
token = strtok(NULL, " ");
}
//checking if priority is specified
if (argument_count > 1){
priority = atoi(arguments[--argument_count]);
if (priority<1 || priority>4){
printf("either invalid priority or you are passing arguments for a job");
process_table->history[process_table->history_count].completed = true;
return -1;
}
process_table->history[process_table->history_count].priority = priority;
}
arguments[argument_count] = NULL;
status = fork();
if (status < 0){
printf("fork() failed.\n");
exit(1);
}
else if (status == 0){
//exec to execute command (actual part of child process)
if (execvp(arguments[0],arguments) == -1) {
perror("execvp");
printf("Not a valid/supported command.\n");
exit(1);
}
exit(0);
}
else{
//parent process returns pid and stops child
if (kill(status, SIGSTOP) == -1){
perror("kill");
exit(1);
}
return status;
}
}