The nameless pipe is linux A primitive method of pipeline communication in , It has the following characteristics :

1、 Simplex communication mode , It has fixed read end and write end ;

2、 A pipe can be seen as a special kind of file , For its reading and writing, you can use ordinary read(),write() Wait for the documents IO Operation function interface , But it doesn't belong to any file system , And it only exists in memory ;

3、 It can only be used for communication between processes that are related ;

4、 Usually used , First, create a pipe , And then call fork Function to create a child process , The child process inherits the pipe created by the parent process ;

5、 Only if the read end of the pipe exists , It makes sense to write data to the pipeline , Otherwise, the process that writes the data to the pipeline will receive the data sent by the kernel SIGPEPE The signal ;

6、 When writing data to a pipe ,linux The atomicity of writing is not guaranteed , As long as there's space in the pipeline buffer , The write process will try to write data to the pipeline , If the pipeline buffer is full , So write operations will always block ;

The following examples illustrate how to use the pipe in detail , First, create the pipeline , Then the parent process uses fork Function create subprocess , Finally, by closing the read descriptor of the parent process and the write descriptor of the child process , Establish communication between them , The order of parent-child processes is determined by semaphores PV Operation to achieve ; chart 1 For detailed implementation code , chart 2 To compile the run results .