Linux process groups and sessions

Source: Internet
Author: User
Tags session id

The Linux process has a certain relationship with each other. For example, on the basis of the Linux process, we see that each process has a parent process, and all processes are rooted in the init process, forming a tree structure. We are here to explain the process groups and sessions so that we can manage the process in a richer way.

Process Group

Each process belongs to a process group, and each process group can contain multiple processes. The process group will have a process group lead process (leader), and the PID of the lead process (PID see Linux Process Foundation) becomes the ID of the process group (the Process group ID, pgid) to identify processes groups.

$ps-O Pid,pgid,ppid,comm | Cat

  PID  pgid  PPID COMMAND17763 17763 17751 bash18534 18534 17763 ps18535 18534 17763 Cat


The PID is the id,pgid of the process itself, the ID of the process group in which the process is located, and ppid the process's parent process ID. From the above results, we can extrapolate the following relationship:

The arrows in the diagram indicate that the parent process produces child processes through the fork and exec mechanisms. Both PS and cat are child processes of bash. The PID of the process group's lead process becomes the process group ID. The leadership process can be terminated first. At this point the process group still exists and holds the same pgid until the last process in the process group is terminated.

One important reason we classify some processes as process groups is that we can send signals to a process group. All processes in the process group will receive the signal. We will discuss this in depth in the next section.

Sessions (session)

Further, multiple process groups can also form a session if the shell supports job control . Bash (Bourne-again Shell) supports work control, while SH (Bourne shell) is not supported.

A session is established by the process in which it is called the session's lead process (sessions leader). The PID of the session leader process becomes the SID (Session ID) of the recognition session. Each process group in a session is referred to as a work (job). A session can have one process group as the foreground work for the session (foreground), while the other process groups are background work (background). Each session can be connected to a control terminal. The foreground process group that is passed to the session when the control terminal has input and output. signals generated by the terminal, such as CTRL + Z, ctrl+\, are passed to the foreground process group.

The meaning of the session is to include multiple tasks in a terminal and take one of them as the foreground to receive the input and output of the terminal and the terminal signal directly. Other work runs in the background.

A command can be run in the background by adding & to the end:

$ping localhost > Log &

At this point the terminal displays:

[1] 10141

1 in parentheses means work number, while 10141 is Pgid

We query for more detailed information in the following ways:

$PS-O Pid,pgid,ppid,sid,tty,comm

(TTY indicates control terminal)

The signal can be passed by kill

$kill-sigterm-10141

Or

$kill-sigterm%1

To send to the workgroup. The above two commands, one is sent to Pgid (by adding in front of pgid -to indicate is a pgid rather than PID), one is sent to work 1 (% 1), the two are equivalent.

A job can change from a background job to a foreground job by $FG:

$cat > Log &

$FG%1

When we run the first command, we cannot enter the command because we are working in the background, until we bring the work into the foreground before we can enter it into the cat command. When the input is complete, press Ctrl+d to tell the shell to end the input.

The concept of a process group (work) is simpler and easier to understand. The session is primarily built for a terminal. When we open multiple terminal windows, we actually create multiple terminal sessions. Each session will have its own foreground work and background work. In this way, we increase the level of management and operation for the process . In an age without graphical interfaces, sessions allow users to initiate and manage multiple layers of processes through the shell. For example, I can initiate multiple background work through the shell, and when the standard input and output is not occupied, I can still continue with other work. Today, a graphical interface can help us address this need, but workgroup and session mechanisms are still being applied in many parts of Linux.

Linux process groups and sessions

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: info-contact@alibabacloud.com 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.