An interesting question about fork () calls

Source: Internet
Author: User

I often see someone asking this question:


# Include < Stdio. h >
# Include < Sys / Types. h >
# Include < Unistd. h >

Int Main (){
Int PID = 0 ;
For ( Int I = 0 ; I < 5 ; I ++ ){
PID = Fork ();
If (PID = 0 ){
Printf ( " PID: % d \ n " , Getpid ());
}
}
Return   0 ;
}

How many lines of PID are printed: XXX. many people think it is very simple. There are not five sub-processes. The answer is five. In this way, we can say that we have no idea how to implement fork () system calls in Linux/Unix. The above question is equivalent to asking this question.ProgramThe answer is an-1, because the initial process does not print this information. If you change the program like this:


# Include < Stdio. h >
# Include < Sys / Types. h >
# Include < Sys / Wait. H >
# Include < Unistd. h >

Int Main (){
Int PID = 0 ;
For ( Int I = 0 ; I < 5 ; I ++ ){
PID = Fork ();
Wait (null );
}
Printf ( " PID: % d \ n " , Getpid ());
Return   0 ;
}

Then the answer is obviously the number of all processes. The preceding wait call aims to ensure that each process does not output information across processes.

In order to evaluate an, we will briefly introduce the fork () system call. in Linux, fork () calls will call clone (), while clone () will eventually call do_fork () the key to system calling to generate a sub-process is how the sub-process is generated. In Linux/Unix, the sub-process generated by fork () is equivalent to copying the entire parent process. First, the PCB is copied, then share the memory page table to the parent process page (copy at write time ). Generally speaking, sub-processes and parent processes look exactly the same.CodeSegment, same data segment, same process control block, but they are independent. When the kernel returns to the user State, the system calls the PID of the child process returned to the original process, the child process returns 0, so that the parent and child processes can be distinguished.

Back to the above question, why is the answer 5 wrong? For example, when the parent process I = 0, fork () has a child process P1, however, P1 is in the same state as the parent process, that is, it will continue to loop, from I = 1 to fork () A P2, p2 will continue to fork () Other Sub-processes starting from I = 3, which will generate many sub-processes.

The number of processes generated.

If F (n) is set, it indicates the number of processes generated by the entire program when the program executes N cycles. It is easy to obtain the recursive formula:

F (n) = 1 + f (n-1) + f (n-2) + f (n-3) +... + f (0)

For example, for I = 0; I <n; I ++

Because the sub-process of Fork () at I = 0 will continue loop n-1 times next time, the sub-process of Fork () at I = 1 will only need to loop N-2 times next time ....

The constant 1 is the process itself.

Boundary condition, F (0) = 1

In this way, we get the answer to the question:

F (n) = 1 + f (n-1) + f (n-2) +... + f (0)

F (0) = 1

Here we can find the closed form:

F (0) = 1

F (1) = 2

F (2) = 4

...

Using mathematical induction, we can obtain F (n) = 2 ^ n.

Therefore, for program 1, 2 ^ 5-1 = 31 rows are printed.

For program 2, a total of 2 ^ 5 = 32 processes are generated.

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.