summaryrefslogtreecommitdiffstats
path: root/02_exercise/builtins.c
diff options
context:
space:
mode:
Diffstat (limited to '02_exercise/builtins.c')
-rw-r--r--02_exercise/builtins.c22
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);