Famous pipe nameless pipe and famous pipe:
1. Pipeline is a special type of file, in the first-in- first-out to meet the principle of write can read and write, cannot locate read-write location.
2. The piping is unidirectional.
3. The nameless pipe is blocked in the read-write location , and the famous pipe is blocked at the creation location .
4. nameless pipes are generally used only for inter- relationship interprocess communication; A well- known pipeline exists as a disk file, which can realize the communication between two processes in this machine.
Shell creates a named pipe
mknod pipe name P //create a well-known pipeline named Pipetest mknod for the command p is a parameter that represents a well-known pipe
directive > pipe name & //Input The result of the instruction into the pipeline file
directive < pipe name //To specify the contents of the pipe name as input
Programming to implement well-known pipelines
int Mkfifo (__const char *__path, __mode_t __mode): The established pipe file must not exist before, mode is the permission of the file. Returns 0 successfully, otherwise returns-1.
Read/write is implemented through read (), write ().
See the way it's blocked (I'm too lazy to type it ....) )
Example:
Write process
#include <stdio.h>#include<unistd.h>#include<sys/types.h>#include<fcntl.h>#include<string.h>#include<stdlib.h>#include<errno.h>#include<sys/stat.h>#defineFifo_name "/tmp/my_fifo"//The path to the named pipe to createintMainintargcChar*argv[]) { intpipe_fd; intRes; CharBuffer[] ="Hello world!"; if(Access (fifo_name, F_OK) = =-1)//whether the file exists{res= Mkfifo (Fifo_name,0766);//Creating Pipelines if(Res! =0) {fprintf (stderr,"Could not create FIFO%s\n", Fifo_name); Exit (Exit_failure); }} printf ("Process%d opening FIFO o_wronly\n", Getpid ()); PIPE_FD= Open (Fifo_name, o_wronly);//Open a famous pipeprintf"The file ' s descriptor is%d\n", PIPE_FD); if(PIPE_FD! =-1) {res= Write (pipe_fd, buffer,sizeof(buffer));//Write Data if(res = =-1) {fprintf (stderr,"Write error on pipe\n"); Exit (Exit_failure); } printf ("Write data is%s,%d bytes is write\n", buffer, res); Close (PIPE_FD); } Elseexit (exit_failure); printf ("Process%d finished\n", Getpid ()); Exit (exit_success);}
Read process
#include <stdio.h>#include<unistd.h>#include<sys/types.h>#include<fcntl.h>#include<string.h>#include<stdlib.h>#include<errno.h>#include<sys/stat.h>#defineFifo_name "/tmp/my_fifo"//The path to the named pipe to createintMainintargcChar*argv[]) { intpipe_fd; intRes; Charbuffer[4096]; intBytes_read =0; memset (Buffer,' /',sizeof(buffer)); printf ("Process%d opening FIFO o_rdonly\n", Getpid ()); PIPE_FD= Open (Fifo_name, o_rdonly);//Open a famous pipeprintf"The file ' s descriptor is%d\n", PIPE_FD); if(PIPE_FD! =-1) {Bytes_read= Read (pipe_fd, buffer,sizeof(buffer)); printf ("The read data is%s\n", buffer); Close (PIPE_FD); } Elseexit (exit_failure); printf ("Process%d finished,%d bytes read\n", Getpid (), bytes_read); Exit (exit_success);}
At run time, the write process is run at a terminal, at which time the write process is blocked.
Use Ctrl+alt+[f1-f6] to switch the terminal and run the read process at the new terminal.
Read process-side results as
And then switch back to the original terminal, the write process is completed.
"Linux Advanced Programming" (Chapter Nineth) interprocess communication-Pipeline 3