-
Notifications
You must be signed in to change notification settings - Fork 0
/
expr6.c
44 lines (41 loc) · 998 Bytes
/
expr6.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
//
// Created by zcw on 11/21/19.
//
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
pid_t pid;
static void sig_usr(int signo) /* argument is signal number */
{
printf("\n pid: %d\n", pid);
if (signo == SIGUSR1) printf("\npid: %d received SIGUSR1 %d\n", pid, signo);
else if (signo == SIGINT) printf("\npid: %d received SIGINT %d\n", pid, signo);
else {
printf("received signal %d\n", signo);
exit(2);
}
exit(0);
}
int main() {
pid = fork();
if (pid == 0) {
// Child
printf("Child pid: %d\n", pid);
if (signal(SIGUSR1, sig_usr) == SIG_ERR) {
printf("can't catch SIGUSR1\n");
exit(1);
}
for ( ; ; )
pause();
}
else {
printf("Father pid: %d\n", pid);
if (signal(SIGINT, sig_usr) == SIG_ERR) {
printf("can't catch SIGINT\n");
exit(1);
}
for ( ; ; )
pause();
}
}