Linux Process Concepts

Table of Contents

Parent, Child, Orphan and Zombie Process

Parent
  • A process which forks
Child
  • A process which is forked from another one
Orphan
  • When a parent process terminates, the running child becomes an orphan process.
  • init process (pid:1) adopt the process, this process is called re-parenting.
Zombie
  • A process completed its execution, but still exists in the process table.
  • Normally, a parent process should read its child's exit status by calling wait() system call
  • If the parent forgets to call wait(), the child process remains in the process table even after it has finished its execution.
  • Since init periodically calls wait() for terminated child processes, orphan processes won't remain as zombies.

Process Groups, Sessions and Controlling Terminal

Process Group
  • A collection of processes that permits the signaling of related processes.
  • A newly created process joins the process group of its creator.
  • When shells like bash run a command, it forks and call setpgid() to allocate a new process group, and make it the foreground process group if the shell was in foreground. stackexchange ref
Session
  • A collection of process groups
  • When attached to a terminal,
    • The terminal is now called as a controlling terminal
    • The session now has the foreground process group, which associated and interacts with the terminal
  • When a process calls setsid(), it becomes
    • the leader of the new session
    • the process group leader of the new process group
    • has no controlling terminal.

Double Fork

1. `Parent`    = PID: 28084, PGID: 28084, SID: 28046
2. `Fork#1`    = PID: 28085, PGID: 28084, SID: 28046
3. `Decouple#1`= PID: 28085, PGID: 28085, SID: 28085
4. `Fork#2`    = PID: 28086, PGID: 28085, SID: 28085

When executing a process:

  1. the grand parent process forks
  2. the parent process becomes a new session leader.
  3. the parent process executes a child process which is going to do the actual task.
  4. the parent process terminates

As a result:

Pseudo Terminal Details

As an example, ssh works as follows:

ssh <host>
connect to host and allocate a pty (pseudo terminal).
ssh <host> <cmd>
Just let host run <cmd>, since it generally doesn't need to allocate pty.
ssh <host> tmux
commands like tmux or screen need pty to work properly, but the default won't do this. In this case, specify -t to explicitly allocate pty for the command.

Job Control Commands cli

Notation Meaning
%N Job number [N]
%S Invocation (command-line) of job begins with string S
%?S Invocation (command-line) of job contains within it string S
%% "current" job (last job stopped in foreground or started in background)
%+ "current" job (last job stopped in foreground or started in background)
%- Last job
$! Last background process