1. Opening and closing operations of pipes
#include <unistd.h>#include<stdio.h>#include<stdlib.h>intMainvoid ){ intfd[2];/*array of file descriptors for pipelines*/ Charstr[ the]; if(Pipe (FD)) <0) {perror ("Pipe"); Exit (1); } Write (fd[1],"Create the pipe successfully!\n",1024x768 ); /*writing data to the pipe write end*/Read (fd[0], str,sizeof(str));/*read the data from the pipe read-out end*/printf ("%s", str); printf ("pipe file descriptors is%d,%d \ n", fd[0], fd[1]) ; Close (fd[0]);/*close the read-in file descriptor for the pipeline*/Close (fd[1]);/*to close the read-out file descriptor for a pipe*/ return 0;}
Above this program is the opening and closing of the pipeline instance, first define the pipe file descriptor array fd[2], can be understood as the ends of the pipeline, respectively, read the end of the fd[0] and write end fd[1] end.
Write ( fd[1], "Create the pipe successfully!\n", writes the data to the pipe writing end with the function write (), (1024x768 ); , write to write end fd[1] writes "Create the pipe successfully!\n", the number 1024 is the maximum length of the data written.
Read ( fd[0], str, sizeof(str)) reads the data from the pipe read-out end with the function read (), and reads the data from the pipe end and stores it in the string str.
2. The application of pipelines between parent-child processes
Based on the knowledge of the process operations previously learned, we should be able to write how to communicate between parent and child processes through pipelines.
The first step is to define the basic things such as the file descriptor array, the array where the data is read, the process identifier, and so on.
The second step is to create the pipeline.
Step three, create a child process.
The fourth step is to write data to the pipeline in the parent process.
The fifth step is to read the data from the pipeline in the child process.
Sixth step, output the data printed in the child process.
These six steps are the process by which the parent process sends data to the child process, which is a framework in which the program is written to the framework, and this simple program can be written out. However, in the process of writing code to pay attention to a problem, first create a pipeline, and then create a child process, the parent process has a pipeline, the child process also has a pipeline, the parent process how to write the content of the child process from the pipeline read out? The book has a parent process that writes the data, and the child processes the program that reads the data. This program, in the application, closes the read data side of the parent process pipeline in the parent process, writes the data, closes the write data side of the child process pipeline in the child process, and then reads the data. This allows you to write data in the parent process to read the data in the child process. The specific code is as follows.
#include <unistd.h>#include<stdio.h>#include<stdlib.h>#include<fcntl.h>#include<limits.h>#include<sys/types.h>#defineBUFSZ pipe_buf/* PIPE_BUF pipeline default one-time read/write data length */intMain (void ){ intfd[2]; CharBuf[bufsz]; pid_t pid; ssize_t Len; if(Pipe (FD)) <0){/*Creating Pipelines*/perror ("failed to pipe" ); Exit (1 ); } if(PID = fork ()) <0){/*Create a child process*/perror ("failed to fork" ); Exit (1 ); } Else if(PID >0) {Close (fd[0] );/*closing the read-out end of a pipeline in a parent process*/Write (fd[1],"Hello my son!\n", -);/*The parent process writes data to the pipeline*/Exit (0); } Else{Close (fd[1] );/*the child process closes the write end of the pipeline*/Len= Read (fd[0], buf, BUFSZ);/*The child process reads data from the pipeline*/ if(Len <0) {perror ("process failed when read a pipe" ); Exit (1 ); } ElseWrite (Stdout_fileno, buf, Len);/*output to standard output*/Exit (0); }}
The above code after debugging, found that there is no problem, it proves that the first idea is correct, in such a way to make the parent-child process through the pipeline communication. However, this is when the read-out end of the pipeline in the parent process is closed before the data is sent to the child process, and if the read out of the pipeline in the parent process is not closed can it pass through? It is also possible to modify the code to find that the read-out end of the pipe is not closed.
Further thinking, can the data written in the parent process be read at the same time in the parent and child processes? After debugging the code, adding the data in the read pipeline to the parent process and displaying the code as follows, the discovery data can only be read from the parent process and cannot be read from the child process.
else if (pid > 0 ) {Write (fd[ 1 ],
"
hello my son!\n
",
14 );
/*
The parent process writes data to the pipeline
*/
read (fd[
0
],buf1,bufsz");p rintf ("%s\n%
s\n", BUF1,BUF1); Exit (
0
This is only two possible, one possibility is that the pipeline is one-time, after the delivery of the pipeline is automatically cleared. The other is to write the data on the pipeline write side of the parent process, which can be read out at the pipe read-out end of the parent process or child process, but read only once, read from the parent process and then no longer read in the child process, and read in the child process no longer in the parent process. Continue to modify the code to debug, first verify the possibility.
Else if(PID >0) {Write (fd[1],"Hello my son!\n", -);/*The parent process writes data to the pipeline*/Read (fd[0],buf1,bufsz);p rintf ("%s\n%s\n ", buf1,buf1);//Write data for the second timeWrite (fd[1],"Hello my son2!\n", the);/*The parent process writes data to the pipeline*/Read (fd[0],buf1,bufsz);p rintf ("%s\n%s\n ", BUF1,BUF1); Exit (0); }
After commissioning, it was found that the pipeline was always available, thus overturning the first hypothesis. That leaves the second possibility, close the read-out end of the pipeline in the parent process, the code below, and then debug the discovery that you can only read the data from the child process.
else if (pid > 0 ) {Close (fd[ 0 1 ], " hello my son!\n " , 14 ); /* The parent process writes data to the pipeline */ read (fd[ 0 ],buf1,bufsz");p rintf ("%s\n%s\n", BUF1,BUF1); Exit ( 0
From this series of debugging procedures, you can understand the pipeline's working process and understand the process of communication between the parent and child processes.
In the first picture is the pipeline after the creation of the pipeline and sub-process, but in the communication only from the left to select a written line, on the right to select a read-out line. The more used is in Figure 2 in the parent process to send data through the pipeline into the child process.
3. The application of pipelines between sibling processes
The application of the pipeline between the parent-child process it is clear that the application between the brothers is well understood.
The first step is to define the basic things such as the file descriptor array, the array where the data is read, the process identifier, and so on.
The second step is to create the pipeline.
Step three, create the first child process.
The fourth step is to write data to the pipeline in the first child process.
Fifth step, create a second child process
The sixth step is to read the data in the pipeline in the second child process.
Seventh step, output the data printed in the second child process.
The specific code looks like this:
#include <unistd.h>#include<stdio.h>#include<stdlib.h>#include<limits.h>#include<fcntl.h>#include<sys/types.h>#defineBUFSZ Pipe_bufvoidErr_quit (Char*msg) {perror (msg); Exit (1);}intMain (void ){ intfd[2]; CharBUF[BUFSZ];/*buffers*/pid_t pid; intLen; if(Pipe (FD)) <0)/*Creating Pipelines*/Err_quit ("Pipe" ); if(PID = fork ()) <0)/*Create first child process*/Err_quit ("Fork"); Else if(PID = =0){/*in a child process*/Close (fd[0] );/*to close a file descriptor that is not used*/Write (fd[1],"Hello brother!\n", the);/*Send Message*/Exit (0); } if(PID = fork ()) <0)/*Create a second child process*/Err_quit ("Fork"); Else if(PID >0){/*in the parent process*/Close (fd[0] ); Close (fd[1] ); Exit (0 ); } Else{/*in a child process*/Close (fd[1] );/*to close a file descriptor that is not used*/Len= Read (fd[0], buf, BUFSZ);/*reading Messages*/Write (Stdout_fileno, buf, Len); Exit (0); }}
Pipeline communication operations