-
Notifications
You must be signed in to change notification settings - Fork 1
/
signal_inherit.c
58 lines (49 loc) · 1.08 KB
/
signal_inherit.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
54
55
56
57
58
/*************************************************************************
> File Name: signal_inherit.c
> Author: NiGo
> Mail: nigo@xiyoulinux.org
> Created Time: 2020年04月23日 星期四 22时30分06秒
************************************************************************/
//测试子进程是否继承父进程signal注册的信号
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
void my_err(const char *str)
{
perror(str);
exit(1);
}
void how(int signo)
{
printf("----inherit success!\n");
exit(0);
}
int main(int argc, char *argv[])
{
pid_t pid;
//注册
signal(SIGINT, how);
if ((pid = fork()) == -1)
{
my_err("fork error");
}
else if (pid > 0)
{
printf("Parent: I has died\n");
exit(1);
}
else
{
printf("Child: This is my show time\n");
while (1)
{
sleep(5);
printf("Child: Yeah, I'm still alive\n");
}
return 0;
}
}