-
Notifications
You must be signed in to change notification settings - Fork 0
/
signal_handlers.c
53 lines (45 loc) · 1.01 KB
/
signal_handlers.c
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "headers.h"
void child_termination_handler()
{
int wstatus;
int pid = waitpid(-1, &wstatus, WNOHANG);
if (pid > 0)
{
if (WIFEXITED(wstatus))
{
int status = WEXITSTATUS(wstatus);
if (status == EXIT_FAILURE)
{
fprintf(stderr, "\nProcess with pid %d ended abnormally\n", pid);
}
else
{
fprintf(stderr, "\nProcess with pid %d ended normally\n", pid);
}
int rc = remove_background_jobs(pid);
}
}
}
void ctrl_z_handler()
{
if (foreground_pid != -1)
{
int rc = kill(foreground_pid, SIGTSTP);
if (rc == -1)
{
perror("kill() error");
}
int res = add_background_jobs(cmd_name, foreground_pid);
}
}
void ctrl_c_handler()
{
if (foreground_pid != -1)
{
int rc = kill(foreground_pid, SIGKILL);
if (rc == -1)
{
perror("kill() error");
}
}
}