The fork of the Linux Process Primitive () (original!) )

Source: Internet
Author: User

I. Usage Analysis:

Fork () This function, it can be said that the name of the person, it is well known that fork the word is meant to fork, the foreigner to take the academic name when there will always be some pictographic ideas, so there is ~

The fork () function is a bifurcation function in computer programming. That is, a parent process will create a child process corresponding to it.

So the problem is that most of the functions we normally learn are only one return value, but fork () is particularly special in that it can produce two return values in one call at a time!

Fork () returns the ID of the child process for the first time (the child process ID is returned in the parent process)

The second return is 0 (returns 0 in the child process)

In general, fork () 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;

Quote a netizen to explain why the value of PID is different in the parent-child process.

"In fact, the equivalent of a list, the process forms a list, the parent process fpid (p means point) to the process ID of the child process, because the child process has no child process, so its PID is 0."

So how does fork () return two times?

First step: Create (child process creation)

The second step: Clone (equivalent to the parent process to initialize the child process, their own 0-3g user space completely copied to the child process, the following will be described in detail)

return child_pid; (returns the child process ID after passing its message to the child process)

Next we can understand that when the fork () function is halfway through, the parent process breaks, switches to the child process, and the child process goes through the remaining half of the fork () function, which completes the return 0,

This also explains why the fork () function returns the child process ID, which indicates that the current process is the parent process, and returns 0 indicating that the current process is a child process.

The question is again, do you want to clone it when the child process is created?

Of course not, when we make a fork () system call, the kernel copies the entire address space of the parent process as-is and assigns the copied copy to the child process. This behavior is very time-consuming, so now Linux presents a

The parent and child processes share the page instead of copying the page, and the clone operation is done only when the child process has a write operation, that is, the read-time share, and the copy-on-write principle . Specific can read this article, the big guy said very detailed http://www.cnblogs.com/wuchanming/p/4495479.html

So what do you copy when you copy a parent-child process?

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.

Two. practiced hand small problem

Continuous invocation of 1.fork ():

How many processes are created by the following programs?

Fork (); fork (); fork (); fork ();

Some people want to count the creation of several processes by invoking printf ("+"), which is not appropriate. The specific reason is the following code:

1#include <unistd.h>2#include <stdio.h>3 intMain () {4pid_t Fpid;//Fpid represents the value returned by the fork function5     //printf ("fork!"); 6printf"fork!/n"); 7Fpid =Fork (); 8     if(Fpid <0)  9printf"Error in fork!"); Ten     Else if(Fpid = =0)   Oneprintf"I am the child process and my process ID is%d/n", Getpid ());  A     Else   -printf"I am The parent process, my process ID is%d/n", Getpid ());  -     return 0;  the}

The results of the implementation are as follows:
fork!
I am The parent process, my process ID is 3361
I am the child process and my process ID is 3362
If you put the statement printf ("fork!/n"); Comment out, execute printf ("fork!");
The result of the new program execution is:
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 about the buffer mechanism of printf., some content in 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. Therefore, in the sub-degree of stdout buffer inside there is also fork!.  So, what you finally see is fork!. was printf 2 times!!!
While 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!!!
So printf ("+") does not respond correctly to the number of processes.

2.

1 fork (); 2 3 Fork () &&fork () | | fork (); 4 5 fork ();

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.

Resources:

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

The fork of the Linux Process Primitive () (original!) )

Related Article

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.