From cc59cc8557aa9b1fdb62261fade15d9ade9d11fb Mon Sep 17 00:00:00 2001 From: Stefan Zabka Date: Sun, 24 May 2020 22:07:53 +0200 Subject: Can now kill running processes --- 02_exercise/process.c | 1 + 02_exercise/shell.c | 28 ++++++++++++++++++++++++---- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/02_exercise/process.c b/02_exercise/process.c index 95a0600..0b69074 100644 --- a/02_exercise/process.c +++ b/02_exercise/process.c @@ -7,6 +7,7 @@ #include "array.h" #include "process.h" + // given a substring of a line tries to parse out as much information as possible int parse_command(char const *line, char const *end, process *p) { char *part; diff --git a/02_exercise/shell.c b/02_exercise/shell.c index f4b6738..fc50995 100644 --- a/02_exercise/shell.c +++ b/02_exercise/shell.c @@ -12,10 +12,28 @@ #include "builtins.h" +process *processes ; + +void signal_handeler (int signal) { + printf("Received signal %d", signal); + if(signal == SIGINT) { + for(size_t i = 0; i < arrayLen(processes); ++i) { + pid_t pid = processes[i].pid; + if(pid != 0) { + kill(pid, SIGINT); + } + } + } +} + int main(void) { setvbuf(stderr, NULL, _IONBF, 0); setvbuf(stdout, NULL, _IONBF, 0); - + // I don't think the shell should exit on SIG_TERM + if(signal(SIGINT, SIG_IGN)== SIG_ERR) { + perror("Couldn't ignore sigterm"); + exit(errno); + } printf("Welcome! Available built-ins are:\n" "cd: `cd ` - if no path is given, return to the current dir\n" @@ -47,14 +65,13 @@ int main(void) { } line[length - 1] = '\0'; // cut the line feed - process *processes = NULL; + processes = NULL; parse_line(line, &processes); if (strcmp(processes[0].argv[0], "cd") == 0) { if (arrayLen(processes) != 1) { perror("Can't chain cd with other processes"); } - int ret; switch(arrayLen(processes[0].argv)) { case 3: @@ -81,12 +98,15 @@ int main(void) { } else if (strcmp(processes[0].argv[0], "wait") == 0) { builtin_wait(processes[0]); } else { + if(arrayLen(processes) != 0 && processes[0].blocking) { + signal(SIGINT, signal_handeler); + } for (size_t i = 0; i < arrayLen(processes); ++i) { int ret = exec_command(processes[i]); if (ret) printf("[%i] ", ret); } - + signal(SIGINT, SIG_IGN); } free((void *)line); -- cgit v1.2.3-54-g00ecf