The difference between shells that do job control and shells that don't

December 24, 2007

Apart from having job control, the difference between a job control shell and a non job control shell is that job control shells put each command into a difference process group. They do this even for commands running in the foreground, because you may later want to ^Z the command and push it into the background, and process groups are the core mechanism of doing job control.

(Process groups do two things in job control. First, they let the shell reliably send stop or start signals to all of the processes spawned by a command. Second, they control what processes can do IO with the terminal; only processes in the foreground process group can read from the terminal, and you can use 'stty tostop' to make it so that only the foreground process group can write to the terminal.)

Shells that don't do job control just ignore process groups and thus leave all commands in the current process group (which is also the foreground process group).

This matters because of what happens when you end a session. When a terminal closes (either a pty or a real terminal), only the current foreground process group is sent a SIGHUP; other process groups are left alone. Thus, background processes started by commands run from a job control shell are insulated from SIGHUP, because they inherit the command's process group and when the command finished, that is no longer the foreground process group. This allows commands to be somewhat sloppy in how they start long running background processes.

(So how do suspended processes get cleaned up when you log out? There's a second mechanism: orphaned process groups that have one or more suspended processes are sent SIGHUP and then SIGCONT.)

Since there are very few shells these days that don't do job control, this may be a hard error for people to see in testing. But it's really not hard to see in code; the rule is that if you want a process to survive the session ending, you must take explicit steps to insulate it from SIGHUP et al. If you don't, you're being lazy and counting on the side effects of running under a job control shell.

(A recent correction got me interested in all of this.)

Written on 24 December 2007.
« Multihomed hosts and /etc/hosts
Process memory layout for 32-bit Linux programs »

Page tools: View Source, Add Comment.
Login: Password:
Atom Syndication: Recent Comments.

Last modified: Mon Dec 24 23:42:18 2007
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.