Linux Pipe Functions

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.