3

APUE 说

由于进程组在父进程终止时是孤立的,因此 POSIX.1 要求向新孤立进程组中停止的每个进程(就像我们的子进程一样)发送挂断信号 (SIGHUP),然后发送继续信号 (SIGCONT)

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#define errexit(msg) do{ perror(msg); exit(EXIT_FAILURE); } while(0)
static void sig_hup(int signo)
{
    printf("SIGHUP received, pid = %d\n", getpid());
}
static void sig_cont(int signo)
{
    printf("SIGCONT received, pid = %d\n", getpid());
}
static void sig_ttin(int signo)
{
    printf("SIGTTIN received, pid = %d\n", getpid());
}
static void pr_ids(char *name)
{
    printf("%s: pid = %d, ppid = %d, pgrp = %d, tpgrp = %d\n",
           name, getpid(), getppid(), getpgrp(), tcgetpgrp(STDIN_FILENO));
}
int main(int argc, char *argv[])
{
    char c;
    pid_t pid;
    setbuf(stdout, NULL);
    pr_ids("parent");
    if ((pid = fork()) < 0) {
            errexit("fork error");
    } else if (pid > 0) { /* parent */
            sleep(5);
            printf("parent exit\n");
            exit(0);
    } else { /* child */
            pr_ids("child...1");
            signal(SIGCONT, sig_cont);
            signal(SIGHUP, sig_hup);
            signal(SIGTTIN, sig_ttin);
            kill(getpid(), SIGTSTP);
            //sleep(10);
            pr_ids("child...2");
            if (read(STDIN_FILENO, &c, 1) != 1) {
                    printf("read error from controlling TTY, errno = %d\n", 
                                                             errno);
            }
            printf("child exit\n");
    }
    exit(0);
  }

程序输出:

parent: pid = 2036, ppid = 1959, pgrp = 2036, tpgrp = 2036
child...1: pid = 2037, ppid = 2036, pgrp = 2036, tpgrp = 2036
parent exit
xiejingfeng@xiejingfeng-desktop:/codes/apue $ SIGCONT 收到,pid = 2037
SIGHUP 收到,pid = 2037
child...2: pid = 2037, ppid = 1, pgrp = 2036, tpgrp = 1959
从控制 TTY 读取错误,errno = 5
child exit

输出不像书中所说的那样,因为程序首先接收 SIGCONT 然后 SIGHUP,这让我很困惑,你们能帮帮我吗?

提前致谢。

4

1 回答 1

6

在子进程恢复执行之前,无法传递 SIGHUP。当一个进程停止时,除了 SIGCONT 和 SIGKILL 之外的所有信号传递都会暂停。

因此,SIGHUP 确实先到达,但在 SIGCONT 唤醒进程执行之前无法处理它。

于 2013-07-21T05:15:14.460 回答