Process communication mode under Linux (IPC)--Pipeline communication

Source: Internet
Author: User

Unix IPC: Pipelines, Named Pipes (FIFO)

Piping

1. Concept

A pipeline is a one-way (half-duplex), first-out, non-structured byte stream that connects the output of one process with the input of another.

The write process writes data at the end of the pipeline, and the read process reads the data at the first end of the pipeline. When the data is read out, it is removed from the pipeline, and no other read process can read the data.

The pipeline provides a simple flow control mechanism. When a process attempts to read an empty pipeline, the process blocks until the data is written to the pipeline. Similarly, when the pipeline is full, the process attempts to write the pipeline, and the write process blocks until the other process reads the data from the pipeline.

2, the characteristics of the pipeline

(1) unidirectional data communication with fixed read and write end

(2) can only be used for communication between processes that have affinity (that is, between parent-child processes or sibling processes)

(3) The transmission of the pipeline is a non-formatted byte stream , requiring the pipeline input and the output party in advance to agree on a good data format

(4) pipeline life cycle with process, process exit, file will be recycled by the operating system

(5) Linux considers pipelines as a kind of file and manages the pipelines by means of file management, which can also make

Use normal read () and write () functions. but it is not an ordinary file and does not belong to any other file system, only

exists in the memory space of the kernel.

3. Pipeline creation and shutdown

The > pipeline is a file descriptor-based communication method that creates two file descriptors Fds[0] and fds[1 when a pipeline is established)

>FDS[0] Fixed for read pipe, fds[1] fixed for write pipe

> When the pipe is closed just close these two file descriptors and use the normal close () function to close each file descriptor individually

650) this.width=650; "title=" Picture 1.png "src=" http://s3.51cto.com/wyfs02/M00/83/9C/ Wkiol1d33ric8rknaaamkgh5sh8872.png "alt=" Wkiol1d33ric8rknaaamkgh5sh8872.png "/>


4. Pipeline Creation function

Required header files #include <unistd.h>
Function prototypes
int pipe (int fd[2])

function passed in value

FD[2] is used to save two file descriptors for the pipeline, and then you can manipulate the two file descriptors directly

function return value

Success: 0
Error:-1

5, the pipeline read and write instructions

> Steps to use a pipeline for inter-parent-child interprocess communication:

> Create pipeline: The parent process calls the pipe () function to create a pipe

> At this point, both the read and write end of the pipeline are in one process, and this pipeline is not much use.

> Parent process creates a child process through the fork () function

The > Child process inherits the pipeline created by the parent process, which corresponds to the file descriptor of the pipeline in the parent-child process.

> Determine the transport direction of the pipeline: close unrelated read or write-side file descriptors in the parent, child process, depending on the desired transport direction

> Communication: Call the Write () function in the write process to invoke the Read () function in the reading process

> Close pipeline: Call Close () to dismiss the file descriptor associated with the pipe.

650) this.width=650; "width=" 802 "height=" 427 "title=" Qq20160703001701.png "style=" WIDTH:581PX;HEIGHT:309PX; "src=" Http://s5.51cto.com/wyfs02/M02/83/9D/wKiom1d36VuBf35lAAEeoxsTnBg229.png "alt=" Wkiom1d36vubf35laaeeoxstnbg229.png "/>


650) this.width=650; "Width=" 798 "height=" 425 "title=" Qq20160703001750.png "style=" width:585px;height:331px; "src=" Http://s5.51cto.com/wyfs02/M00/83/9C/wKioL1d36ZXw3lFIAAGIauWooo8280.png "alt=" Wkiol1d36zxw3lfiaagiauwooo8280.png "/>


650) this.width=650; "Width=" 786 "height=" 425 "title=" Qq20160703001816.png "style=" WIDTH:513PX;HEIGHT:304PX; "src=" Http://s2.51cto.com/wyfs02/M01/83/9C/wKioL1d36gWyofMEAACenSbprLM712.png "alt=" Wkiol1d36gwyofmeaacensbprlm712.png "/>


> closed the parent process's write-end fd[1] and the child process's read-fd[0] so that a "child process is written to the parent process reads" can be established

The channel.

> can also close the parent process's read-side fd[0] and the child process's write-side fd[1], so you can create a "parent process to write to the child process read

Access to the "channel.

> Parent process can also create multiple sub-processes, each of which inherits the corresponding fd[0] and fd[1], just close the corresponding

The port can establish a channel between each child process (sibling process).

6.4 Special cases needing attention in piping (assuming all blocking I/O operations, not setting the O_NONBLOCK flag)

(1) If all the file descriptors pointing to the end of the pipe are closed (the reference count for the pipe write is 0), and the process still reads the data from the read-side of the pipe, then read the remaining data in the pipeline, and then read again returns 0, just like the end of the file.

(2) If all the file descriptors pointing to the pipe write end are not closed (the reference count of the pipe writes is greater than 0), and the process that holds the end of the pipeline does not write the data to the pipeline, then there is a process reading the data from the pipe, then 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) If all the file descriptors pointing to the pipe read end are closed (the reference count for the pipe read is 0), then 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) If all the file descriptors pointing to the pipe read end are not closed (the reference count of the pipe reads is greater than 0), and the process that holds the pipe read 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, The data is written and returned until there is an empty position in the pipeline.

7. Limitations of Anonymous pipelines

> Only one-way data flow is supported

> can only be used to communicate between processes that have affinity, without a name

> Buffer Limited, pipeline only exists in main memory, size is one page

The > transmits the unformatted byte stream


Named Pipes

1. Concept

A FIFO differs from a pipe in that it provides a path name associated with it and is stored in the file system as a FIFO file. A named pipe is a device file, so even if the process does not have a relationship with the process that created the FIFO, as long as the path can be accessed, it can communicate with each other through FIFO, it is worth noting that FIFO always works in first-in and out-of-the-box, the first written data will be read from the pipeline first.

2. Creation and use of Named pipes

Create:

Linux has two ways of creating Named pipes:

(1) Create a named pipe interactively under the shell

You can use the Mknod or MKFIFO command

(2) using system functions to establish named pipes in the program

there are two system functions that create a named pipe: Mknod and Mkfifo, the function prototypes are as follows:

650) this.width=650; "title=" Qq20160703145224.png "src=" http://s1.51cto.com/wyfs02/M02/83/9E/ Wkiol1d4tbgjz8dpaabtrcum6yg994.png "alt=" Wkiol1d4tbgjz8dpaabtrcum6yg994.png "/>

Both of these functions create a file that is actually present in the file system, filename Specifies the file name, and mode specifies the Read permission for the files. try to use MKFIFO (simple, standard)

The Mkfifo function is to create a file in the file system that provides a FIFO function, named pipes. Anonymous pipes are not visible to the file system and are limited to two processes between the parent and child processes, and the named pipe is a visible file, so it can be used for any two interprocess communication, regardless of whether the two processes are parent-child or not, regardless of whether the two processes are not related to each other.

How to use:

Named pipes are used in the same way as pipelines, except that when you use Named pipes, you must first call open (). Because a named pipe is a file that exists on the hard disk, the pipeline is a special file that exists in memory.

Note: Calling Open opens a named pipe process that may be blocked. However, if you open it in both read and write mode (O_RDWR), it must not be blocked, and if opened as read-only (o_rdonly), the process that calls the open function will be blocked until a writer opens the pipe, and the Write (o_wronly) Open will block until the read party opens the pipeline.


Process communication mode under Linux (IPC)--Pipeline communication

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.