camino camino - 1 year ago 73
C Question

why SIGHUP signal not received when becoming an Orphaned Process Group

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. ”

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(),

char c;
pid_t pid;

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

Answer Source

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.

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download