interprocess communication _03 Named pipes

Source: Internet
Author: User

Why do you have a named pipeThe generation of anonymous pipelines overcomes a small amount of data transfer between related processes, but Anonymous pipelines cannot transmit data between processes that are not related .
To resolve the problem, a named pipe appears.
Named pipes are also allocated a block of storage in the kernel, and is associated with a file name and stored in the file system as a FIFO (first in first out) file. Such other unrelated processes only need to know the name of the pipe to be able to access the pipeline .

Two and anonymous pipe differences in usageand anonymous pipes. The only difference is how the pipeline is created and opened differently , other modes of operation are completely consistent.
When created: Named Pipes are opened with the Mkfifo function, the Named pipes open function anonymous pipe, create and open with the pipe function step implementation.

operation of three named PipesThe operation of the anonymous pipe is similar to the file as a special file。 The only difference from ordinary files is that when created normal files are created with the Create () function or the open () function anonymous pipes created with Mkfifo
four creation of named Pipesmode One: manually created$ mkfifo FileName
mode two: code creation
#include <sys/types.h> #include <sys/stat.h>int mkfifo (const char * pathname,     //pipe name with path                  mode_t mode)        ///Pipe operation permissions//return value: 0 (Success),-1 (open failed, error code see ERRNO)

value of mode parameters:s_i (r/w/x) (Usr/grp/oth), s_irwx (u/g/o)the r/w/x, respectively, represent read/write/run permissions,Usr/grp/oth indicates the file owner/file group/other User S_irwxu =S_IRUSR |S_IWUSR |s_ixusr          S_irwxg =S_irgrp |S_iwgrp |S_ixgrp          S_irwxo =S_iroth |S_iwoth |S_ixoth
For example, S_IRUSR indicates that all files have read access;s_iwusrindicates that all files have write permission;Multiple values with ' | ' Connection, for examples_irusr|S_iwusr saysall files have read and write permissionThe above values can also be directly replaced with octal numbers, such as: 00600 meansall files have read and write permission

opening of five named pipesThe process that is responsible for writing the named Pipes has just written in the form of a named pipe that is responsible for reading the named pipes in the form of a read-only open named pipe. both the read and write ends of the named Pipes are connected to the successful opening of the named pipe, otherwise the process is blocked in the open function (without setting the non-clogging flag).
#include <fcntl.h> #include <sys/stat.h> #include <sys/types.h>int open (const char* pathname,     // Pipe name with path                int flags          //Pipe open way                ...);              Other parameters, such as the ability to assign permissions at the time of creation, which is created by MKFIFO, ignores//returns the value: >2 (descriptive descriptor of the return pipeline), 1 (open failed, error code see ERRNO)

Flags parameter Value

O_rdonly opening files as read-only

O_wronly open a file as a write-only method

The O_rdwr opens the file in a writable and writable manner.

The above three flags are mutually exclusive and can only be taken one, with the following options or combinations of operations (|) Use.

O_creat If the file you want to open does not exist, you can create the file on your own initiative.

O_EXCL is generally used in conjunction with the O_creat, and when the file is created, it is found that the file already exists.

O_nonblock Open the file in an unstoppable way, which means that no data is read or waiting, and it is returned to the process immediately.

O_trunc if the file exists and opens in a writable manner, empty the contents of the file.

O_append moves the file cursor to the end of the file while writing data.

O_sync Open the file in a synchronized manner.

O_noctty if the file to be opened is a terminal device, the terminal is not treated as a Process control terminal.  

O_nofollow assuming that the file referred to by pathname is a symbolic connection, the open file will fail.

O_directory assuming that the file referred to in pathname is not a folder, it will fail to open the file.


Six Use cases
/************************************************************************************ > File name:testfifo.c & Gt Author:qiaozp > Mail: [email protected] > Created time:2014-9-17 14:29:35 > Step:1 call mkfifo function creation Build a named pipe 2 the write-side process uses the Open function to o_wronly the pipe, and reads data 3 in the read-side process with the open function to connect the pipe in a o_rdonly way, and reads the data with the Read function * /#include <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) {i F (argc! = 2) {cout << "Please add a number of references [r/w], R for the read pipeline, and W for the write pipeline.        "<< Endl;    return-1; }//Create Pipeline if ((Mkfifo (pipe_name, s_irwxu) = =-1) && (errno! = eexist)) {cout << create pipeline [< < Pipe_name << "] failure, error message [" << strerror (errno) << "]." << Endl;    return-1;    } int fd;    Char Buff[buff_len];            if (strncasecmp (Argv[1], "w", 1) = = 0) {//write pipeline Process if (FD = open (Pipe_name, o_wronly) = = = 1) { cout << "Connection pipeline [" << pipe_name << "] failure, error message [" << strerror (errno) << "]." << End            L        return-1;            } do {cout << "Please enter data that needs to be transferred 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 << "] failure, error message [" << strerror (errno) << "]." &L            t;< Endl;        return-1;            } do {memset (buff, 0, Buff_len);           Read (fd, buff, sizeof (buff)); cout << "Data read from the pipeline:" << buff << Endl;    } while (strncasecmp (Buff, "End", 3)! = 0); } else {cout << "Please add the correct number of references [r/w], R for the read pipeline, and W for the write pipeline.    "<< Endl; } return 0;}





interprocess communication _03 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.