From 41775b83ad93264b487bf0dc353b145922ee6d73 Mon Sep 17 00:00:00 2001 From: Niklas Halle Date: Mon, 25 May 2020 18:30:38 +0200 Subject: enabled terminating wait whilst resuming the waited for process, also added fg --- 02_exercise/builtins.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) (limited to '02_exercise/builtins.c') 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); -- cgit v1.2.3-54-g00ecf