We all know that a good way to create sub-processes in Linux is to call the fork function, but many beginners may have difficulty understanding the fork. Let's take an example to see how to use fork. When using fork, remember that fork is" Forks . I remember that when I was a beginner to the fork () function and encountered this function, I could not understand why fork was written like this, and it would be divided into two processes: parent and child.
Let's take a look at the fork () Classic Mode // ------------------------------------------------------ begin
Int pid = fork ();
If (PID <0 ){
// Failed, generally because the user's process count has reached the limit or the memory has been used up
........
}
Else if (pid = 0 ){
// The sub-process executes Code
......
}
Else {
// Code executed by the parent process
.........
}
.........
// ----------------------------------------------------------- End
(This Code may be a bit problematic and will be explained later.) First, let's take a look at the fork return value. There are three situations-1, 0,> 0.
-1: Of course, it is a failure, and it will not be split into two processes. The return value 0 is a sub-process, and the return value> 0 is a parent process. The returned value is of course the PID of the Capital Process. Here is a difficult place to understand. It can be explained in a simple way: when a process encounters a fork call, copy the entire process, that is, the sub-process. At this time, the return value of the process is set to greater than 0, that is, the generated subprocess PID is copied, And the return value is set to 0 in the copied process. That is, there are already two processes, only the PID value is different (ignore other settings ). At this time, both processes start to run from fork, but the PID is different, so if ..... else if .... esle executes the corresponding code according to the PID. It does not mean that a part is the code of the parent process, and a part is the code of the Self-process. it's just some conditional judgment. so after fork, the real code will be executed by two processes, but (when fork is successful) the PID in the sub-process is zero, so the else if condition is true, the other two are not true, therefore, execute the code in else if. if the PID in the parent process is greater than 0, else is set up and runs the code. if for () K fails, of course-1 is returned. At this time, there is no sub-process. now let's take a look at the fork return value. If it fails,-1 is returned. In Linux, half of the system calls are used. The error code is in errno. 0 sub-process, because you can get the PID of your process and parent process through getpid () and getppid ();> 0, in the parent process, this is Fork () because there is no sub-process PID in the process. PID system call. In this case, we need to save the returned PID and then use it to control the sub-process. For example Program Call kill (PID, 9) to kill the child process. Finally, I want to tell you whether the fork is executed first by the parent process or by the child process, depending on the CPU call. Algorithm That is, it is possible for them to execute first. This article is from" OSS "Blog, please be sure to keep this source Http://binux.blog.51cto.com/742827/163234
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