summaryrefslogtreecommitdiffstats
path: root/02_exercise/signal_handler.c
blob: e135739ce47b206100b33294432d153f6c0a09c1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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);
}