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

What are the basics of the Linux process

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "what are the basic knowledge of the Linux process". 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 "what are the basic knowledge of the Linux process".

What a computer can actually do is essentially very simple, such as calculating the sum of two numbers, finding an address in memory, and so on. These most basic computer actions are called instruction. The so-called program is a collection of such a series of instructions. Through the program, we can let the computer complete the complex operation. Programs are stored as executable files most of the time. Such an executable file is like a recipe, and the computer can make delicious meals according to the recipe.

So what's the difference between a program and a process (process)?

A process is a concrete implementation of a program. Only when recipes are useless and we always follow the instructions of recipes really step by step can we make dishes. A process is the process of executing a program, similar to the process of actually cooking according to a recipe. The same program can be executed many times, each time opening up a separate space in memory to load, resulting in multiple processes. Different processes can also have their own independent IO interfaces.

One of the important functions of the operating system is to provide convenience for the process, such as allocating memory space for the process, managing process-related information, and so on, as if preparing a beautiful kitchen for us.

Take a look at the progress

First, we can use the $ps command to query the running process, such as $ps-eo pid,comm,cmd. The following figure shows the execution result:

(- e for listing all processes,-o pid,comm,cmd for we need PID,COMMAND,CMD information)

Each line represents a process. Each row is divided into three columns. The * column PID (process IDentity) is an integer. Each process has a * PID to represent its own identity. Processes can also identify other processes based on PID. The second column, COMMAND, is the abbreviation for this process. The third column CMD is the program corresponding to the process and the parameters that come with it at run time.

(the third column has some enclosed in brackets []. They are part of the kernel and are dressed as processes for easy management by the operating system. We don't have to think about them.)

Let's see * line, PID is 1, the name is init. This process is generated by executing the file / bin/init. When Linux starts, init is a system-created process that will exist until we shut down the computer. This process is of special importance and we will refer to it again and again.

How to create a process

In fact, when the computer is powered on, only one init process is established by the kernel. The Linux kernel does not provide system calls that directly create new processes. All remaining processes are set up by the init process through the fork mechanism. The new process copies itself through the old process, which is called fork. Fork is a system call. The process lives in memory. Each process allocates its own address space in memory. When a process fork, Linux opens up a new memory space in memory for the new process, and copies the contents of the old process space to the new space, and then the two processes run simultaneously.

The old process becomes the parent process (parent process) of the new process, and accordingly, the new process is the child process (child process) of the old process. In addition to having a PID, a process also has a PPID (parent PID) to store the parent process PID. If we follow the PPID upward, we will always find that the source is the init process. So, all processes also form a tree structure with init as its root.

We query the processes under the current shell as follows:

Root@vamei:~# ps-o pid,ppid,cmd PID PPID CMD16935 3101 sudo-i16939 16935-bash33774 16939 ps-o pid,ppid,cmd

We can see that the second process bash is a child process of * process sudo, while the third process ps is a child process of the second process.

You can also display the entire process tree with the $pstree command:

Init ─┬─ NetworkManager ─┬─ dhclient │ └─ 2 * [{NetworkManager}] ├─ accounts-daemon ─── {accounts-daemon} ├─ acpid ├─ apache2 ─┬─ apache2 │ └─ 2 * [apache2 ─── 26 * [{apache2}]] ├─ at-spi-bus-laun ─── 2 * [{at-spi-bus-laun}] ├─ atd ├─ avahi-daemon ─── avahi-daemon ├─ bluetoothd colord ─── 2* [{colord}] ├─ console-kit-dae ─── 64* [{console-kit-dae}] ├─ cron ├─ cupsd ─── 2* [dbus] ├─ 2* [dbus-daemon] ├─ dbus-launch ├─ dconf-service ─── 2* [{dconf-service}] ├─ dropbox ─── 15 * [{dropbox}] ├─ firefox ─── 27 * [{firefox}] ├─ gconfd-2 ├─ geoclue-master ├─ 6 * [getty] ├─ gnome-keyring-d ─── 7 * [{gnome-keyring-d}] ├─ gnome-terminal ─┬─ bash │ ├─ bash ─── pstree │ ├─ gnome- Pty-helpe │ ├─ sh ─── R ─── {R} │ └─ 3 * [{gnome-terminal}]

Fork is usually called as a function. This function returns twice, returning the PID of the child process to the parent process and 0 to the child process. In fact, child processes can always query their own PPID to find out who their parent process is, so that a pair of parent and child processes can query each other at any time.

Usually after calling the fork function, the program designs an if selection structure. When PID equals 0, the process is a child process, then let it execute some instructions, such as using the exec library function (library function) to read another program file and execute it in the current process space (which is actually one of the main purposes of using fork: to create a process for a program); and when PID is a positive integer, indicating the parent process, then execute some other instructions. This allows the child process to perform functions that are different from the parent process after it has been established.

Termination of child processes (termination)

When the child process terminates, it notifies the parent process, empties the memory it occupies, and leaves its own exit information in the kernel (exit code, 0 if running smoothly; integer > 0 if there is an error or exception). In this information, it explains why the process exited. When the parent process knows that the child process is terminated, it has the responsibility to use the wait system call on the child process. This wait function takes the exit information of the child process from the kernel and clears the space occupied by that information in the kernel. However, if the parent process terminates earlier than the child process, the child process becomes an orphand process. The orphan process is passed on to the init process, and the init process becomes the parent of the process. The init process is responsible for calling the wait function when the child process terminates.

Of course, a bad program may also cause the exit information of the child process to remain in the kernel (the parent process does not call the wait function on the child process), in which case the child process becomes a zombie process. When a large number of zombie processes accumulate, memory space is squeezed.

Processes and threads (thread)

Although process and thread are two different things related to each other in UNIX, thread is just a special process in Linux. Memory space and IO interfaces can be shared among multiple threads. Therefore, the process is the implementation of the Linux program.

Thank you for your reading, the above is the content of "what are the basic knowledge of the Linux process". After the study of this article, I believe you have a deeper understanding of the basic knowledge of the Linux process, 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