First, fork basic knowledge
A process, including code, data, and resources assigned to the process. The fork () function creates a process that is almost identical to the original process through a system call, that is, two processes can do exactly the same thing, but two processes can do different things if the initial parameters or the variables passed in are different.
After a process calls the fork () function, the system first assigns resources to the new process, such as space for storing data and code. All the values of the original process are then copied to the new new process, with only a few values that are different from the value of the original process. The equivalent of cloning a self.
The fork () function Gets the child process that inherits all the system resources of the parent process, including the code snippet, data area, constant area, and so on. The address space of the parent-child process begins to be shared, and only if any one of the parent-child processes attempts to modify the contents of it, copy on write. Show that the view is irrelevant to each other.
See a simple example:
#include <unistd.h> #include <stdio.h>int main () {pid_t fpid;//fpid represents the value returned by the fork function int count=0;fpid=fork (); if (Fpid < 0) printf ("Error in fork!"); else if (Fpid = = 0) {printf ("I am the child process, my process ID is%d\n", getpid ()); count++;} else {printf ("I am the parent process, my process ID is%d\n", getpid ()); count++;} printf ("Count is:%d\n", count); return 0;}
Results:
I am the parent process, my process ID is 5573
Count:1I am the child process and my process ID is 5574Count:1
Why the fpid of two processes is different, which is related to the characteristics of the fork function.
A wonderful thing about a fork call is that it is called only once, but it can return two times, and it may have three different return values:
1. In the parent process, fork returns the process id;2 for the newly created child process. In the subprocess, fork returns 0;3. If an error occurs, fork returns a negative value;
There are two possible reasons for fork errors: 1. The current number of processes has reached the system-specified limit, when the value of errno is set to Eagain. 2. System memory is low, then the value of errno is set to Enomem.
after a successful creation of a new process, there are two fundamentally identical processes in the system, which do not have a fixed sequencing and which process first executes the process scheduling policy to look at the system. Each process has a unique (distinct) process ID, which can be obtained through the getpid () function, a variable that records the PID of the parent process, and the value of the variable can be obtained through the getppid () function.
After the fork is executed, the variable for process 1 is count=0,fpid! =0 (parent process). The variable for process 2 is count=0,fpid=0 (child process), the variables of both processes are independent, there are different addresses, not shared, and this should be noted. We can say that we are using fpid to identify and manipulate parent-child processes. Others may wonder why the code is not copied from # include, because Fork is a copy of the current situation of the process, and when the fork is executed, the process has finished executing int count=0;fork only the next code to execute to the new process.
Second, Fork Advanced
Look at the code first:
#include <unistd.h> #include <stdio.h>int main () { int i=0; printf ("I son/pa ppid pid fpid\n"); Ppid refers to the current process of the parent process PID //pid refers to the current process of the PID, //fpid refers to the fork returned to the current process value for (i=0;i<2;i++) { pid_t fpid=fork (); if (fpid==0) printf ("%d child %4d%4d%4d\n", I,getppid (), Getpid (), fpid); else printf ("%d parent%4d%4d%4d\n", I,getppid (), Getpid (), fpid); } return 0;}
Results:
I son/pa ppid pid fpid 0 Parent 2043 3224 3225 0 child 3224 3225 0 1 parent 2043 3224 3226
1 parent 3224 3225 3227 1 child 1 3227 0 1 child 1 3226 0
First step: In the parent process, the instruction executes into the For loop, i=0, and then after execution Fork,fork executes, there are two processes in the system, namely p3224 and p3225 (which I use pxxxx to represent the process ID of xxxx). You can see that the parent process p3224 is p2043, and the parent process p3225 the child process is exactly p3224. We use a linked list to represent this relationship:
p2043->p3224->p3225
Is it possible for readers to notice that p3226,p3227 's parent process should not be p3224 and p3225, and how can it be 1? Here we have to talk about the process of creating and dying, and after p3224 and p3225 are done with the second loop, the main function should quit, that is, the process is dead, because it has done everything. p3224 and p3225 died, p3226,p3227 there is no parent process, which is not allowed in the operating system, so p3226,p3227 's parent process is set to P1, P1 will never die.
To summarize, this program executes the following process:
Also, if you remove the "\ n" after each program runs different results, which is related to the buffer mechanism of printf, printf some content, the operating system just put the content in the stdout buffer queue, and did not actually write to the screen. However, as soon as you see a \ n, the stdout is refreshed immediately, so you can print it right away.
printf ("fork!") was run After, "fork!" Just put in the buffer, the program runs to the fork when the buffer inside the "fork!" quilt process copied past. Therefore, there is also fork! in the stdout buffer of the child process. So, what you finally see is fork!. was printf 2 times!!!
And after running printf ("fork!\n"), "fork!" is immediately printed on the screen, and then the stdout buffer in the sub-process to fork will not have fork! Content. So the results you see will be fork!. was printf 1 times!!!
Finally, take a look at the total number of processes created by the following program:
#include <stdio.h> #include <unistd.h>int main (int argc, char* argv[]) { fork (); Fork () && fork () | | Fork (); Fork (); return 0;}
The answer is a total of 20 processes. Fork () && fork () | | The explanation of fork () is as follows:
Fork () && fork () | | Fork (), additional fork out of 4 processes. The return value of the child process is 0.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
A detailed description of the fork function in Linux