Contents

Process Groups, Sessions and Daemon Overview

To understand what a daemon is,
I consulted The Linux Programming Interface and took notes related to it.

  • A process group is a collection of related processes.
  • A session is a collection of related process groups.

Process groups and sessions are defined for job control

Process Group

A process group is a collection of related processes that share the same process group identifier (PGID).
Each process group has a process group leader,
which is the process that created the group.
The PGID of the process group is the PID of its leader.

The PGID of any newly created process is inherited from its parent.

The lifetime of a process group begins when its leader creates it and ends when all processes have left the group.
A process ’leaves’ a process group either by terminating or by being switched to another process group.

Session

A session is a collection of related process groups that share the same session identifier (SID).
Each session has a session leader,
which is the process that created the session.
The SID of the session is the PID of its leader.

Whenever a new process is created,
its SID is inherited from its parent.

All processes in the same session share a controlling terminal.
A controlling terminal is established the first time a session leader opens a terminal device,
and a terminal can only be the controlling terminal for one session.
Hence, there is a one-to-one relationship between a session and a controlling terminal.

At any given time, there are:

  • foreground process group:
    A process group within a session.
    Only processes in this process group can receive input from the controlling terminal.
  • background process groups:
    All process groups that are not the foreground process group.

Terminal

When a terminal is opened, a session leader is established,
and this same session leader also acts as the controlling process.
Meanwhile, the foreground process group waits for input from the terminal,
which could be user input or signals from the user,
while background process groups exist concurrently.

When a terminal is terminated,
the kernel sends a SIGHUP signal to the session leader to indicate that the terminal session has ended.

Shell Job Control

Process groups and sessions are defined to explain shell job control.
Here is an example of shell job control:

The terminal used by the user to log in is the controlling terminal,
and the login shell acts as both the session leader and the controlling process.
Commands executed from this shell will create one or more processes.
These processes form new process groups,
and any other processes created by them become part of these process groups.
All these processes are created from this shell and thus belong to this login session.

Daemon

Characteristics of daemons:

  • long-lived:
    Usually activated when the system starts,
    running until the system is turned off.
    Running in the background and having no controlling terminal: To ensure that no job control or terminal-related signals generated by the kernel affect the daemon.

Daemons usually end with ’d'.

Several common daemons:

  • cron
  • sshd
  • httpd