A classic understanding of fork Problems

Source: Internet
Author: User

Main ()
{
Pid_t PID;
If (pid = fork () <0)
{
Printf ("error! ");
}
Else
{
If (pid = 0)
Printf ("A/N ");
Else
Printf ("B/N ");
}
}
Returns a, B, or B,.
Because the fork call will execute two returned results respectively from the child process and the parent process
Because the parent process has nothing to do with the child process, both the parent process and the child process may first return

Reading a program

Main ()
{
Pid_t a_pid, B _fork;
If (a_pid = fork () <0)
{
Printf ("error! ");
}
Else
{
If (a_pid = 0)
Printf ("B/N ");
Else
Printf ("A/N ");
}

If (B _pid = fork () <0)
{
Printf ("error! ");
}
Else
{
If (B _pid = 0)
Printf ("C/N ");
Else
Printf ("A/N ");
}
}

If two processes are created, the result is displayed.
B
C
A
A
C
A

In fact, the key to understanding fork () is where its return points are. The most special thing about fork is that it has two or three return values. Note that two values are returned at the same time. The return value of PID = 0 is used to execute the sub-process code, and a code block whose return value is greater than 0 is the parent process. When fork is called for the first time, it is divided into two processes: A parent process and B sub-process. Each of them prints B and A each before the second fork call. Both Processes contain
If (B _pid = fork () <0)
{
Printf ("error! ");
}
Else
{
If (B _pid = 0)
Printf ("C/N ");
Else
Printf ("A/N ");
}
}
This code. Obviously, the parent process a and child process B are divided into two separate processes in this Code. Each of these two processes prints a and c each time. At this point, print a two times C and B three times in the program. 6 letters in total.
Note: When the first split is divided into two processes, the Parent and Child Processes contain identical code (the second time is still the same), but the PID value returned by the parent and child processes is different, the PID value in the parent process code is greater than 0, and the value in the child process code is equal to 0, so that the Branch selection statement such as if can be used to execute their respective tasks.

 

Make the following changes:

# Include <unistd. h>;
# Include <sys/types. h>;

Main ()
{
Pid_t PID;
Printf ("fork! "); // Printf (" fork! N ");
PID = fork ();

If (PID <0)
Printf ("error in fork! ");
Else if (pid = 0)
Printf ("I am the child process, my process ID is % DN", getpid ());
Else
Printf ("I am the parent process, my process ID is % DN", getpid ());
}

The result is
[Root @ localhost C] #./A. Out
Fork! I am the child process, my process ID is 4286
Fork! I am the parent process, my process ID is 4285

But I changed it to printf ("fork! N "); the result is
[Root @ localhost C] #./A. Out
Fork!
I am the child process, my process ID is 4286
I am the parent process, my process ID is 4285

Why is there only one fork! Printed? Why are there two in the previous one?

Let me also:
Wujiajia's understanding is incorrect,
Printf ("aaaaaaaa"); // print once; print twice
If you change printf ("aaaaaa") to printf ("aaaaaan"), it is printed only once.
The main difference is that there is an N-carriage return symbol.
This is related to the buffer mechanism of printf. In some content of printf, the operating system only places the content in the Buffer Queue of stdout and does not actually write it to the screen.
However, as long as N is displayed, stdout will be refreshed immediately, so it can be printed immediately.
After printf ("aaaaaa") is run, aaaaaa is only put in the buffer, and then run to fork, The aaaaaa quilt process in the buffer inherits
Therefore, aaaaaa is also available in the stdout buffer.
So what you finally see is that aaaaaa was printf twice !!!!
After printf ("aaaaaan") is run, aaaaaa is immediately printed to the screen, and there is no aaaaaa content in the stdout buffer in the child process to which the fork goes.
The result you see is that aaaaaa is printf once !!!!

(Excellent)

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.