Pipeline communication under Linux

Source: Internet
Author: User

Pipelines and message queues are essentially the same, all of which are buffers of the Linux kernel.

Pipe limits
The pipe is half-duplex, and the data can only flow in one direction; two pipelines are required to communicate with each other
Can only be used to communicate between processes that have a common ancestor (a process that has affinity); Typically, a pipeline is created by a process, and then the process calls fork, and the pipeline is then applied between the parent and child processes. The plumbing is getting smaller.

Pipe reading and writing rules

When there is no data to read

The read call is blocked, that is, the process pauses execution until the data arrives.

If the setting does not wait, the read call returns a -1,errno value of Eagain.

When the pipes are full

The write call blocks until a process reads the data.

If the setting does not wait, the call returns a -1,errno value of Eagain

When the pipes are written and full

Write Call blocking

The call returns a -1,errno value of Eagain

If the corresponding file descriptor for all the pipe writes is closed, read returns

The write operation generates a signal if the file descriptor corresponding to the read end of all the pipes is closed sigpipe

When the amount of data to be written is not greater than PIPE_BUF, Linux guarantees the atomicity of the write.

When the amount of data to be written is greater than Pipe_buf, Linux will no longer guarantee the atomicity of writes.

Pipeline essence, a fixed-size buffer in the kernel.

The command in the shell script is essentially implemented by pipeline technology.

A pipeline is created first, and then several commands are created to create several processes

For the process that executes the first command, the read end of the process is closed, and the write end of the marked pipe is copied to the standard output using the DUP2 command, and then the EXEC function family is executed, which is equivalent to writing the pipe. For a process that executes the second command,

The write end is closed and the read end of the pipe is copied to the standard output using the DUP2 command, and then the EXEC Function Group command is executed, so that the output of the first command is equivalent to the input of the second command, and the redundant commands are similar.

Named pipes

One limitation of pipeline applications is that they can only communicate between processes that have a common ancestor (affinity).

If we want to exchange data between unrelated processes, you can do this with a FIFO file, which is often referred to as a named pipe. A named pipe is a special type of file.

Create a named pipe

Named pipes can be created from the command line by using the following command: MKFIFO filename

Named pipes can also be created from the program, and related functions are: int mkfifo (const char *filename,mode_t);

Pipeline communication under Linux

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.