Process Groups, Sessions, and Job Control 701
Figure 34-1 shows the process group and session relationships between the various
processes resulting from the execution of the following commands:
$ echo $$ Display the PID of the shell
400
$ find / 2> /dev/null | wc -l & Creates 2 processes in background group
[1] 659
$ sort < longlist | uniq -c Creates 2 processes in foreground group
At this point, the shell (bash), find, wc, sort, and uniq are all running.
Figure 34-1: Relationships between process groups, sessions, and the controlling terminal
34.2 Process Groups
Each process has a numeric process group ID that defines the process group to
which it belongs. A new process inherits its parent’s process group ID. A process
can obtain its process group ID using getpgrp().
PID = 400
PPID = 399
bash
PGID = 400
PPID = 400
find
PGID = 658
SID = 400
PID = 659
PPID = 400
wc
PGID = 658
SID = 400
PPID = 400
sort
PGID = 660
SID = 400
PID = 661
PPID = 400
uniq
PGID = 660
SID=400
Process
group 660
Session 400
session
leader
background
process groups
foreground
process group
controlling process
process group leaders
Controlling terminal
Foreground PGID = 660
Controlling SID = 400
SID = 400
PID = 658 PID = 660
Process
group 658
Process
group 400
#include <unistd.h>
pid_t getpgrp(void);
Always successfully returns process group ID of calling process