免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 6300 | 回复: 7
打印 上一主题 下一主题

父进程退出,会给子进程发出SIGHUP信号? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-04-11 11:45 |只看该作者 |倒序浏览
apue上的一段代码,

  1. static void        sig_hup(int);
  2. static void        pr_ids(char *);

  3. int
  4. main(void)
  5. {
  6.         char        c;
  7.         pid_t        pid;

  8.         pr_ids("parent");
  9.         if ( (pid = fork()) < 0)
  10.                 err_sys("fork error");

  11.         else if (pid > 0) {        /* parent */
  12.                 sleep(5);                /* sleep to let child stop itself */
  13.                 exit(0);                /* then parent exits */

  14.         } else {                        /* child */
  15.                 pr_ids("child");
  16.                 signal(SIGHUP, sig_hup);//我就是搞不懂,这个SIGHUP是什么时候,由谁发出来的?是不是父进程退出都会发SIGHUP信号给子进程,叫他们跟着他爹一块去死?       
  17.                 kill(getpid(), SIGTSTP);        //还有,因为父进程想他儿子跟他一块完蛋,会自动给处于暂停壮态的子进程发SIGCONT信号,唤醒这个儿子,这样做是为了不耽误他爷俩一块over?
  18.                 pr_ids("child");       
  19.                 if (read(0, &c, 1) != 1)
  20.                         printf("read error from control terminal, errno = %d\n", errno);
  21.                 exit(0);
  22.         }
  23. }



复制代码

[ 本帖最后由 xxldc 于 2008-4-11 11:57 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2008-04-11 11:51 |只看该作者
哈哈,我喜欢楼主的风格,但是你的问题我不懂

论坛徽章:
0
3 [报告]
发表于 2008-04-11 12:00 |只看该作者
原帖由 xxldc 于 2008-4-11 11:45 发表
signal(SIGHUP, sig_hup);//我就是搞不懂,这个SIGHUP是什么时候,由谁发出来的?是不是父进程退出都会发SIGHUP信号给子进程,叫他们跟着他爹一块去死?        
                kill(getpid(), SIGTSTP);        //还有,因为父进程想他儿子跟他一块完蛋,会自动给处于暂停壮态的子进程发SIGCONT信号,唤醒这个儿子,好一块over?

SIGHUP是在子进程停止,而父进程又终止时由内核发给子进程的
SIGCONT也是这样来的,它用来唤醒子进程

论坛徽章:
0
4 [报告]
发表于 2008-04-11 16:03 |只看该作者
不顶一下,真的不行

论坛徽章:
0
5 [报告]
发表于 2008-04-11 16:44 |只看该作者
三楼好像不对。

lz不是有APUE么,书上讲了呀:
摘自APUE2

  1. SIGHUP
  2. This signal is sent to the controlling process (session leader) associated with a controlling terminal if a disconnect is detected by the terminal interface. Referring to Figure 9.12, we see that the signal is sent to the process pointed to by the s_leader field in the session structure. This signal is generated for this condition only if the terminal's CLOCAL flag is not set. (The CLOCAL flag for a terminal is set if the attached terminal is local. The flag tells the terminal driver to ignore all modem status lines. We describe how to set this flag in Chapter 18.)

  3. Note that the session leader that receives this signal may be in the background; see Figure 9.7 for an example. This differs from the normal terminal-generated signals (interrupt, quit, and suspend), which are always delivered to the foreground process group.

  4. This signal is also generated if the session leader terminates. In this case, the signal is sent to each process in the foreground process group.

  5. This signal is commonly used to notify daemon processes (Chapter 13) to reread their configuration files. The reason SIGHUP is chosen for this is that a daemon should not have a controlling terminal and would normally never receive this signal.

复制代码


不过看完这个后,我又有个新的疑问:


  1. This signal is also generated if the session leader terminates. In this case, the signal is sent to each process in the [b]foreground[/b] process group.
复制代码


既然这样,为什么还需要nohup?

论坛徽章:
0
6 [报告]
发表于 2008-04-11 17:30 |只看该作者

回复 #5 lgfang 的帖子

你讲的当调制解调器(或网络)断开连接时,终端将sighup发给控制进程(会话首进程)
但这里讲的孤儿进程组吧.

论坛徽章:
0
7 [报告]
发表于 2008-04-11 21:09 |只看该作者
原帖由 xxldc 于 2008-4-11 11:45 发表
apue上的一段代码,

static void        sig_hup(int);
static void        pr_ids(char *);

int
main(void)
{
        char        c;
        pid_t        pid;

        pr_ids("parent");
        if ( (pid = fork()) < 0)
                err_sys("fork error"); ...


上面的意思是说,当父进程5秒钟退出时,进程组成为了孤儿进程组,这时内核就会发出SIGHUP信号,但这个信号会使进程退出,所以得捕捉它。
但内核发完SIGHUP后又接着向进程发送SIGCONT信号,这时进程又接着运行(子进程已经停止运行,因为SIGTSTP,并且read)。

论坛徽章:
0
8 [报告]
发表于 2008-05-16 01:35 |只看该作者
哈哈,搂主真搞笑,先弄明白孤儿进程组的概念再说,当父进程退出使某进程组成为孤儿进程组时,如果此组内有停止进程,则内核会发SIGHUP信号的。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP