Linux-interprocess communication-Anonymous pipeline

Source: Internet
Author: User
Tags function prototype

I. Concept: interprocess communication ( IPC,interprocess communication)

Each process has a different user address space, and the global variables of any process are not visible in the other process, so the exchange of data between cities must pass through the kernel,

In the kernel to open a buffer process 1 data from the user space copied to the kernel buffer, process 2 and then read the data from the kernel buffer, the mechanism provided by the system becomes inter-process

Communication (IPC,interprocess communication).

Second, the pipeline

Pipelines are divided into anonymous pipes and named Pipes, where we only speak anonymous pipes, and the implementation of named Pipes will be shared in the next article.

1, pipeline is one of the most basic IPC mechanism, has the pipe function to create:

Header file #include <unistd.h>

Function prototype:int pipe (int fileds[2]);

Return value: The call returned 0 successfully, and the call failed to return -1.

When the pipe function is called, a buffer (called a pipe) is opened in the kernel for communication, and he has a write end to the read end, which is then passed to the user via the fileds parameter .

Program two file descriptor,Fileds[0] points to the read end of the pipe,Fileds[1] points to the write end of the pipeline. So the pipeline in the user program looks like an open file, through

  Read (fileds[0]); or write (fileds[1]) reads and writes data to this file, actually reads and writes the kernel buffer.

2, the pipeline to achieve inter-process communication process:

  STEP1: The parent process calls pipe to open a pipeline and gets two file descriptors pointing to the read and write ends of the pipeline;

STEP2: The parent process calls the fork function to create the child process, then the child process also has two file descriptors pointing to the read and write ends of the same pipeline;

STEP3: The parent process closes the pipe read end, and the child process closes the pipe write end. The parent process can write data in the network pipeline, the child process can read the data from the pipeline, the pipeline is applicable

The probation queue realizes that the data flows from the writing end and flows out from the reading end, thus realizing interprocess communication.

3, the characteristics of anonymous pipeline (short pipe):

(1) pipelines only support one-way communication. (To have the parent/child interprocess communication close the corresponding read and write ends)

(2) pipelines are used for inter-process communication with relatives, often for inter-parent interprocess communication. (The child process inherits the file descriptor of the parent process, so it can point to the same pipe and see the same data)

(3) pipelines are dependent on the file system.

(4) The life cycle of the pipeline ends with the end of the process.

(5) byte stream oriented

(6) synchronous mutual exclusion mechanism is provided inside the pipeline

4, the implementation of anonymous pipelines:

Operation Result:

Linux-interprocess communication-Anonymous pipeline

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.