Inter-process communication _ 03 Named Pipe

Source: Internet
Author: User
1. Why is there a named pipeline?The generation of anonymous pipelines solves a small amount of data transmission between unrelated processes, Anonymous pipelines cannot transmit data between unrelated processes.
To solve this problem, a named pipe emerged.
The named pipe is also allocated a storage area in the kernel, and It is associated with a file name and stored in the file system as a FIFO (first in first out) file. In this way, other unrelated processes can access the MPs queue as long as they know the MPs queue name.

2. Differences between the use of anonymous PipelinesAnd anonymous Pipeline The only difference is that the pipeline creation and opening methods are different..
When creating the pipeline, use the mkfifo function to name the pipeline. When opening the pipeline, use the open function to name the anonymous pipeline. When creating and opening the pipeline, use the pipe function to implement it in one step.

Operations on Named PipesOperations on anonymous pipelines are similar to operations on files. As a special file. The only difference from a common file is that Create a common file using the CREATE () function or the open () function Create an anonymous MPs queue using mkfifo
4. Create a named PipelineMethod 1: manually create $ mkfifo filename
Method 2: code creation
# Include <sys/types. h> # include <sys/STAT. h> int mkfifo (const char * pathname, // MPs queue name with path mode_t mode) // MPs queue operation permission // return value: 0 (successful ), -1 (opening failed. For error codes, see errno)

Mode Parameter Value: S_ I (R/W/X) (usr/GRP/oth), s_irwx (U/g/O), where, r/W/X indicates read/write/execute permissions respectively, usr/GRP/oth indicate the file owner/file group/other users s_irwxu = s_irusr | s_iwusr | s_ixusr records = s_irgrp | s_iwgrp | s_ixgrp s_irwxo = s_iroth | s_iwoth | s_ixoth
For example, s_irusr indicates that the file owner has the read permission; s_iwusr indicates that the file owner has the write permission; multiple values are connected using '|, for example, s_irusr | s_iwusr indicates that the file owner has the read and write permissions. The preceding value can also be directly replaced by an octal number. For example, 00600 indicates that the file owner has the read and write permissions.

5. Open the Named PipeThe process responsible for writing the named pipeline has opened the named pipeline only in the form of writing, and the process responsible for reading the named pipeline has opened the named pipeline in the form of read-only. The read and write ends of the named pipeline are connected to each other to open the named pipeline. Otherwise, the process will be blocked in the open function (without setting the non-blocking mark ).
# Include <fcntl. h> # include <sys/STAT. h> # include <sys/types. h> int open (const char * pathname, // pipeline name with path int flags // pipeline opening method ...); // other parameters. For example, you can grant permissions during creation. Here, because mkfifo is created, the result is ignored. // return value:> 2 (return the descriptor of the pipeline ), -1 (opening failed. For error codes, see errno)

Flags parameter values:

O_rdonly open a file in read-only mode

O_wronly open the file in write-only mode

O_rdwr can open files in read/write mode.

The three flags are mutually exclusive and can only be used with one of the following options or operations (|.

O_creat: if the file to be opened does not exist, the file is automatically created.

O_excl is generally used with o_creat. If it is set, an error is returned if the file already exists when the file is created.

O_nonblock open the file in an unblocking way, that is, whether there is data read or waiting, it will immediately return to the process.

O_trunc clears the content of an object if the object exists and is opened in writable mode.

When writing data, o_append moves the file cursor to the end of the file.

O_sync opens the file in synchronous mode.

If the file to be opened is a terminal device, o_noctty does not regard the terminal as a process control terminal.

O_nofollow if the file indicated by pathname is a symbolic connection, opening the file will fail.

O_directory if the file indicated by the pathname parameter is not a directory, opening the file will fail.


6. Use instances
/*************************************** **************************************** * ***> File Name: testfifo. c> author: qiaozp> mail: [email protected]> created time: 14:29:35> step: 1. Call the mkfifo function to create a named MPs queue. 2. Use the OPEN function to connect to the MPs queue in o_wronly mode, use the write function to read data. 3. The reading process uses the OPEN function to connect to the pipeline in o_rdonly mode, use the READ function to read data *********************************** **************************************** * ********/# includ E <errno. h> # include <fcntl. h> # include <string. h> # include <iostream> # include <sys/STAT. h> # include <sys/types. h> using namespace STD; # define buff_len 100 # define pipe_name ". /pipeone "int main (INT argc, char ** argv) {If (argc! = 2) {cout <"add the parameter [R/W] To the end. r indicates the read pipeline; W indicates the write pipeline. "<Endl; Return-1;} // create the pipeline if (mkfifo (pipe_name, s_irwxu) =-1) & (errno! = Eexist) {cout <"create pipeline [" <pipe_name <"] failed, error message [" <strerror (errno) <"]. "<Endl; Return-1;} int FD; char buff [buff_len]; If (strncasecmp (argv [1]," W ", 1) = 0) {// process of the write pipeline if (FD = open (pipe_name, o_wronly) =-1) {cout <"connection pipeline [" <pipe_name <"] failed, error message [" <strerror (errno) <"]. "<Endl; Return-1 ;}do {cout <" Enter the data to be transmitted from the pipeline: "; memset (buff, 0, buff_len ); scanf ("% s", buff ); Write (FD, buff, sizeof (buff);} while (strncasecmp (buff, "end", 3 )! = 0);} else if (strncasecmp (argv [1], "r", 1) = 0) {// read pipeline process if (FD = open (pipe_name, o_rdonly) =-1) {cout <"connection pipeline [" <pipe_name <"] failed, error message [" <strerror (errno) <"]. "<Endl; Return-1 ;}do {memset (buff, 0, buff_len); read (FD, buff, sizeof (buff )); cout <"The data read from the MPs queue is:" <buff <Endl;} while (strncasecmp (buff, "end", 3 )! = 0);} Add the correct parameter [R/W] To the end of else {cout <". r indicates the read pipeline; W indicates the write pipeline. "<Endl;} return 0 ;}





Inter-process communication _ 03 Named Pipe

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.