レナート   Wunschkonzert, Ponyhof und Abenteuerspielplatz   ﻟﻴﻨﺎﺭﺕ

Mon, 26 Nov 2007

Lazyweb: POSIX Process Groups and Sessions

Dear Lazyweb,

I have trouble understanding what exactly POSIX process groups and sessions are good for. The POSIX docs are very vague on this. What exactly is the effect of being in a process group with some other process, and what does being in the same session with it add on top? And what is the benefit of being a group/session leader in contrast of just being a normal random process in the group/session?

The only thing I understood is that kill(2) with a negative first parameter can be used to "multicast" signals to entire process groups, and that SIGINT on C-c is delivered that way. But, is that all? The POSIX docs say "... for the purpose of signaling, placement in foreground or background, and other job control actions", which is very vague. What are those "other job control actions?". What does job control persist of besides multicasting signals? And what is "placement in foreground or background" other than delivering signals?

And I totally don't get POSIX sessions and how they differ from POSIX process groups. Please enlighten me!

Puzzled,
    Lennart

posted at: 03:22 | path: /projects | permanent link to this entry | 7 comments


Posted by Scott Robinson at Mon Nov 26 04:19:56 2007
Do publish a follow-up post if you discover the answer.

I too have always been curious.

Posted by Colin Walters at Mon Nov 26 04:20:13 2007
You have seen the GNU libc docs right?

http://info2html.sourceforge.net/cgi-bin/info2html-demo/info2html?(libc.info.gz)Job%20Control

I wouldn't say I totally understand all this, though I am slowly trying to figure it out, as I figure it's important since I'm implementing a shell...

Posted by Anonymous at Mon Nov 26 04:45:57 2007
For one thing, process groups ensure that when you close your shell (such as by terminating an SSH or X terminal), all of the processes receive a SIGHUP.

Posted by Ray at Mon Nov 26 05:15:30 2007
The Stevens/Rago book goes pretty in depth into this.  You might want to check it out.

The concepts are mainly useful for shells and are for determining which processes get control of the stdin/stdout.

All the process on a command pipe line are in the same process group.  So for instance, with,

cat file | grep keyword | sort | uniq > output &

the entire job is in a process group.  Because of the amperstand, it's a background job. The command prompt is in its own process group which is the foreground process group.

The command prompt and that job are part of the same session.  Each session has a controlling terminal (normally the pty associated with the xterm).  It is defined to be the controlling terminal of the foreground process group.  Processes in background process groups will get sent signals (SIGTTOU and SIGTTIN) if they try to use the session's terminal.

There are some lifecycle details I don't quite remember off hand, too.  I think it's something like if process groups are orphaned (the session leader exits), then they  get sent a hangup signal and get moved to init's session.

Anyway, check out the APUE, it's pretty easy to read and covers all the arcane details.

Posted by Anonymous at Mon Nov 26 10:16:09 2007
The session consists of the association of the processes with a controlling terminal (tty), which typically is not a real (kernel-implemented) terminal but a program that acts as one, then it's implemented by using a pseudo-tty (pty).

Foreground and background is related to processes that you run in the interactive shell; background processes get suspended as soon as they start producing output. There's also a special permission that allows you to send ctrl-c to a setuid process, as long as it is in your session.

The leader gets signaled if one of the child processes dies, that's as far as I know the only difference.

If any process dies (regardless of sessions) its childs become orphans. One of the tasks of the init process is to 'adopt' these orphans. This is important because of the signaling when the child processes exit. As long as a child process' parent hasn't acknowledged the signal, the child cannot be cleaned up (it'll be a zombie process). This is so the exit result of the process doesn't get lost.

Posted by fraggle at Mon Nov 26 22:38:49 2007
I bet it was designed by a committee.

Posted by kenneth topp at Tue Nov 27 06:51:53 2007
a) primary usage of sessions on any machine is delivery of HUP (disconnect) and WNCH (window resizes).  Anyone who creates a daemon should run  setsid to get off the terminal, ignoring signals is adaquate, but wrong.
b) session leader just has difference behavoirs of running setpgid, not better, just different.
c) What does job control persist of besides multicasting signals?  renice(1)
d) And what is "placement in foreground or background" other than delivering signals?  I think this is how processes get access to stdin/stdout/stderr for the tty

e) And I totally don't get POSIX sessions and how they differ from POSIX process groups.  process group == session.

Leave a Comment:

Your Name:


Your E-mail (optional):


Comment:


As a protection against comment spam, please type the following number into the field on the right:
Secret Number Image

Please note that this is neither a support forum nor a bug tracker! Support questions or bug reports posted here will be ignored and not responded to!


It should be obvious but in case it isn't: the opinions reflected here are my own. They are not the views of my employer, or Ronald McDonald, or anyone else.

Please note that I take the liberty to delete any comments posted here that I deem inappropriate, off-topic, or insulting. And I excercise this liberty quite agressively. So yes, if you comment here, I might censor you. If you don't want to be censored your are welcome to comment on your own blog instead.


Lennart Poettering <mzoybt (at) 0pointer (dot) net>
Syndicated on Planet GNOME, Planet Fedora, planet.freedesktop.org, Planet Debian Upstream. feed RSS 0.91, RSS 2.0
Archives: 2005, 2006, 2007, 2008, 2009, 2010, 2011

Valid XHTML 1.0 Strict!   Valid CSS!