Preliminary study process -- fork

Source: Internet
Author: User

During this time, I plan to make a summary of what I learned before, so that I will not always learn the same thing and forget the same thing. For C Programming in Linux, start by using fork to open up a new process.

When I first came into contact with fork, I thought this function was really interesting. I called it once and returned it twice. Let's look at the following section.Program:

 

1 # Include < Stdio. h >
2 # Include < Stdlib. h >
3 # Include < Unistd. h >
4 # Include < Sys / Types. h >  
5
6 Int Main ( Int Argc, Char   * Argv [])
7 {
8 Pid_t PID; /* Save process number */
9
10 PID = Fork (); /* Generate new processes */
11
12 If (PID <   0 ){ /* If the process fails to be created */
13 Printf ( " Error! \ N " );
14 Exit ( - 1 );
15 } Else   If (PID =   0 ){ /* If you enter the sub-process */
16 Printf ( " This is a child process, " );
17 Printf ( " My PID is % d \ n " , Getpid ());
18 } Else {
19 Printf ( " This is parent process, " ); /* If it is a parent process */
20 Printf ( " My PID is % d \ n " , Getpid ());
21 }
22
23 Return   0 ;
24

The running result is as follows:

This is parent process, my PID is 2945
This is a child process, my PID is 2946

At first, I was really confused about this. Isn't the C language specifying that a function can only have one return value? How can this problem be returned twice and the program has been executed twice again. It was later known that fork was the "ghost", and the program began to "Split" into two processes at fork. When the program was running in a child process, the fork return value is 0, which is the selection branch of else if (pid = 0). When the program runs in the parent process, the return value is the PID of the child process; the worst case is that the process fails to be opened. At this time, the system usually returns a negative if (PID <0), which can be used to identify the error. However, for the sake of simplicity, I did not judge the error in this program.

After the process ends, call exit () to exit the process, but you will find that in the above program, except that exit () is called after the thread creation fails, exit () is not used, and only a return exists at the end of the program. Previously, I thought that return replaces exit (), however, when I read "Linux kernel design and implementation" today, I found that the above mentioned point is that during compilation, the compiler is in the mian () the retrurn of the function is followed by an exit (). Originally, exit () still exists, but I didn't notice it at ordinary 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.