Linux Process Basics
Vamei Source: Http://www.cnblogs.com/vamei Welcome reprint, Please also keep this statement. Thank you!
What a computer can actually do is essentially very simple, such as calculating the number of two numbers, and then finding an address in memory, among other things. These most basic computer actions are called Instructions (instruction). The so-called program , is the set 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 recipes.
So What is the difference between a program and a process?
A process is a concrete implementation of a program . Only recipes are no use, we have to follow the recipes of the actual steps to implement, in order to make dishes. A process is the process of executing a program, similar to the process of actually cooking a dish according to a recipe. The same program can be executed multiple times, each time in memory to open up a separate space to load, resulting in multiple processes. Different processes can also have their own independent IO interfaces.
An important function of the operating system is to facilitate the process, such as allocating memory space for the process, information about the management process, and so on, as if we had prepared a fine kitchen.
Take a peek at the process
First, we can use the $ps command to query a running process, such as $ps-eo pid,comm,cmd, to execute the result:
(-e means list all processes,-o pid,comm,cmd means we need pid,command,cmd information)
Each row represents a process. Each line is divided into three columns. The first column of the PID(Process Identity) is an integer, each process has a unique PID to represent its own identity, the process can also be based on the PID to identify other processes. The second column of command is the abbreviation for this process. The third column of CMD is the program that the process corresponds to and the parameters that the runtime takes.
(The third column is enclosed in brackets [].) They are part of the kernel feature that is dressed up as a process to facilitate the management of the operating system. We don't have to think about them. )
We look at the first line, PID is 1, the name is init. This process is generated by executing the/bin/init file (program). When Linux starts ,Init is the first process created by the system, and this process persists until we shut down the computer. This process is of particular importance and we will keep mentioning it.
How to create a process
In fact, when the computer is powered on, the kernel (kernel) only establishes an INIT process. Linux kernel does not provide a system call to create a new process directly. All the remaining processes are created by the Init process through the fork mechanism. The new process has to replicate itself through the old process , which is the fork. Fork is a system call. The process survives in memory. Each process allocates its own piece of space in memory (address space). When the process is fork, Linux opens up a new memory space in memory to the new process, and copies the contents of the old process space into the new space, after which two processes run concurrently.
The old process becomes the parent process of the new process, and, accordingly, the new process is the child process of the old process. A process In addition to a PID, there will also be a PPID (parent PID) to store the parent process PID. If we follow the ppid, we will always find that its source is the init process. Therefore, all processes also constitute a tree-like structure with the root of init.
Below, we query the process under the current shell:
PS-o pid,ppid,cmd pid ppid cmd16935 sudo-i16935-bashps-o pid,ppid, CMD
As we can see, the second process bash is the child process of the first process sudo, and the third process PS is the child process of the second process.
You can also use the $pstree command to display the entire process tree:
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 two times, returns the PID of the child process to the parent process, and 0 returns to the child process. In fact, child processes can always query their own ppid to know who their parent process is, so that a pair of parent processes and child processes can query each other at any time.
Usually after the fork function is called, the program designs an if selection structure. When the PID equals 0 o'clock, indicating that the process is a subprocess, let it execute certain instructions, such as using the Exec library function (library functions) to read another program file and execute it in the current process space (this is actually one of our main purposes of using fork: Create a process for a program), and when the pid is a positive integer, the parent process is executed, and some additional instructions are performed. This allows the child process to perform a different function than the parent process after it has been established.
End of child process (termination)
When the child process is terminated, it notifies the parent process and empties the memory it occupies and leaves its own exit message in the kernel (exit code, 0 if it runs smoothly, or an integer that is >0 if there is an error or an exception condition). In this message, it explains why the process exited. When the parent process learns that the child process is terminated, it is the responsibility to use the wait system call for that child process . This wait function takes out the exit information of the child process from the kernel and empties the space occupied by the information in kernel. However, if the parent process is older than the child process end, the child process becomes an orphan (orphand) process. The orphan process is passed on to the Init process, and the Init process becomes the parent process of the process. The INIT process calls the wait function when the child process is terminated.
Of course, a bad program can also completely 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 (zombie) process. When a large number of zombie processes accumulate, the memory space is squeezed out.
process and thread (thread)
Although in Unix, the process and the thread are connected but different two things, but in Linux, the thread is only a special process. Memory space and IO interfaces can be shared between multiple threads. Therefore, a process is the only way to implement a Linux program.
Summary
program, process, PID, memory space
Child process, parent process, ppid,fork, wait
Welcome to the Linux concept and System series article
Linux Process Basics