Contents

Process Groups, Sessions and Daemon Overview

Trying to figure out what daemon is,
so I found The Linux Programming Interface and took notes related to it.

  • A Process group is a bunch of related processes
  • A Session is a bunch of related process groups

Defining process group and session is for job control

Process Group

A process group is a bunch of related processes sharing the same process group identifier (PGID).
There is a process group leader in a process group,
which is the process that builds this process group,
and the PGID of the process group is the process group leader’s PID.

The PGID of any newly created process is its parent’s PGID.

The lifetime of a process group is calculated from the time when the process group leader creates the process group and is ended until all the processes leave the process group.
By ’leaves’ a process group it might mean the process is terminated,
or the process is switched to another process group.

Session

A Session is a bunch of related process groups sharing the same session identifier (SID).
There is a session leader in these processes,
which is the process that created this session,
and the SID of this session is the PID of the session leader.

Whenever a new process is created,
its SID is its parent’s SID.

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

At any time, there are:

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

Terminal

There will be a session leader when a terminal is opened,
and the same session leader is also the controlling process.
Meanwhile, the foreground process group are waiting for the input from the terminal,
which could be user input or signals from the user,
and the background process groups exist at the same time.

When a terminal is terminated,
the kernel sends SIGHUP to the session leader to inform that the terminal is over.

Shell Job Control

Defining process groups and sessions are for an explanation for shell job control.
Here is a shell job control example:

The terminal user used to login is the controlling terminal,
and the login shell is both the session leader and the controlling process.
Commands from this shell will create a process or more,
these processes become new process groups,
and other processes created by these processes becomes part of the process groups.
All those processes are created from this shell and thus belong to this login session.

Daemon

What daemons have:

  • long-lived:
    Usually activated at the system started,
    running till the time system turned off.
  • Running at the background and has no controlling terminal:
    To make sure there will be not job control or terminal-related signal generated by the kernel to affect the daemon.

Daemons usually end with 'd'.

Several common daemons:

  • cron
  • sshd
  • httpd