Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Introduction to the relationship between Linux processes

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains the "introduction of the relationship between Linux processes". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn the "introduction to the relationship between Linux processes".

The processes of Linux are related to each other. For example, in the Linux process base, we see that each process has a parent process, and all processes take the init process as the root, forming a tree structure. Here we talk about process groups and sessions to manage processes in a richer way.

Process Group (process group)

Each process belongs to a process group (process group), and each process group can contain multiple processes. The process group will have a process group leader process (process group leader), and the PID leading the process (PID see Linux process basis) becomes the ID (process group ID, PGID) of the process group to identify the process group.

The code is as follows:

$ps-o pid,pgid,ppid,comm | cat

PID PGID PPID COMMAND

17763 17763 17751 bash

18534 18534 17763 ps

18535 18534 17763 cat

PID is the ID,PGID of the process itself is the ID of the process group in which the process belongs, and PPID is the parent process ID of the process. From the above results, we can infer the following relationship:

The arrows in the figure indicate that the parent process generates child processes through fork and exec mechanisms. Ps and cat are both child processes of bash. The PID of the lead process of the process group becomes the process group ID. The leadership process can end first. The process group still exists and holds the same PGID until the end of the last process in the process group.

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

Session (session)

Further, multiple process groups can form a session (session) on the premise that shell supports work control (job control). Bash (Bourne-Again shell) supports work control, while sh (Bourne shell) does not.

The session is established by the process within it, which is called the session leader process (session leader). The PID of the session leader process becomes the SID (session ID) that identifies the session. Each process group in a session is called a job. A session can have one process consisting of foreground work (foreground) for the session, while other process groups are background work (background). Each session can be connected to a control terminal (control terminal). When the control terminal has input and output, it is passed to the foreground process group of the session. Signals generated by the terminal, such as CTRL+Z, CTRL+\, are passed to the foreground process group.

The significance of the conversation is to include multiple jobs in a terminal and take one of them as a foreground to directly receive the input and output of the terminal as well as the terminal signal. Other work runs in the background.

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

The code is as follows:

$ping localhost > log &

The terminal displays:

The code is as follows:

[1] 10141

1 in parentheses indicates work number, while 10141 is PGID

We query for more detailed information in the following ways:

The code is as follows:

$ps-o pid,pgid,ppid,sid,tty,comm

(tty stands for control terminal)

The signal can pass through kill

The code is as follows:

$kill-SIGTERM-10141

Or

The code is as follows:

$kill-SIGTERM 1

To send it to the working group. The above two commands, one to send to PGID (a PGID instead of PID by adding-before PGID) and the other to work 1 (% 1), are equivalent.

A job can be changed from background work to foreground work through $fg:

The code is as follows:

$cat > log &

$fg 1

When we run the first command, we cannot enter the command because we are working in the background, and we cannot enter the cat command until we bring the work to the foreground. After the input is complete, press CTRL+D to inform shell that the input is over.

The concept of process group (work) is relatively simple and easy to understand. The session is mainly established for a terminal. When we open multiple terminal windows, we actually create multiple terminal sessions. Each session has its own foreground and background work. In this way, we add a level of management and operation to the process. In the absence of a graphical interface, sessions allow users to initiate and manage multi-level processes through shell. For example, I can initiate multiple background work through shell, and the standard input and output is not occupied, I can still continue to do other work. Today, graphical interfaces can help us address this need, but workgroups and conversation mechanisms are still used in many parts of Linux.

Thank you for your reading, the above is the content of "introduction of the relationship between Linux processes". After the study of this article, I believe you have a deeper understanding of the introduction of the relationship between Linux processes, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report