Explain the fork creation process in Linux (GO)

Source: Internet
Author: User

As you know, a good way to create sub-processes in Linux is function call fork, but many beginners may have a bit of difficulty understanding the fork.        Let's take a look at the use of fork, for example.        When we use fork to remember fork is "fork" meaning is very good understanding. Remember the beginning fork () function and encountered this function, always can not understand why fork is so written, will be divided into two processes of father and son.
First look at the Classic mode of fork ()//--------------------------------------------------------begin
int pid=fork ();
if (PID < 0) {
Failed, typically the user's number of processes has reached a limit or the memory has been exhausted
........
}
else if (PID = = 0) {
Code that the child process executes
......
}
else{
Code executed by the parent process
.........
}
.........
-------------------------------------------------------End
(This code may be a bit of a problem, explained later), first look at the return value of fork, there are three cases -1,0,>0
-1: Of course it's a failure, and it won't split into two processes.   Return 0 is a child process, return 〉0 is the parent process, the value returned at this time is of course the PID of the funding process. Here is a more difficult to understand a place, his can be such a simple explanation: when the process encountered a fork call, the process of the entire copy, that is, the child process. At this point, the return value of the process is set to 〉0, that is, the generated subprocess PID was just copied, and the return value is set to 0 in the copy-generated process.     That is, there are already two processes, only the values of the PID are different (ignoring the other settings). At this point, two processes are executed from the fork, but the PID is different, so if ... else if .... esle will execute the corresponding code according to the PID, not to say that a part is the code of the parent process, and that part is the code of the self-process. Just a few conditions to judge. So when the fork, the total code will be executed by two processes, only (fork success) in the sub-process of the PID is zero, so else if the condition is true, the other two is not true, so the code in the else if is executed. Parent process PID >0,  So else is set up to execute the code in it.    If the for () k fails, it is of course returned-1, at which time there is no child process. Now take a look at the fork return value, fail, return half of the system calls under -1,linux, and the error code in errno. 0 sub-processes, because you can get the PID of your own process and parent process through GETPID () and Getppid (); >0, in the parent process, this is the child process PID of fork () because there is no system call in the process that can get the PID of its own child process. At this point, the returned PID will be saved and later used to control the child process.   For example, when the program exits, call Kill (pid,9) to kill its own child process. Finally, what I want to tell you is whether the parent process executes first or the child process after the fork, depending on the CPU call algorithm, which means that it is possible for them to execute first. This article is from the "http://binux.blog.51cto.com/742827/163234" blog, please be sure to keep this source.

Explain the fork creation process in Linux (GO)

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.