summaryrefslogtreecommitdiffstats
path: root/02_exercise/signal_handler.c
diff options
context:
space:
mode:
Diffstat (limited to '02_exercise/signal_handler.c')
-rw-r--r--02_exercise/signal_handler.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/02_exercise/signal_handler.c b/02_exercise/signal_handler.c
new file mode 100644
index 0000000..e135739
--- /dev/null
+++ b/02_exercise/signal_handler.c
@@ -0,0 +1,30 @@
+#include "signal_handler.h"
+
+#include <wait.h>
+#include <stdio.h>
+#include <unistd.h>
+
+void signal_handler(int signal) {
+ if (signal == SIGINT) {
+ interrupt_issued = 1;
+ } else if (signal == SIGTSTP) {
+ stop_issued = 1;
+ kill(getpid(), SIGINT);
+ }
+}
+
+void handle_interrupt(pid_t pid) {
+ int status;
+ if (stop_issued) {
+ stop_issued = 0;
+ interrupt_issued = 0;
+ } else if (interrupt_issued) {
+ interrupt_issued = 0;
+ kill(pid, SIGKILL);
+ waitpid(pid, &status, 0);
+ printf("Killed [%ld]\n", (long)pid);
+ return;
+ }
+ kill(pid, SIGCONT);
+ printf("[%ld]\n", (long)pid);
+} \ No newline at end of file