Brief introduction of pipeline pipe for inter-process communication of Linux (bottom)

Source: Internet
Author: User
Tags function prototype

In the last article, I believe that we have a vague understanding of the concept of pipelines, this article through code examples to strengthen the understanding of the pipeline.

The pipe function is mainly used to create pipelines, and the pipes are prototyped as follows:

First, function prototype

#include <unistd.h>

int pipe (int pipefd[2]);

Parameters: An integer array, after the pipeline is created successfully, Pipefd[0] represents the read end of the pipeline, Pipefd[1] represents the write end of the pipeline.

A successful return of 0, the failure returns-1, while the errno is set.

Second, parent-child process communication

Above, we describe the entire process by which the parent-child process communicates through a pipeline, and now we use the C language:

#include <stdio.h> #include <unistd.h>int main () {    int fds[2] = {0};    1. Create pipe    if (pipe (FDS) ==-1) {        perror ("pipe");        return 1;    }    2.fork Sub    -process pid_t pid = fork ();    if (PID > 0) {        //father        //3. Parent process closes the read end, writes the data to the write side        close (fds[0]);        Char buff[1024]={0};        printf ("Parent to child#");        Fflush (stdout);        ssize_t s = Read (0,buff,sizeof (Buff)-1);        BUFF[S-1] = 0;        Write (fds[1],buff,s);        Close (fds[1]);    } else if (pid==0) {   //child        //3. Child process closes the write end, reading the data from the read-side        close (fds[1]);        Char buff[1024]={0};        ssize_t s = Read (fds[0],buff,sizeof (buff));        printf ("Child to Receive#%s\n", buff);        Close (Fds[0]);    } else{        perror ("fork");        return 2;    }    Wait (NULL); Reclaim the subprocess    return 0;}

In the code above, we show the normal communication situation of the parent-child process, and we need to consider four exceptions in addition to the normal situation:

Three or four cases

In order to save the article space, the code part omitted, the reader can self-test.

1. The parent process continues to write data to the pipeline, the child process does not read the data from the pipeline, and the subprocess Fds[0] read-side remains open.

This situation causes the pipeline to be full, while the parent process is blocked until there is an empty position in the pipeline.

2. The parent process continuously writes data to the pipeline, the child process does not read the data from the pipeline, and the subprocess closes the fds[0] read end.

In this case, the parent process receives the sigpipe signal and is then terminated.

3. The parent process writes some data to the pipeline, and the child process continuously reads the data from the pipeline, and the parent process Fds[1] writes the end to remain open.

In this case, the child process enters the blocking state after the data in the pipeline has been read. Until new data is generated in the pipeline.

4. The parent process writes some data to the pipeline, and the child process continuously reads the data from the pipeline, and the parent process closes the fds[1] write end .

In this case, the child process reads the data in the pipeline, and the last read returns 0, just like the end of the file.

Brief introduction of pipeline pipe for inter-process communication of Linux (bottom)

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.