diff options
author | Niklas Halle <niklas@niklashalle.net> | 2020-05-25 18:30:38 +0200 |
---|---|---|
committer | Niklas Halle <niklas@niklashalle.net> | 2020-05-25 18:30:38 +0200 |
commit | 41775b83ad93264b487bf0dc353b145922ee6d73 (patch) | |
tree | 510876af17d5d3d8f012535389b6eb5b2765d28c /02_exercise/builtins.c | |
parent | cc59cc8557aa9b1fdb62261fade15d9ade9d11fb (diff) | |
download | betriebssysteme-41775b83ad93264b487bf0dc353b145922ee6d73.tar.gz betriebssysteme-41775b83ad93264b487bf0dc353b145922ee6d73.zip |
enabled terminating wait whilst resuming the waited for process, also added fg
Diffstat (limited to '02_exercise/builtins.c')
-rw-r--r-- | 02_exercise/builtins.c | 22 |
1 files changed, 17 insertions, 5 deletions
diff --git a/02_exercise/builtins.c b/02_exercise/builtins.c index ac655d8..4ab4c7c 100644 --- a/02_exercise/builtins.c +++ b/02_exercise/builtins.c @@ -14,16 +14,17 @@ void handle(int sig) { } -void builtin_wait(process p) { - +void builtin_wait(process p, bool bind) { struct sigaction handler; handler.sa_handler = handle; sigemptyset(&handler.sa_mask); handler.sa_flags = 0; - bool error = false; + pid_t *pids; arrayInit(pids); + size_t i = 1; + bool error = false; while (!error && p.argv[i] != NULL) { char *end; pid_t tmp = strtol(p.argv[i], &end, 10); @@ -40,14 +41,25 @@ void builtin_wait(process p) { while (!arrayIsEmpty(pids)) { int status; pid_t current_pid = arrayPop(pids); - printf("Waiting for %ld...\n", (long)current_pid); + if (bind) { + printf("Resuming %ld...\n", (long) current_pid); + } else { + printf("Waiting for %ld...\n", (long) current_pid); + } // install signal handler without SA_RESTART, so waitpid gets interrupted sigaction(SIGINT, &handler, NULL); if (waitpid(current_pid, &status, WUNTRACED) < 0) { if (EINTR == errno) { // cancelled by ctrl-c + if (bind) { + kill(current_pid, SIGKILL); + waitpid(current_pid, &status, WUNTRACED); + printf("Killed [%ld]\n", (long)current_pid); + } else { + kill(current_pid, SIGCONT); + } } else { - perror("Could not wait on process"); + perror("Could not wait for process"); } } else { printf("[%i] TERMINATED\n", current_pid); |