summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Zabka <zabkaste@hu-berlin.de>2020-05-24 22:07:53 +0200
committerStefan Zabka <zabkaste@hu-berlin.de>2020-05-24 22:07:53 +0200
commitcc59cc8557aa9b1fdb62261fade15d9ade9d11fb (patch)
tree3fd2f9b2cbe664011c3e78ad41ef6385327e4e3c
parent2dfb4e933a3c3d66670b506b5f6aca55266df6e5 (diff)
downloadbetriebssysteme-cc59cc8557aa9b1fdb62261fade15d9ade9d11fb.tar.gz
betriebssysteme-cc59cc8557aa9b1fdb62261fade15d9ade9d11fb.zip
Can now kill running processes
-rw-r--r--02_exercise/process.c1
-rw-r--r--02_exercise/shell.c28
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 <path>` - 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);