A classic understanding of fork problems

Source: Internet
Author: User
Tags printf

Main ()
{
pid_t pid;
if (Pid=fork () <0)
{
printf ("error!");
}
Else
{
if (pid==0)
printf ("a/n");
Else
printf ("b/n");
}
}
The result is to return a, B or b,a
Because the fork call will perform two returns from the child process and the parent process, respectively
Because the parent and child processes are independent, both the parent process and the child process may first return

Looking at 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 you are creating two processes, the result will appear
B
C
A
A
C
A

In fact, the key to Understanding fork () is where its return point is. The most special part of fork is that he has two or even three return values, and note that it returns two values at the same time. Where Pid=0 's return value is used to execute the child Process code, and a return value greater than 0 is the code block of the parent process. When the first fork is called, the fork is divided into two processes, which may be set to a parent process and B child process. They individually print B and a once before the second fork call, and in both processes of the first fork
if (B_pid=fork () <0)
{
printf ("error!");
}
Else
{
if (b_pid==0)
printf ("c/n");
Else
printf ("a/n");
}
}
This piece of code. Obviously, the a parent process and the B sub-process are separated into two processes in this code. Each process in both processes prints a,c each time. Here, a total of three times a two C and a B are printed in the program. A total of 6 letters.
Note: When the first fork is divided into two processes, the parent-child process contains exactly the same code (the second time is still the same), except that the PID value in the parent process code is greater than 0, and the value in the child Process code is equal to 0 because of the different values returned in the father-child process. This allows you to perform their respective tasks through branch selection statements such as if.

I 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 ());
}

 

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 to printf ("Fork!n"), and 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 only one fork! print out. Why was there 2 on the previous one?

I'll take a look too:
Wujiajia's understanding is somewhat wrong,
printf ("aaaaaaaa");//print once; it will print 2 times.
If you replace printf ("aaaaaa") with printf ("Aaaaaan") then it is only printed once.
The main difference is because there is an n carriage sign
This is related to the buffer mechanism of printf, when some content of printf, the operating system just put the content in the stdout buffer queue, and did not actually write to the screen
However, as soon as you see N, the stdout is refreshed immediately, so you can print it right away.
After running printf ("aaaaaa"), AAAAAA is only put in the buffer, and then run to fork, buffer inside the AAAAAA quilt process inherits
Therefore, in the sub-degree of stdout buffer inside there is also aaaaaa.
So, what you end up seeing is that AAAAAA was 2 times printf!!!!
After running printf ("Aaaaaan"), AAAAAA is immediately printed on the screen, and then the stdout buffer in the sub-process to fork will not have aaaaaa content
So the result you see is that AAAAAA was printf 1 times!!!!

Essentials

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.