Inter-process communication-Named pipes

Source: Internet
Author: User

Concept

  • One disadvantage of the pipeline is that there is no name, so it can only be used for inter-process communication with affinity, and the named pipe (named pipe or FIFO) solves this problem.

  • FIFO provides a path name associated with it, stored in the file system as a FIFO file. The pathname in the file system is global and can be accessed by each process, so a path name in the file system can be used to identify an IPC channel.

  • Anonymous pipelines (pipelines) are not visible to the file system, and are limited to communicating between the parent and child processes two processes. The named pipe is a visible file , so he can communicate between any two processes , regardless of whether the two processes are parent-child or not, regardless of whether the two processes are related to each other.

  • FIFO always works according to the principle of FIFO (first Input first Output), the data that is written will first be read out of the pipeline.

  • because everything in Linux can be considered a file, the use of named pipes becomes very , it is also very convenient to use, and we can use it as usual file name in the command.

To create a named pipe

There are two ways of creating named Pipes under Linux.

    • Use the mkfifo command to interactively create a named pipe under the shell.

    • Use the Mkfifo system call function in your program to create a named pipe.

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/87/06/wKiom1fRaTeQrJDdAAAwVWN4LsM279.png "title=" Qq20160908213538.png "alt=" Wkiom1frateqrjddaaawvwn4lsm279.png "/>

Pathname is the full pathname of the named pipe that was created, mode is the pattern that creates the named pipe, indicating its read and write permissions. Returns 0 on success, returns 1 on failure, and creates an appropriate error code.

Using Named pipes

Named pipes can be used after they are created, and the use of named pipes and pipelines is basically the same, except that when you use Named pipes, you must first open (). Because a named pipe is a file that exists on the hard disk, the pipeline is a special file that exists in memory.

It is important to note that a process that calls open () opens a named pipe may be blocked, ① but will not cause blocking if it is opened in both read and write mode (O_RDWR), and the process that calls the open () function will be blocked until a write party opens the pipeline if the ② is opened in read-only (o_rdonly) mode ③ if it is opened in write mode (o_wronly), it will also block until a read party opens the pipe.

Code

    • FIFO Write End

650) this.width=650; "src=" http://s2.51cto.com/wyfs02/M00/87/04/wKioL1fRd1qyLeWBAAYMHSucU4o593.jpg "title=" Fifo_ Wr.jpg "alt=" Wkiol1frd1qylewbaaymhsucu4o593.jpg "/>


    • FIFO Read End

650) this.width=650; "src=" http://s5.51cto.com/wyfs02/M01/87/04/wKioL1fRd7vhorBdAATp8lB2mTs930.jpg "title=" Fifo_ Read.jpg "alt=" Wkiol1frd7vhorbdaatp8lb2mts930.jpg "/>






650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/87/04/wKioL1fRehvRJpCMAAGOyPY4vwc056.png "title=" Test.png "alt=" Wkiol1frehvrjpcmaagoypy4vwc056.png "/>


This article is from the "zgw285763054" blog, make sure to keep this source http://zgw285763054.blog.51cto.com/11591804/1850892

Inter-process communication-Named pipes

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.