The Linux process has a certain relationship with each other. For example, 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 becomes the process group ID, Pgid, to identify the process group.
$ps-O Pid,pgid,ppid,comm | Catpid 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 (Bash's pgid is labeled wrong):
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 added by the end of the&Way to let it run in the background:
$ping localhost > Log &[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 the front of the PgidPlus-to represent a pgid, not a PID.), one is sent to work 1 (%1), the two are equivalent.
A job can change from backstage work to foreground work 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.
Summarize:
Process Group, Pgid
Session, Sid, Job, Forground, background
FG, Kill-pid, &,%
Linux process groups and sessions