Inter-process relationships: process, zombie process, orphan process, process group, foreground process Group, background process Group, orphan Process Group, session, control terminal

Source: Internet
Author: User
Tags session id terminates

Different shells are different in the order in which the sub-processes are created when using pipe lines, this article takes bash as an example, and is a typical representation of the shell that supports job control.

Zombie process with orphan process

Zombie Process: Terminates before the parent process, but the parent process does not dispose of it (gets the information about terminating the child process, releasing the resources it still occupies). The only way to eliminate a zombie process is to terminate its parent process.
Orphan process: The process's parent process terminates before itself. It is characterized by the ppid=1 (the ID of the init process). An orphan process can be a self-made orphan process group.

Abbreviations used in the text

PID = Process ID (generated by the kernel based on deferred reuse algorithm )
PPID = parent Process ID (can only be modified by the kernel)
Pgid = Process Group ID (child process, parent process can be modified)
SID = Session ID (the process itself can be modified, but with limitations, see below)
Tpgid= Control terminal process group ID (modified by control terminal to indicate the current foreground process group)

Relationship between processes, process groups, and sessions overall relationship

A process belongs to a process group, a process group belongs to a session, a session may or may not have a control terminal

    • Session First Process:
      When you create a new session, the only process in the session is PID=SID. It is usually a login shell, or it can be called setsid () to become a new session after the orphan process.
    • Session:
      A collection of one or more process groups. A login shell-initiated session, typically consisting of a session first process, a foreground process group, and a background process group.
Process Group

A collection of one or more processes that belong to a session. Fork () does not change the process group ID.

    • Process Group Leader:
      The PID is equal to the pgid process. The leader can change the process group ID of the child process so that it is transferred to another process group.
      For example, a shell process (as in bash, for example), when using a pipe line , such as echo "hello" | cat bash, sets the process group ID for all processes within that pipe line with the process ID of the first command. Process echo cat ID is set to the process group ID at this time echo .
    • Foreground Process Group
      A process group in which processes in the process group can read and write to terminal devices.
      The login shell (for example, bash) sets the foreground process group by calling the Tcsetpgrp () function, which associates the FD(file descriptor) of the end device with the specified process group. The process that becomes the foreground process group is its tpgid=pgid, and can often be judged by comparing them to the front and back process groups.
    • Background Process Group
      In one session, all process groups except the foreground process group and the first session process. The process in the process group can write to the terminal, but when attempting to read the terminal, it will receive the Sigttin signal and stop. The login shell can issue a message on the terminal based on the settings [1] informing the user that a process wants to read the terminal.
      The foreground process group ID can only have one, and the latter process group can exist multiple at the same time. The pgid≠tpgid of the background process group.
Orphan Process Group
    • Definition 1
      The parent process for each member of the group is either a member of the group or is not a member of the session that the group belongs to
    • Definition 2
      Not an orphan. The condition of the group is that there is one process in the group whose parent process belongs to another group in the same session.
      That is, terminating the parent process can make the process a zombie process Orphan (thanks to the Netizen "Hello"). This parent process is usually the leader of the process group because only its parent process is outside the process group, and the parent process of the other process (the child of the leader) is the ID of the leader process.
Parse: Generate a Zombie process (group) and read terminal
    1. The child process generated by the leader Fork () whose process group ID is unchanged (because fork () does not change the process group ID)
    2. If the team leader is bash, set the process group ID of the child process to the PID of the first command, which is the first command as the leader and a new process group
    3. In the new process group generated by bash, at least one of the process's ppid points to that bash, or the process is made up of an orphan process group that cannot notify bash of changes in process state
    4. Bash detects the state of a child process (a new process group) through the wait function family to determine how to set the foreground process group ID (to a specified terminal device)
    5. When a background process (group) attempts to read a control device, the terminal driver sends a sigttin signal to it, and the process (group) should be awakened by bash to enter the foreground
    6. For an orphan process (group), Bash is unaware of its status because bash does not know its PID, and the only process that knows its PID has terminated, and cannot know its group ID, so it cannot put its group ID into the foreground. If the orphan process (group) tries to read the terminal, the read () call fails and the errno is set to Eio.

[1] The stty tostop background process group can be prevented from writing to the terminal by using a command, and when a write request is made, a Sigttou signal is received.

(Lesca original, reproduced please specify transfer from

Copyright notice» Inter-process relationships: process, zombie process, orphan process, process group, foreground process Group, background process Group, orphan Process Group, session, control terminal
    • Lesca Last updated: June 13, 2011
    • All contents of this blog are published under "Attribution-Non-commercial use-consistent" Creative Commons agreement
    • The full text is reproduced, must contain this copyright information ; Partial reprint or quote, please indicate author's attribution and article Origin
    • This article link:

(go) Inter-process relationships: process, zombie process, orphan process, process group, foreground process Group, background process Group, orphan Process Group, session, control terminal

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.