For concurrent program design

Source: Internet
Author: User
For details about concurrent programming-general Linux technology-Linux programming and kernel, see the following. # Include
# Include
# Include
# Include
Int main ()
{
Int r, p1, p2, fd [2], I;
Char buf [50]; // Buffer
Pipe (fd); // define MPs queue
For (I = 0; I <2; I ++)
{// How can I execute the loop body once?
While (p1 = fork () =-1); // The parent process creates a child process. It is created repeatedly when the child process fails until it jumps out of the while
If (p1 = 0)
{// Return from child process p1 and execute child process p1
Lockf (fd [1],); // locks the pipeline to achieve mutual exclusion between the two sub-Processes
Sprintf (buf, "the messge is from process 1 \ n"); // write data to the buffer
Printf ("process 1 is running! The id is % d \ n ", getpid ());
Write (fd [1], buf, 60); // write the buf content to the MPs queue
Sleep (3); // sleep for 3 seconds for the parent process to read and control synchronization between parent and child Processes
Lockf (fd [1],); // release the pipe write end
Exit (0); // close the child process p1
} // If
Else // return from the parent process and execute the parent process
{
While (p2 = fork () =-1); // create a sub-process p2, loop upon failure
If (p2 = 0)
{// Return from child process p2 and execute child process p2
Lockf (fd [1],); // lock the write end and control the mutual exclusion between sub-Processes
Sprintf (buf, "the message is from process 2 \ n ");
Printf ("process 2 is running! The id is % d \ n ", getpid ());
Write (fd [1], buf, 60); // write the content in buf to the MPs queue.
Sleep (3); // sleep for 3 seconds so that the parent process can read messages in the pipeline.
Lockf (fd [1],); // release the pipe write end
Exit (0); // close the child process p2
}
Wait (0); // The parent process is blocked and enters the waiting queue. When the child process ends, the original program is returned for further execution to control synchronization between parent and child processes.
If (r = read (fd [0], buf, 60) =-1) // read data from the pipeline and store it in buf
Printf ("can't read the pipe \ n ");
Else
Printf ("father process is reading message: % s \ n", buf );
// Display messages in the MPs queue
Wait (0); // The parent process is blocked and enters the waiting queue. When the child process ends, the original program is returned for further execution to control synchronization between parent and child processes.
If (r = read (fd [0], buf, 60) =-1) // read data from the pipeline and store it in buf
Printf ("can't read the pipe \ n ");
Else
Printf ("father processs is reading message: % s \ n", buf );
Exit (0); // close the parent process
} // Else
} //
}

How can there be two running results? The details are as follows:
I,
Process 1 is running! The id is 3005
Process 2 is running! The id is 3006
Father process is reading message: the message is from process 1

Father process is reading message: the message is from process 2

II,
Process 1 is running! The id is 3031
Father process is reading message: the message is from process 1
Process 2 is running! The id is 3032

Father process is reading message: the message is from process 2

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.