In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "the introduction and creation method of Linux process". In daily operation, I believe many people have doubts about the introduction and creation method of Linux process. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "introduction and creation method of Linux process". Next, please follow the editor to study!
Process
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 first column PID (process IDentity) is an integer, and each process has a unique PID to represent its own identity, and the process 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 look at the first line, PID is 1, and the name is init. This process is generated by executing the file / bin/init. When Linux starts, init is the first process created by the system, which 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:
Class= "" > 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 of the first process sudo, while the third process ps is a child of the second process.
You can also display the entire process tree with the $pstree command:
Class= "> 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 only way to implement the Linux program.
At this point, the study of "introduction and creation method of Linux process" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.