Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

296 Process Relationships Chapter 9


This function returns an error if the caller is already a process group leader.Toensure
this is not the case, the usual practice is to callforkand have the parent terminate and
the child continue. We are guaranteed that the child is not a process group leader,
because the process group ID of the parent is inherited by the child, but the child gets a
new process ID. Hence, it is impossible for the child’s process ID to equal its inherited
process group ID.
The Single UNIX Specification talks only about a ‘‘session leader’’;there is no
‘‘session ID’’similar to a process ID or a process group ID. Obviously,asession leader
is a single process that has a unique process ID, so we could talk about a session ID that
is the process ID of the session leader.This concept of a session ID was introduced in
SVR4. Historically,BSD-based systems didn’t support this notion, but have since been
updated to include it. Thegetsidfunction returns the process group ID of a process’s
session leader.

Some implementations, such as Solaris, join with the Single UNIX Specification in the practice
of avoiding the use of the phrase ‘‘session ID,’’opting instead to refer to this as the ‘‘process
group ID of the session leader.’’The two areequivalent, since the session leader is always the
leader of a process group.

#include <unistd.h>
pid_t getsid(pid_tpid);
Returns: session leader’s process group ID if OK,−1 on error

Ifpidis 0,getsidreturns the process group ID of the calling process’s session leader.
For security reasons, some implementations may restrict the calling process from
obtaining the process group ID of the session leader ifpiddoesn’t belong to the same
session as the caller.

9.6 ControllingTe rminal


Sessions and process groups have a few other characteristics.

•Asession can have a singlecontrolling terminal.This is usually the terminal
device (in the case of a terminal login) or pseudo terminal device (in the case of a
network login) on which we log in.
•The session leader that establishes the connection to the controlling terminal is
called thecontrolling process.
•The process groups within a session can be divided into a singleforeground
process groupand one or morebackground process groups.
•Ifasession has a controlling terminal, it has a single foreground process group
and all other process groups in the session arebackground process groups.
•Whenever we press the terminal’s interrupt key (often DELETE or Control-C),
the interrupt signal is sent to all processes in the foreground process group.
Free download pdf