shell进程只会向由它创建的进程组发送的SIGHUP信号,不会向不是由它创建的进程组发送SIGHUP信号。
使用alarm设置一个定时器来发送SIGALRM信号,如果一个进程接收到SIGALRM信号而不处理时会导致进程终止。
-
gwwu@hz-dev2.wgw.com:~/test/signal>more trap_sighup1.c
-
#include <unistd.h>
-
#include <signal.h>
-
#include <stdio.h>
-
static void handler(int sig)
-
{
-
-
}
-
-
int main(int argc, char *argv[])
-
{
-
pid_t childPid;
-
struct sigaction sa;
-
-
setbuf(stdout,NULL);
-
-
sigemptyset(&sa.sa_mask);
-
sa.sa_flags = 0;
-
sa.sa_handler = handler;
-
if(sigaction(SIGHUP,&sa,NULL) == -1) {
-
printf("sigaction error\n");
-
return -1;
-
}
-
-
childPid = fork();
-
-
if(childPid < 0){
-
printf("fork error\n");
-
return -1;
-
}
-
if(childPid == 0 && argc > 1) {
-
if(setpgid(0,0) < 0) {
-
printf("etpgid error\n");
-
return -1;
-
}
-
}
-
-
printf("PID = %ld,PPID = %ld,PGID = %ld,SID = %ld\n",(long)getpid(),
-
(long)getppid(),(long)getpgrp(),(long)getsid(0));
-
-
alarm(60);
-
-
for(;;) {
-
pause();
-
printf("%ld catch sighup\n",(long)getpid());
-
}
-
}
gwwu@hz-dev2.aerohive.com:~/test/signal>echo $$
10231
gwwu@hz-dev2.wgw.com:~/test/signal>gcc -g trap_sighup1.c -o trap_sighup1
gwwu@hz-dev2.wgw.com:~/test/signal>./trap_sighup1 > trap_sighup1_log 2>&1 &
[1] 12260
gwwu@hz-dev2.wgw.com:~/test/signal>./trap_sighup1 x > trap_sighup_x_log 2>&1
接着关闭终端
重新连接终端,查看对应的log文件。
gwwu@hz-dev2.wgw.com:~/test/signal>more trap_sighup1_log
PID = 12260,PPID = 10231,PGID = 12260,SID = 10231
PID = 12261,PPID = 12260,PGID = 12260,SID = 10231
12261 catch sighup
12260 catch sighup
gwwu@hz-dev2.wgw.com:~/test/signal>more trap_sighup2_log
trap_sighup2_log: No such file or directory
gwwu@hz-dev2.wgw.com:~/test/signal>more trap_sighup_x_log
PID = 12267,PPID = 10231,PGID = 12267,SID = 10231
PID = 12268,PPID = 12267,PGID = 12268,SID = 10231
12267 catch sighup
阅读(731) | 评论(0) | 转发(0) |