Fork function of small error-function

Source: Internet
Author: User

Author: huaqing visionary lecturer

For fork This function believes that everyone is not unfamiliar. We knew he would create a subprocess, return two values, and fork would return two times. So here we are today to sort out our understanding of this function.

For the fork will return two times this statement I do not know how people understand, "return and then return again", in the first contact with this function when many students think so. So here we are, and we're going to analyze that.

First we know, what happens if the call succeeds after calling fork in the process? Yes, he would create a subprocess, but where would the child process be created? This involves the knowledge of the composition of the process structure. First of all, we see that a process in the virtual address from low to high stored in the body section, data segments (where we have initialized and uninitialized data collectively referred to as data segments), and stack segments, mainly these three segments. So we're talking about the text section, which has properties for read-only sharing. That is to say, we can run multiple times for a program, but they run in different processes. So these different processes are not sharing the same text section. So is our fork function, after the process calls the fork function, the child processes are generated, and the child processes share the same body segment as the process that invokes the fork, which is the parent process. After the fork function returns successfully, there are two processes running the same body segment at the same time. Let's take a look at the following note:

PID = fork ();

if (PID < 0)

{

perror{};

Exit (1);

}

/* At this time, there are two programs running to this * *

else if (PID = = 0)

{

/* Child Process Code * *

}else

{

/* Parent Process Code * *

}

Let's look at the comment on the code above, which means that after fork successfully returns, there are two programs running at the same time/* At this point, two programs run to this comment, where the fork in the parent-child process has returned a different value. In the parent process, the fork of the parent process returns a number greater than 0, which we all know is the PID of the subprocess; in the subprocess, the fork of the child process returns a 0, indicating that the process was fork by another process. Then our parent-child process will go down and judge the following else if. In the parent process, because the fork return value is greater than 0, the else if (PID = = 0) Decision statement does not hold, and then executes the code in the else below. In the subprocess, the PID is equal to 0, which means that the child process discovers the condition after it has judged the condition of else if (PID = = 0), and then it executes the code inside. Such a fork framework allows us and the parent-child process to differentiate the code blocks of the respective processes in the same body section.

So now we look at this "fork will return two times" is the argument still rigorous? Yes, we fork only return once, but in different processes (the caller and the creator) it returns the same value, and then because it shares the same text segment, it gives us the illusion of returning two times.

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.