Linux c Process pipe communication code analysis, linuxpipe
[Root @ luozhonghua 04] # cat ex04-3-pipe02.c
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Include <unistd. h>
# Include <sys/types. h>
Int main (void ){
Int result =-1;
Int fd [2], nbytes;
Pid_t pid;
Char string [] = "hello, pipe ";
Char readbuffer [80];
Int * write_fd = & fd [1]; // writable
Int * read_fd = & fd [0]; // read
Result = pipe (fd); // create pipe
If (-1 = result ){
Printf ("create pipe faile ");
Return-1;
}
Pid = fork (); // forking Program
If (-1 = pid ){
Printf ("fork process failed \ n ");
Return-1;
}
If (0 = pid ){
Close (* read_fd );
/* Write characters to the MPs queue */
Result = write (* write_fd, string, strlen (string ));
} Else {
Close (* write_fd );
/* Read pipe data */
Nbytes = read (* read_fd, readbuffer, sizeof (readbuffer ));
Printf ("received % d data, content: % s \ n", nbytes, readbuffer );
Printf ("sizeof (readbuffer) = % d \ n", sizeof (readbuffer ));
}
Return 0;
}
-------------- Test results
[Root @ luozhonghua 04] #./ex04-3-pipe02
Received 10 data records with the following content: hello, pipe
Sizeof (readbuffer) = 80
Linux inter-process communication experiment (pipeline) source code
The source code is huge. See advanced environment programming.
In linux, the problem of pipeline communication between C processes can be understood to see how the program runs incorrectly.
Your requirement is that "after writing data at the end of the MPs queue, the MPs queue should read the data at the header and write data at the end of the MPs queue until the end of the MPs queue is in a loop". This is actually a process synchronization problem, the other side reads the data before it can be written. Therefore, we need to use the inter-process communication method to notify the write end of the read segment that I have read it. You can write the next one. There are many ways to synchronize this process, such as using semaphores. However, since you are a pipeline code, I will use the pipeline Method for your reference. The following code creates two pipelines. pipe_a2b is your original pipeline, and pipe_b2a is newly added to allow the read end to notify the write end to continue.
# Include <stdio. h>
# Include <stdlib. h>
# Include <unistd. h>
# Include <errno. h>
# Include <string. h>
Int main ()
{
Char r_buf [10];
Char w_buf [10];
Pid_t pid;
Int pipe_a2b [2];/* parent write, child read */
Int pipe_b2a [2];/* child write, parent read */
Memset (r_buf, 0, sizeof (r_buf ));
If (pipe (pipe_a2b) <0)
{
Printf ("creat pipe error \ n ");
Exit (1 );
}
If (pipe (pipe_b2a) <0)
{
Printf ("creat pipe error \ n ");
Exit (1 );
}
Pid = fork ();
If (pid = 0)
{
/* Child, reader */
Close (pipe_a2b [1]);
Close (pipe_b2a [0]);
While (1)
{
Read (pipe_a2b [0], r_buf, 10 );
Printf ("FIFO: % s \ n", r_buf );
Write (pipe_b2a [1], "c", 1);/* tells parent continue */
}
Close (pipe_a2b [0]);
Close (pipe_b2a [1]);
}
If (pid> 0)
{
/* Parent, writer */
Close (pipe_a2b [0]);
Close (pipe_b2a [1]);
While (1)
{
Printf ("please input w_buf: \ n ");
Scanf ("% s", w_buf );
Write (pipe_a2b [1], w_buf, strlen (w_buf ));
/* Wait for the "c" from child to continue to next write */
Read (pipe_b2a [0], r_buf, 10 );
} ...... Remaining full text>