What computers can actually do is essentially very simple , For example, calculate the sum of two numbers , Another example is to find an address in memory and so on . These basic computer actions are called instructions  (instruction). So called procedure (program), It's a collection of such a series of instructions . Through the program , We can make computers do complicated operations . Programs are mostly stored as executable files . Such an executable is like a recipe , The computer can make delicious food according to the menu .

that , Procedures and processes (process) What's the difference between ?

A process is a concrete implementation of a program . Only recipes don't work , We have to follow the instructions of the recipe step by step , To make dishes . A process is the process of executing a program , It's like following a recipe , The real process of cooking . The same program can be executed multiple times , Each time can open up a separate space in memory to load , And then there are multiple processes . Different processes can also have their own independent IO Interface .

An important function of the operating system is to provide convenience for the process , For example, allocating memory space for a process , Information about the management process, etc , It's like having a nice kitchen ready for us .


Take a look at the process

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

(-e Indicates that all processes are listed ,-o pid,comm,cmd It means that we need PID,COMMAND,CMD Information )


Each line represents a process . Each row is further divided into three columns . First column PID(process IDentity) It's an integer , Every process has a unique PID To represent yourself , The process can also be based on PID To identify other processes . Second column COMMAND It's short for this process . The third column CMD The parameters of the program and the corresponding process are .

( The third column has some brackets [] The enclosed . They are part of the kernel function , Dressed as a process to facilitate the management of the operating system . We don't have to think about them .)


Let's look at the first line ,PID by 1, The name is init. The process is to execute /bin/init This file ( Program ) Generated . When Linux start-up When ,init Is the first process created by the system , This process will always exist , Until we shut down the computer . This process is of special importance , We'll keep talking about it .


How to create a process

actually , When the computer is turned on , kernel (kernel) Only one init process .Linux The kernel does not provide the ability to create new processes directly system call . All the rest of the process is init Process passing fork The mechanism is established . The new process has to copy itself through the old process , This is it. fork.fork It's a system call . Processes live in memory . Each process allocates its own space in memory (address space). When a process fork When ,Linux Open up a new memory space in memory for new processes , And copy the contents of the old process space to the new one , After that, the two processes run at the same time .

The old process becomes the parent of the new process (parent process), And the corresponding , The new process is the child process of the old process (child process). There is only one process PID outside , There will be another PPID(parent PID) To store the parent process of PID. If we follow PPID Keep going up , Always find the source is init process . So , All the processes also constitute a process with init Is the tree structure of the root .

as follows , We look up the current shell Under the process of :


[email protected]:~# ps -o pid,ppid,cmd
16935  3101 sudo -i
16939 16935 -bash
23774 16939 ps -o pid,ppid,cmd

We can see , The second process bash It's the first process sudo Can be inherited by child processes. , And the third process ps Is a child of the second process .


You can also use $pstree Command to display the entire process tree :


     │                └─2*[{NetworkManager}]
     │         └─2*[apache2───26*[{apache2}]]
     │                ├─bash───pstree
     │                ├─gnome-pty-helpe
     │                ├─sh───R───{R}
     │                └─3*[{gnome-terminal}]


fork Usually called as a function . This function will return twice , Put the child process's PID Return to parent process ,0 Return to child process . actually , A child process can always query its own data PPID To know who your father is , such , A pair of parent and child processes can query each other at any time .

Usually called fork After the function , The program will design a if Selection structure . When PID be equal to 0 when , Indicates that the process is a child process , So let it execute certain instructions , For example, using exec Library function (library function) Read another program file , And execute in the current process space ( This is actually what we use fork One of the main purposes of : Create a process for a program ); And when PID The timing is an integer , Description is the parent process , Then execute some other instructions . thus , After the child process is established , Let it perform functions different from the parent process .


The end of a subprocess (termination)

When the child process ends , It notifies the parent process , And empty the memory that you occupy , And leave your own exit message in the kernel (exit code, If it works , by 0; If there are errors or anomalies , by >0 The integer of ). In this message , Will explain why the process exited . When the parent process learns that the child process is ending , Is responsible for using wait system call . This wait Function can get the exit information of the child process from the kernel , And clear the space occupied by the information in the kernel . however , If the parent process terminates earlier than the child process , The child process becomes an orphan (orphand) process . The orphan process will be passed on to init process ,init The process becomes the parent of the process .init The process is responsible for calling... At the end of the child process wait function .

Of course , A bad program may also cause the exit information of the child process to stay in the kernel ( The parent process does not call the child process wait function ), In this case , Child processes become zombies (zombie) process . When a lot of zombie processes accumulate , Memory space will be squeezed .


Processes and threads (thread)

Although in UNIX in , Processes and threads are related but different , But in Linux in , A thread is just a special process . Multiple threads can share memory space and IO Interface . therefore , The process is Linux The only way to implement a program .



Program , process ,PID, Memory space

Subprocesses , The parent process ,PPID,fork, wait