From 1d8b5a6f9c66a4c5a696b342ed2f8d472381ed2d Mon Sep 17 00:00:00 2001 From: cyp0633 Date: Wed, 25 May 2022 10:44:33 +0800 Subject: [PATCH] Shell Lab: trace06 --- shlab/tsh.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/shlab/tsh.c b/shlab/tsh.c index 0a6bf8d..0ef2bc6 100644 --- a/shlab/tsh.c +++ b/shlab/tsh.c @@ -325,6 +325,16 @@ void sigchld_handler(int sig) { deletejob(jobs,pid); // remove pid from job list } + if (WIFSIGNALED(status)) + { + printf("Job [%d] (%d) terminated by signal %d\n", pid2jid(pid), pid, WTERMSIG(status)); + deletejob(jobs, pid); + } + + } + if (pid < 0 && errno != ECHILD) + { + unix_error("waitpid error"); } return; } @@ -334,8 +344,13 @@ void sigchld_handler(int sig) * user types ctrl-c at the keyboard. Catch it and send it along * to the foreground job. */ -void sigint_handler(int sig) +void sigint_handler(int sig) { + pid_t pid = fgpid(jobs); // get pid of foreground job + if (kill(-pid, SIGINT) < 0) // try to send SIGINT + { + unix_error("sigint error"); // failed + } return; }