About the fork () function under Linux

Source: Internet
Author: User

Previous questions

1.

int main () {
int i;
for (i = 0; i<5;i++) {
int pid = fork ();
if (PID = = 0) {
Do something
} else {
Do something
}
}
Do Somthing,sleep
return 0;
}
5
10
15
32

2

2.Linux how many "-" will be printed:

int main (void) {
int i;
for (i=0;i<4;i++) {
Fork ();
printf ("-\n");
}
return 0;
}
16
30
32
15

3. How many "-" of the following program output?

int main (void)
{
int i;
for (i = 0; i < 2; i++) {
Fork ();
printf ("-");
}
return 0;
}
2
4
6
8

Analysis: Problem 1, only need to make a process diagram (similar to a binary tree) to draw out, calculate the last layer of nodes can be,

Question 2: Calculate the number of prints, not the number of creation processes

Question 3: The biggest difference with the title 2 is that the title 2 is printed with a newline character, the title 3 does not.

Properties of the fork () system call

    • The fork () system call is a system call that creates a child process in its own process under UNIX, one call, two returns, and if the return is 0, it is a child process, and if the return value >0, it is the parent process (the PID of the child process is the return value), which is known to the public.
    • At the fork () call, the entire parent process space is copied to the child process as it is: directives, variable values, program call stacks, environment variables, buffers, and so on.

About the fork () function under Linux

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.