Interprocess communication Pipeline (PIPE,FIFO)

Source: Internet
Author: User

Operation Principle of pipeline

Piping is the most basic IPC mechanism, created by the pipe function

#include <unistd.h>int pipe (int _pipe[2]);

Call the pipe function in the kernel to open a buffer for communication, it has a read end and a write end, through the Filedes parameter out to the program two file descriptor, Filedes[0] point to the read end of the pipeline, Filedes[1] point to the write end of the pipeline. The pipeline is like an open file, via read (Filedes[0]), or write (Filedes[1]), which reads and writes data to this file, actually reading and writing the kernel buffer. The pipe function call returned 0 successfully, and the call failed to return-1.

The steps for communication are as follows:

  1. parent process Create pipeline

  2. Parent Process fork out child process

    When the parent process calls fork to create a child process, the child process also has two file descriptors pointing to the same pipe.

  3. Parent process Close fd[0], child process close fd[1];

    The parent process closes the pipe read end, and the child process closes the pipe write end. The parent process can write to the pipeline, the child process can be read from the pipeline, the pipeline is implemented by the ring queue, the data flows from the writing end flow from the reading end, to achieve inter-process communication.

There are some special cases for pipeline use:

(1) All file descriptors pointing to the end of the pipe are closed (the reference count for the pipe write is equal to 0), while there is still a process reading the data from the read end of the pipe, then read again will return 0, as if reading to the end of the file.
(2) The file descriptor pointing to the end of the pipe is not closed (the reference count of the pipe write end is greater than 0), and the process that holds the end of the pipeline does not write data to the pipeline, when there is a process reading the data from the pipe, the remaining data in the pipeline is read, again read will block, The data is read and returned until the data in the pipeline is readable.
(3) All the file descriptors that point to the pipe read end are closed (the reference count for the pipe read is equal to 0), when there is a process to write to the pipeline, then the process receives the signal sigpipe, which usually causes the process to terminate unexpectedly.
(4) The file descriptor pointing to the end of the pipe is not closed (the reference count of the pipe is greater than 0), and the process holding the read end of the pipeline does not read the data from the pipe, then there is a process to write the data to the pipe, then write again when the pipeline is full block, until there is empty position in the pipeline to

Features of the piping:

    1. Only single data communication is allowed (often used for parent-child interprocess communication)

    2. Pipelines are dependent on the file system

    3. Only allow communication between processes that have a blood relationship

    4. Data transmission service oriented to byte stream;

    5. The life cycle of a pipeline: following a process


Named Pipes (FIFO)


This article from the "777 Fast" blog, declined reprint!

Interprocess communication Pipeline (PIPE,FIFO)

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.