成为孤立进程组时为何未收到SIGHUP信号的原因 [英] why SIGHUP signal not received when becoming an Orphaned Process Group

查看:52
本文介绍了成为孤立进程组时为何未收到SIGHUP信号的原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在GNU libc手册中,有关孤立的进程组,它提到了:

In the manual for GNU libc about orphaned process groups, it mentioned :

"process groups that continue running even after the session leader 
has terminated are marked as orphaned process groups. 

When a process group becomes an orphan, its processes are sent a SIGHUP 
signal. Ordinarily, this causes the processes to terminate. However, 
if a program ignores this signal or establishes a handler for it 
(see Signal Handling), it can continue running as  in the orphan process
 group even after its controlling process terminates; but it still 
cannot access the terminal any more. "

我编写了一个测试程序,但是当进程组变成一个孤儿时,它的进程没有收到SIGHUP信号.我想知道为什么吗?

I write a test program, but when the process group becomes an orphan, its process didn't receive the SIGHUP signal. I am wondering why?

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>


static void  
sig_hup(int signo) //**never get called ???**
{
    printf("SIGHUP received, pid = %ld\n", (long)getpid());
}

static void
pr_ids(char *name)
{
    printf("%s: pid = %ld, ppid = %ld, pgrp = %ld, tpgrp = %ld\n",
        name, (long)getpid(), (long)getppid(), (long)getpgrp(),
        (long)tcgetpgrp(STDIN_FILENO));
    fflush(stdout);
}

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

    pr_ids("parent");
    pid = fork();
    if (pid > 0) {       // parent 
        sleep(5);
        exit(0);         // parent exit;
    } else {
        pr_ids("child");
        setsid();        //create new session, and "child" becomes the session leader
        pid = fork();
        if(pid>0) {
            sleep(20);
            exit(0);     // "child" exit
                         // so the process group become an orphan process group
        }
        else{
            pr_ids("grandson");
            signal(SIGHUP, sig_hup);    // establish signal handler 
            sleep(60);                  // now becoming orphan process group
            printf("end\n");
        }
    }
    exit(0);
}

推荐答案

该文档部分专门讨论的是丢失具有一个进程(通常是由于调制解调器挂断或虚拟等效项(结束一个ssh会话等). (我认为可以在此处改进文档中的措词).当在此处使用setsid()时,您将在setsid()返回时放弃对控制终端的访问,因此没有控制终端可以从那里丢失.

That document section is talking specifically about the loss of a controlling terminal of a process that had one—usually by a modem hangup, or the virtual equivalent (ending an ssh session, etc). (I think the phrasing in the document could be improved here). When you use setsid() here, you give up access to the controlling terminal by the time setsid() returns, so there is no controlling terminal to lose from there forward.

您可以open()一个tty设备(例如pty从站)来获得控制终端(请注意,您可能还必须执行一些其他操作-FreeBSD需要一个TIOCSCTTY ioctl),然后再次丢失它,然后然后,您应该会收到SIGHUP信号.

You could open() a tty device (such as a pty slave) to gain a controlling terminal (note that you may have to do some additional operation as well—FreeBSD requires a TIOCSCTTY ioctl), then lose it again, and then you should get the SIGHUP signal.

这篇关于成为孤立进程组时为何未收到SIGHUP信号的原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆