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

全部博文(142)

文章存档

2016年(10)

2015年(60)

2014年(72)

我的朋友

分类: LINUX

2015-04-23 17:21:13

如果因为终端断开引起的向控制进程发送的SIGHUP信号会导致控制进程终止,那么SIGHUP信号会被发送给终端的前台进程组中的成员。控制进程处于任何原因终止,那么前台进程就会收到SIGHUP信号。这个是控制进程终止的结果,不是SIGHUP信号相关联的行为。

点击(此处)折叠或打开

  1. gwwu@hz-dev2.wgw.com:~/test/signal>more trap_sighup2.c
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <signal.h>

  6. static void handler(int sig)
  7. {
  8.     printf("PID %ld caught signal %2d (%s)\n",getpid(),sig,strsignal(sig));
  9. }

  10. int main(int argc, char *argv[])
  11. {
  12.     pid_t parentPid, childPid;
  13.     int j;
  14.     struct sigaction sa;


  15.     if(argc < 2 || strcmp(argv[1], "--help") == 0) {
  16.         printf("Usage: %s {d|s} ... [ > sig.log 2>&1 ]\n",argv[0]);
  17.         return 0;
  18.     }
  19.     setbuf(stdout,NULL);

  20.     parentPid = getpid();
  21.     printf("PID of parent process is %ld,shell process id = %ld\n",(long)parentPid,getppid());
  22.     printf("Foreground process group ID is: %ld,session id = %ld\n",(long)tcgetpgrp(STDIN_FILENO),getsid(0));

  23.     for(j = 1; j < argc; j++) {
  24.         childPid = fork();
  25.         if(childPid < 0) {
  26.             printf("fork error\n");
  27.             return -1;
  28.         }

  29.         if(childPid == 0) {
  30.             if(argv[j][0] == 'd') {
  31.                 if(setpgid(0,0) == -1) {
  32.                     printf("setpgid error\n");
  33.                     return -1;
  34.                 }
  35.             }
  36.             sigemptyset(&sa.sa_mask);
  37.             sa.sa_flags = 0;
  38.             sa.sa_handler = handler;
  39.             if(sigaction(SIGHUP,&sa,NULL) == -1) {
  40.                 printf("sigaction error\n");
  41.                 return -1;
  42.             }
  43.             break;
  44.         }
  45.     }
  46.     alarm(60);
  47.     printf("PID = %ld,PGID = %ld\n",getpid(),getpgrp());
  48.     for(;;)
  49.         pause;
  50. }

点击(此处)折叠或打开

  1. gwwu@hz-dev2.wgw.com:~/test/signal>gcc -g trap_sighup2.c -o trap_sighup2
  2. gwwu@hz-dev2.wgw.com:~/test/signal>echo $$
  3. 23078
  4. gwwu@hz-dev2.wgw.com:~/test/signal>./trap_sighup2 d s s > trap_sighup2.log 2>&1
  5. /*退出后重新登入*/
  6. Last login: Thu Apr 23 17:13:20 2015 from 10.155.60.28
  7. gwwu@hz-dev2.wgw.com:~/codes/istanbul_4_22>
  8. gwwu@hz-dev2.wgw.com:~/codes/istanbul_4_22>cd ~/test/
  9. Display all 168 possibilities? (y or n)
  10. gwwu@hz-dev2.wgw.com:~/codes/istanbul_4_22>cd ~/test/signal/
  11. gwwu@hz-dev2.wgw.com:~/test/signal>more trap_sighup2.log
  12. PID of parent process is 23169,shell process id = 23078
  13. Foreground process group ID is: 23169,session id = 23078
  14. PID = 23169,PGID = 23169
  15. PID = 23172,PGID = 23169
  16. PID = 23171,PGID = 23169
  17. PID = 23170,PGID = 23170
  18. PID 23171 caught signal 1 (Hangup)
  19. PID 23172 caught signal 1 (Hangup)


阅读(929) | 评论(0) | 转发(0) |
0

上一篇:trap sighup

下一篇:iptables redirect重定向

给主人留下些什么吧!~~