1. Function description
Pipe (Build pipeline):
1) header file #include <unistd.h>
2) define function: int pipe (int filedes[2]);
3) function Description: Pipe () creates a pipeline and returns the description of the file as a filedes array of parameters.
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, otherwise returns-1, the cause of the error is stored in errno.
Error code:
The emfile process has run out of file descriptive narratives
The Enfile system has no documented descriptive terms available.
Efault parameter Filedes array address is not valid.
2. For example
#include <unistd.h> #include <stdio.h>int main (void) { int filedes[2]; Char buf[80]; pid_t pid; Pipe (filedes); Pid=fork (); if (PID > 0) { printf ("Father Process,here write a string to the pipe.\n"); Char s[] = "Hello world, this is write by pipe.\n"; Write (Filedes[1], S, sizeof (s)); Close (Filedes[0]); Close (filedes[1]); } else if (PID = = 0) {printf ("This was in the child Process,here read a string from the pipe.\n"); Read (Filedes[0], buf, sizeof (BUF)); printf ("%s\n", buf); Close (Filedes[0]); Close (filedes[1]); } Waitpid (PID, NULL, 0); return 0;}
Execution Result:
[Email protected] src]# gcc pipe.c
[Email protected] src]#./a.out
The Process,here read a string from the pipe.
The Father Process,here write a string to the pipe.
Hello world, which is the write by pipe.
When the data in the pipeline is read, the pipeline is empty. A subsequent read () call will be blocked by default, waiting for some data to be written.
If you need to set to non-clogging, you can do the following settings, for example:
Fcntl (Filedes[0], F_SETFL, O_nonblock);
Fcntl (Filedes[1], F_SETFL, O_nonblock);
Linux Pipe Functions