Chinaunix首页 | 论坛 | 博客
  • 博客访问: 529300
  • 博文数量: 142
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1452
  • 用 户 组: 普通用户
  • 注册时间: 2013-09-12 16:28
文章分类

全部博文(142)

文章存档

2016年(10)

2015年(60)

2014年(72)

我的朋友

分类: LINUX

2015-04-23 16:31:48

shell进程只会向由它创建的进程组发送的SIGHUP信号,不会向不是由它创建的进程组发送SIGHUP信号。
使用alarm设置一个定时器来发送SIGALRM信号,如果一个进程接收到SIGALRM信号而不处理时会导致进程终止。

点击(此处)折叠或打开

  1. gwwu@hz-dev2.wgw.com:~/test/signal>more trap_sighup1.c
  2. #include <unistd.h>
  3. #include <signal.h>
  4. #include <stdio.h>
  5. static void handler(int sig)
  6. {

  7. }

  8. int main(int argc, char *argv[])
  9. {
  10.     pid_t childPid;
  11.     struct sigaction sa;

  12.     setbuf(stdout,NULL);

  13.     sigemptyset(&sa.sa_mask);
  14.     sa.sa_flags = 0;
  15.     sa.sa_handler = handler;
  16.     if(sigaction(SIGHUP,&sa,NULL) == -1) {
  17.         printf("sigaction error\n");
  18.         return -1;
  19.     }

  20.     childPid = fork();

  21.     if(childPid < 0){
  22.         printf("fork error\n");
  23.         return -1;
  24.     }
  25.     if(childPid == 0 && argc > 1) {
  26.         if(setpgid(0,0) < 0) {
  27.             printf("etpgid error\n");
  28.             return -1;
  29.         }
  30.     }

  31.     printf("PID = %ld,PPID = %ld,PGID = %ld,SID = %ld\n",(long)getpid(),
  32.             (long)getppid(),(long)getpgrp(),(long)getsid(0));

  33.     alarm(60)

  34.     for(;;) {
  35.         pause();
  36.         printf("%ld catch sighup\n",(long)getpid());
  37.     }
  38. }
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

阅读(652) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~