Vamei Source: Http://www.cnblogs.com/vamei Welcome reprint, Please also keep this statement. Thank you!
Procedures and processes
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.
How to create a process
In fact, when the computer is powered on, the kernel (kernel) only establishes an INIT process. The 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:
[Email protected]:~# ps-o pid,ppid,cmd pid ppid CMD16935 3101 sudo-i16939 16935-bash23774 16939 ps-o pid,p Pid,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.
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.
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.
Summarize
program, process, PID, memory space
Child process, parent process, ppid,fork, wait
Linux Process Basics