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)