Linux fork () function reprint ~ ~ ~

Source: Internet
Author: User

Transfer from:: http://blog.csdn.net/jason314/article/details/5640969

First, Fork Introduction 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. And then put all the values of the original process

Copied to a new new process, only a few values are different from the value of the original process. The equivalent of cloning a self.

Let's look at an example:

#include <unistd.h>#include<stdio.h>intMain () {pid_t fpid;//Fpid represents the value returned by the fork function    intCount=0; Fpid=Fork (); if(Fpid <0) printf ("Error in fork!"); Else if(Fpid = =0) {printf ("I am the child process and my process ID is%d/n", Getpid ()); printf ("I am father's son/n");//for some people, Chinese looks more straightforward. count++; }      Else{printf ("I am the parent process, my process ID is%d/n", Getpid ()); printf ("I'm a child, his father/n"); Count++; } printf ("The statistical result is:%d/n", Count); return 0; }  

The operating result is:
I am the child process and my process ID is 5574
I'm the father's son.
The statistic results are: 1
I am the parent process, my process ID is 5573
I'm the kid, his father.
The statistic results are: 1


Before the statement fpid=fork (), only one process is executing this code, but after this statement, it becomes two processes executing, the two processes are almost identical,

The next statement that will be executed is the IF (fpid<0) ...
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 of the newly created child process;
2) in the sub-process, fork returns 0;
3) If an error occurs, fork returns a negative value;

After the fork function finishes executing, if the new process is created successfully, there are two processes, one child process and one parent process. In a child process, the fork function returns 0, in the parent process,

Fork returns the process ID of the newly created child process. We can determine whether the current process is a child process or a parent process by the value returned by the fork.

Quote a netizen to explain why the value of Fpid is different in the parent-child process. "In fact, the equivalent of a linked list, the process formed a list, the parent process fpid (p means point) to the process ID of the child process,

Because the child process has no child processes, its fpid is 0.


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 executes, two processes appear,

Some people say that the content of the two process is exactly the same Ah, how to print the result is not the same ah, that is because the reason for judging the conditions, the above list is only the process of code and instructions, and variables ah.


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), and the variables for both processes are independent,

There are different addresses that are not shared, so be aware of this. 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 executed int count=0;

Fork only copies the next code to execute to the new process.

Second, fork advanced knowledge

Let's look at a piece of code:

#include <unistd.h>#include<stdio.h>intMainvoid)  {     intI=0; printf ("i son/pa ppid pid fpid/n"); //Ppid refers to the parent process PID of the current process//PID refers to the PID of the current process,//fpid refers to the value that the fork returns to the current process    for(i=0;i<2; i++) {pid_t fpid=Fork (); if(fpid==0) printf ("%d child%4d%4d%4d/n", I,getppid (), Getpid (), fpid); Elseprintf ("%d parent%4d%4d%4d/n", I,getppid (), Getpid (), fpid); }     return 0; }  

The operating result is:
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


This code is interesting, let's analyze it carefully:
First step: In the parent process, the instruction executes into the For loop, i=0, and then after execution Fork,fork executes, two processes appear in the system, namely p3224 and p3225

(I use PXXXX to indicate 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
After the first fork, the variable for the p3224 (parent process) is i=0,fpid=3225 (the fork function returns to the child process ID in the parent process), with the following code:

 for(i=0;i<2; i++) {pid_t fpid=fork ();//done, i=0,fpid=3225.    if(fpid==0) printf ("%d child%4d%4d%4d/n", I,getppid (), Getpid (), fpid); Elseprintf ("%d parent%4d%4d%4d/n", I,getppid (), Getpid (), fpid); }  return 0;

The variable for the p3225 (child process) is i=0,fpid=0 (the fork function returns 0 in the subprocess), and the Code content is:

 for(i=0;i<2; i++) {pid_t fpid=fork ();//done, i=0,fpid=0.    if(fpid==0) printf ("%d child%4d%4d%4d/n", I,getppid (), Getpid (), fpid); Elseprintf ("%d parent%4d%4d%4d/n", I,getppid (), Getpid (), fpid); }  return 0;

So print out the results:
0 Parent 2043 3224 3225
0 Child 3224 3225 0
The second step: assume that the parent process p3224 first execution, when entering the next loop, I=1, then the fork, and then a new process p3226 in the system, for the parent process at this time,

p2043->p3224 (current process)->p3226 (the child process being created).
For child process p3225, after the first loop is executed, i=1, then fork, a new process p3227 is added to the system, p3224->p3225 (the current process)->p3227 (the child process being created).

From the output you can see that p3225 was originally a p3224 child process and now becomes the parent process of p3227. Father and son are relative, this people should be easy to understand. As long as the current process has been fork, the process becomes the parent process, which prints out the parent.
So print out the result:
1 Parent 2043 3224 3226
1 Parent 3224 3225 3227
Step three: The second step creates two process p3226,p3227, which ends when the printf function finishes executing, because the two processes cannot enter the third loop, cannot fork, and executes return 0;
Here are the results printed by p3226,p3227:
1 Child 1 3227 0
1 Child 1 3226 0
The attentive reader may notice that p3226,p3227 's parent process should not be p3224 and p3225, how could it be 1? Here we have to talk about the process of creating and dying,

After the second loop is executed by p3224 and p3225, the main function should quit, that is, the process should die, because it has done everything. After the death of p3224 and p3225,

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 is never dead, as for why,

Here is not introduced, to stay to the "three, fork higher-level knowledge," said.


To summarize, this program executes the following process:

This program eventually produces 3 sub-processes and executes the printf () function 6 times.
Let's look at one more code:

#include <unistd.h>#include<stdio.h>intMainvoid)  {     intI=0;  for(i=0;i<3; i++) {pid_t fpid=Fork (); if(fpid==0) printf ("son/n"); Elseprintf ("father/n"); }     return 0; }  

The result of its execution is:
Father
Son
Father
Father
Father
Father
Son
Son
Father
Son
Son
Son
Father
Son
This is not a detailed explanation, just a rough analysis.
For I=0 1 2
Father Father father
Son
Son father
Son
Son father father
Son
Son father
Son
Each of these lines represents the run print result of a process, respectively.


Summing up the rules, for this n-cycle situation, the number of times to execute the printf function is 1+2+4+......+2n-1, and the number of child processes created is 1+2+4+......+2n-1.

#include <unistd.h>#include<stdio.h>intMain () {pid_t fpid;//Fpid represents the value returned by the fork function//printf ("fork!"); printf"fork!\n"); Fpid=Fork (); if(Fpid <0) printf ("Error in fork!"); Else if(Fpid = =0) printf ("I am the child process and my process ID is%d\n", Getpid ()); Elseprintf ("I am The parent process, my process ID is%d\n", Getpid ()); return 0; }  

fork!
I am The parent process, my process ID is 3361
I am the child process and my process ID is 3362


fork! I am The parent process, my process ID is 3298
fork! I am the child process and my process ID is 3299

The only difference between the program is a/n carriage return symbol, why the result is so big difference?
This is related to the buffer mechanism of printf, when some content of printf, 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 that there is/N, the stdout is refreshed immediately, so you can print immediately.
printf ("fork!") was run After, "fork!"  Just put in the buffer, the program runs to the fork buffer inside the "fork!" Quilt process copy passed. So stdout in the degree of sub-progressive

There is also a fork! in the buffer.  So, what you finally see is fork!. was printf 2 times!!!

Further analysis of the fork function

#include <stdio.h>  <unistd.h>  int main (intChar* argv[ ])  {     fork ();      && fork () | | fork ();     Fork ();      return 0 ;  }  

, the number of processes that the program created.

The answer is a total of 20 processes, minus the main process, and 19 more processes.
Let's take a closer look at why there are still 19 of processes.
The first fork and the last fork are sure to be executed.
Mainly in the middle of the 3 fork, you can draw a diagram to describe.
Here you need to be aware of && | | Operator.
A&&b, if the a=0, there is no need to continue to implement &&B, a non-0, you need to continue to implement &&b.
a| | B, if A is not 0, there is no need to continue execution | | B, a=0, we need to continue to execute. | | B.
Fork () is different from the return values of the parent and child processes, according to A&&b and a| | B's branch to draw, you can draw 5 branches.

Plus the front fork and the last fork, a total of 4*5=20 process, except main main process, is 19 processes.

Linux fork () function reprint ~ ~ ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.