Unix & Linux
linux process sighup
Updated Mon, 15 Aug 2022 15:30:16 GMT

Is SIGHUP sent to this orphaned process, and why doesn't it terminate?


In The Linux Programming Interface:

SIGHUP is generated when a process group becomes orphaned.

In an interactive bash process,

$ ( sleep 123 &)

will first forks a subshell, and the subshell then forks to execute sleep 123 &. The subshell exits immediately without waiting sleep 123 to finish because of &. At that time,

  • is the sleep process orphaned? (I think yes, Figure 34.3)

  • is SIGHUP sent to the sleep process because it becomes orphaned? (I guess it is, by the quote)

  • why doesn't the sleep process terminate because SIGHUP is sent to it? (I am not sure)

Thanks.


I also have similar question when create a daemon process by first forking a child process and then the child process forking a grandchild and exiting immediately. The grandchild becomes orphaned and isn't SIGHUP sent to it, making it terminate?




Solution

From The Open Group Base Specifications Issue 7, 2018 edition, System Interfaces, under _exit:

  • If the process is a controlling process, the SIGHUP signal shall be sent to each process in the foreground process group of the controlling terminal belonging to the calling process.

  • If the process is a controlling process, the controlling terminal associated with the session shall be disassociated from the session, allowing it to be acquired by a new controlling process.

  • If the exit of the process causes a process group to become orphaned, and if any member of the newly-orphaned process group is stopped, then a SIGHUP signal followed by a SIGCONT signal shall be sent to each process in the newly-orphaned process group.





Comments (5)

  • +0 – Thanks. I upvoted your reply, but (1) In an interactive shell, when you run a command, you start a new process group different from the process group where the original shell belong. (2) in the example I gave, (sleep 123 &), sleep 123 & process becomes the only process in its own process group, and it has been reparented to some other process outside its process session, so it and its group are both orphaned. — Nov 27, 2018 at 13:29  
  • +0 – Take a look at Figure 34.3 at books.google.com/… — Nov 27, 2018 at 13:33  
  • +0 – @Tim: The figure presupposes that the parent is a group leader; maybe the author could have explained this in more detail. Processes don't become group leaders unless they call setsid() or setpgid(); a process is never made a group leader automatically -- it has to say that it wants to be a group (or session) leader. That's why all processes started from a user's interactive shell get SIGHUP when that shell terminates. — Nov 27, 2018 at 13:59  
  • +0 – "In most job-control shells, this function setpgid() is called after a fork to have the parent set the process group ID of the child, and to have the child set its own process group ID. One of these calls is redundant, but by doing both, we are guaranteed that the child is placed into its own process group before either process assumes that this has happened. If we didnt do this, we would have a race condition, since the childs process group membership would depend on which process executes rst" See p294 of APUE 3ed. — Nov 27, 2018 at 14:03  
  • +0 – @Tim: You are right and I am outdated; I did not take job control into account. — Nov 27, 2018 at 14:05