Method of transferring information between fork sub processes (resolves a problem that distinguishes between child processes and child processes)

Source: Internet
Author: User

See a problem today, the request is the parent process fork out two child processes, child process 1 needs to send a data to the child process 2, and then the child process 2 sends this data to the parent process

Specific ideas are as follows.

Parent Process fork out the PID of the child process, only the parent process is saved,

Child Process 1 can obtain the PID of the parent process, child process 2 can obtain the PID of child process 1, but the child process 1 is not the PID of child process 2, can only be sent through the parent process.

Because the child process 1 is first generated, and then the child process 2

The fork code follows PIDCHILD[2] is a global variable.

for (i = 0; i < 2; i++)

{

Pidchild[i] = PID = fork ();

}

After execution, in the parent process, pidchild[0],pidchild[1] will sequentially save the child process 1, the child process 2 PID

And in sub-process 1, because it was created first, so pidchild[0] for its PID

Child Process 2, is created after, so inherits the child process 1 of pidchild[0] (here just indicates that child process 2 can access the PID of child process 1)

The "Share on read, copy on write" principle between parent and child processes

Child Process 1 and child process 2 inherit the parent process variable I

Child Process 1 was created for the first time, so the I value at that time was 0,

Child Process 2 is created for the second time, so I has a value of 1

This is an important basis for the process 1 and sub-process 2 of the District molecule

Differentiate code as if (0 = = pid && i = = 0)//Sub-process 1

if (0 = = pid && i = = 1)//Sub-process 2

Then through the signal function set before the fork, by the parent process to the child Process 1 sub-process 2 of the PID (Pidchild[1]), the child process 1 has been the child process 2 PID,

Child Process 1 is signaled to child process 2, and finally to the parent process by subprocess 2.

Method of transferring information between fork sub processes (resolves a problem that distinguishes between child processes and child processes)

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.