gpt4 book ai didi

c - 为什么在成为孤立进程组时未收到 SIGHUP 信号

转载 作者:IT王子 更新时间:2023-10-29 00:38:51 26 4
gpt4 key购买 nike

在 GNU libc 手册中关于 orphaned process groups ,它提到:

“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信号。我想知道为什么?

#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);
}

最佳答案

如果孤立的进程组在成为孤立进程时被停止,则会得到 SIGHUP 后跟 SIGCONT

sleep 还不够,你需要:

kill(getpid(), SIGSTOP); //or raise(SIGSTOP);

除此之外,如果孤立是由 setsid()setprgrp(),因为它不是由无意识地不知道作业控制的退出进程引起的(参见 http://pubs.opengroup.org/onlinepubs/9699919799/functions/_exit.html)。

但是,使用 kill(getpid(), SIGSTOP) 而不是子进程中的 sleep(60),您的程序将得到一个停止的孤儿,即使您不调用 setsid()

#define _GNU_SOURCE
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <signal.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)
{
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(2);
exit(0); // "child" exit
// so the process group become an orphan process group
}
else{
pr_ids("grandson");
signal(SIGHUP, sig_hup); // establish signal handler
kill(getpid(), SIGSTOP);
printf("end\n");
}
}
exit(0);
}

应该在 parent 去世后(5 秒)让您在 child 中得到一个SIGHUP

关于c - 为什么在成为孤立进程组时未收到 SIGHUP 信号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19756633/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com