Linux Process Concepts
Table of Contents
- Parent, Child, Orphan and Zombie Process
- Process Groups, Sessions and Controlling Terminal
- Double Fork
- Pseudo Terminal Details
- Job Control Commands
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 callswait()
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 callsetpgid()
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:
- the grand parent process forks
- the parent process becomes a new session leader.
- the parent process executes a child process which is going to do the actual task.
- the parent process terminates
As a result:
- The actual process can't acquire TTY, because it's not a session leader.
- The actual process becomes an orphan process, which means it won't remain as a zombie.
Pseudo Terminal Details
As an example, ssh
works as follows:
ssh <host>
- connect to
host
and allocate apty
(pseudo terminal). ssh <host> <cmd>
- Just let
host
run<cmd>
, since it generally doesn't need to allocatepty
. ssh <host> tmux
- commands like
tmux
orscreen
needpty
to work properly, but the default won't do this. In this case, specify-t
to explicitly allocatepty
for the command.
Job Control Commands cli
jobs
disown
fg
,bg
wait
suspend
logout
times
kill
killall
command
builtin
enable
autoload
command
builtin
enable
autoload
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 |