One, define:
Pipelines can also be divided into nameless pipes and Named Pipes , the use of which is not the same.
Nameless Pipes pipe: Mainly used for communication between affinity processes, nameless pipe communication is unidirectional, only from one paragraph to another; The nameless pipe is temporary and disappears automatically when the communication is complete. It is generally used to create a nameless pipe, and then create a child process, so that the child process inherits the parent process's pipe file descriptor, so as to achieve communication between parent-child processes, and between non-affinity pipelines, if you want to use the nameless pipe to communicate, you need to rely on a separate file descriptor delivery mechanism.
Well-known pipeline FIFO: A well-known pipeline is a real special file, using a well-known pipeline can be implemented with the host of arbitrary processes between the data interaction.
Second, application:
The nameless pipe in the 1,shell
Using a nameless pipe in the shell command line is a pipe-to-tube character "| "Implemented. Like Ps-ax|grep MySQL, pass the contents of Ps-ax through the pipeline to grep MySQL
2. Create a nameless pipeline
(1.) Function:
Pipe (Build pipeline):
1) header File
#include <unistd.h>
2) Prototypes:
int pipe (int filedes[2]);
3) function Description:
Pipe () Creates a pipeline and returns the file descriptor as a parameter filedes array.
Filedes[0] is the read end in the pipe
FILEDES[1] is the write end of the pipeline.
4) return value:
Returns zero if successful, or 1, which causes the error to exist in errno.
5) Error Code:
Emfile process has exhausted the maximum number of file descriptors
The Enfile system has no file descriptor available.
Efault parameter Filedes array address is not valid.
(2) Use:
#include <stdio.h> #include <unistd.h> #include <string.h> int main () { int p[2]; int pid; Char *str = "HelloWorld"; Char buf[128]; memset (buf, ' + '); if (pipe (p) = =-1) { printf ("function pipe () calls failed."); return-1; } if ((Pid=fork ()) = =-1) //Create a child process { printf ("function fork () calls failed.\n"); return-1; } else if (PID = = 0) //In the sub-process { printf ("In sub:pid=%d\n", Getpid ()); Write (P[1],str,strlen (str)); Writes Str }else { //In the parent process to printf ("In father:pid=%d\n", Getpid ()) to the nameless pipe; Read (P[0],buf,strlen (str)); Read the Nameless pipe printf ("In father:buf=%s\n", buf); } }
Results:
Note: Read and write are blocking modes
3. Create a famous pipeline
(1) Function: Mkfifo (establish named pipe)
Correlation function Pipe,popen,open,umask
1) Table header file
#include <sys/types.h>
#include <sys/stat.h>
2) Function prototypes
int Mkfifo (const char * pathname,mode_t mode);
3) Function description
Mkfifo () will establish a special FIFO file according to the parameter pathname, the file must not exist , and the parameter mode is the permission (mode%~umask) of the file, so the Umask value will also affect the permissions of the FIFO file.
4) The return value returns 0 if successful, otherwise returns-1, the cause of the error is stored in errno.
5) Error code
eaccess parameter pathname The directory path specified does not have executable permissions
The file specified by the Eexist parameter pathname already exists.
The path name for the Enametoolong parameter pathname is too long.
ENOENT parameter pathname contains a directory that does not exist
ENOSPC file system has insufficient remaining space
The Enotdir parameter pathname directory exists in the path but is not a real directory.
The Erofs parameter pathname the specified file exists in the read-only file system.
(2) Use:
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h>main () { char buffer[80]; int FD; Unlink (FIFO); Mkfifo (fifo,0666); if (fork () >0) { char s[] = "hello!\n"; FD = open (fifo,o_wronly); Write (fd,s,sizeof (s)); Close (FD); } else { fd= open (fifo,o_rdonly); Read (fd,buffer,80); printf ("%s", buffer); Close (FD);} }
Reference: Linux c Chinese function manual
Linux interprocess communication mechanism (IPC mechanism)-Pipeline