Author: vamei Source: http://www.cnblogs.com/vamei welcome reprint, please also keep this statement. Thank you!
What a computer can actually do is actually very simple, such as calculating the sum of two numbers, and finding an address in the memory. These basic computer actions are calledCommand(Instruction ). The so-calledProgram(Is a collection of such a series of commands. Through the program, we can let the computer complete complex operations. Programs are mostly stored as executable files. Such an executable file is likeRecipeThe computer can make delicious meals according to the recipe.
So,ProgramAndProcessWhat is the difference between process?
A process is a programImplementation. Only when recipes are useless can we make dishes by following the instructions of recipes. Process isExecution ProgramThe process is similar to the process of cooking according to recipes. The same program can be executed multiple times, and each time it can open up an independent space in the memory for loading, thus generating multiple processes. Different processes can also have their own independent Io interfaces.
An important feature of the Operating System is to provide convenience for processes, such as allocating memory space for processes and managing process-related information. It is like preparing a beautiful kitchen for us.
1. Take a look at the process
First, we can use$ PSCommand to query running processes, such$ PS-eo pid, comm, cmd, Is the execution result:
(-E Indicates listing all processes,-o pid, comm, CMD indicates we need PID, command, CMD Information)
Each line represents a process. Each row is divided into three columns. First columnPID(Process Identity)Is an integer, each process has a unique PID to represent its ownIdentityThe process can also identify other processes based on the PID. The second column of command is short for this process. The third column CMD isThe program corresponding to the ProcessAnd the runtime parameters.
(The third column contains some brackets. They are part of the features of the kernel and are dressed as processes to facilitate operating system management. We don't have to consider them .)
In the first line, the PID is 1 and the name is init. This process is generated by executing the/bin/init file (Program. WhenLinux StartupWhen,InitIs the first process created by the system. This process will always exist until we close the computer. This process has special importance and we will keep mentioning it.
2. How to create a process
In fact, when the computer is started, only one INIT process is established in the kernel. LinuxKernel does not provide a system call to directly create a new process.All the remaining processes are established by the INIT process through the fork mechanism. New processes must pass through Old ProcessesCopy itselfAnd this isFork. Fork is a system call.. The process is alive in the memory. Each process has its own address space in the memory ). When the fork process is running, Linux creates a new memory space in the memory for the new process, and copies the content in the old process space to the new space, then the two processes run simultaneously.
The old process becomes the parent process of the new process. Correspondingly, the new process is the child process of the old process ). In addition to a PID, a process also hasPpid(Parent PID) to store the parent process PID. If we keep tracing the ppid, we will always find that the source is the INIT process. Therefore, all processes also constitute a tree structure with init as the root.
Query the processes in the current shell as follows:
Root @ vamei :~ #PS-O pid, ppid, cmd pid ppid cmd169353101 Sudo-I16939 16935-Bash23774 16939 PS-O pid, ppid, cmd
We can see that the second process Bash is the sub-process of the first process sudo, and the third process PS is the sub-process of the second process.
You can also use$ PstreeCommand to display the entireProcess tree:
init-inform-NetworkManager-inform-dhclient │ your-2 * [{NetworkManager}] Your-accounts-daemon ── {accounts-daemon} Your-acpid restart-apache2-┬ ── apache2 │ ─ ── 2*[apache2 ── 26 * [{apache2}] ── at-SPI-bus-laun ── 2 * [{at-SPI-bus -laun}] ├ ── ATD ── avahi-daemon ── +thd +─ colord ── 2 * [{colord}] ├ ── console-kit-dae ── -64 * [{console-kit-dae}] ├ ── cron ‑cupsd ── 2 * [prop] ├ ── 2 * [prop-daemon] ├ ── prop-launch restart -dconf-service ── 2 * [{dconf-service}] ── Dropbox ── 15 * [{Dropbox}] ── Firefox ── 27 * [{Firefox}] Then -gconfd-2 ‑geoclue-master ‑6 * [Getty] ‑gnome-keyring-D ── 7 * [{gnome-keyring-d}] ── gnome-terminal-mongo── bash │ mongo─ bash ── pstree │ mongo── gnome-Pty-Helpe │ mongo── sh ── {r} │ mongo── 3 * [{gnome-Terminal }]
Fork is usually called as a function. This function will haveTwo responsesThe child process PID is returned to the parent process, and 0 is returned to the child process. In fact, a child process can always query its own ppid to know who its parent process is. In this way, a pair of parent and child processes can query each other at any time.
After the fork function is called, the program will design an if selection structure. WhenPID is equal to 0Indicates that the process is a sub-process, so let it execute some commands, such as usingExecLibrary Function reads another program file and executes it in the current process space (this is actually a major purpose of using fork: to create a process for a program ).PID is a positive integer.If it is a parent process, other commands are executed. Therefore, after the child process is created, it can execute different functions from the parent process.
3. Termination)
When the child process ends, it notifies the parent process, clears the memory occupied by the child process, and leaves its ownExit Information(Exit code. If it runs smoothly, it is 0; if there is an error or exception, it is an integer greater than 0 ). In this information, it explains why the process exits. The parent process has the responsibility to useWaitSystem Call. This wait function can beObtains the exit information of a sub-process.And clear the space occupied by the information in the kernel. However, if the parent process is terminated earlier than the child process, the child process becomesOrphan(Orphand) process. The orphan process is passed to the INIT process, and the INIT process becomes the parent process of the process. The INIT process is responsible for calling the wait function when the child process ends.
Of course, a bad program may also cause sub-processes.The exit information is stuck in the kernel.(The parent process does not call the wait function for the child process). In this case, the child process becomesBotnets(Zombie) process. When a large number of zombie processes accumulate, the memory space will be occupied.
4. Process and thread)
In UNIX, a process and a thread are two different things, but in Linux, a thread is just a special process. Multiple Threads can share memory space and I/O interfaces. Therefore, processes are the only Implementation Method of Linux programs.
Summary:
Program, process, PID, memory space
Child process, parent process, ppid, fork, wait