Linux interprocess communication (2): the use and principle of named pipes

I. Introduction

 As mentioned in the previous blog, the essence of inter-process communication is how to let two processes see the same resource . The core idea of ​​anonymous pipes is to let the child process inherit the parent process , so that the parent and child processes can see the same pipe file, but this also limits the communication to only blood-related processes.
 The processing method of the named pipe is relatively straightforward: create a FIFO pipe file under the specified path . Multiple processes read and write to the same named pipe file just like using ordinary files, thereby realizing inter-process communication. Although the named pipe is attached to the file system, the kernel will disconnect the disk when transferring data , write to the memory when writing, and read from the memory when reading, which is consistent with the anonymous pipe.

2. Anonymous pipes and named pipes

  • Anonymous pipes are created and opened by the pipe function.

  • Named pipes are created by the mkfifo function and opened with the open function

  • The only difference between a FIFO (named pipe) and a pipe (anonymous pipe) is the way they are created and opened , but once the work is done, they have the same semantics

  • The life cycle of pipes (anonymous pipes and named pipes) depends on the kernel, which is essentially a buffer in the kernel and communicates through the kernel buffer. Although the files of named pipes can be seen in the file system, they are only identifiers, not communication media. That is to say, the named pipe file we created is just a mark, and the file descriptor returned by opening the pipe file points to a buffer in the kernel. Note that we are writing to the buffer, not to the pipe file

Third, the use of named pipes

insert image description here
[Parameter Description]:

  • pathname: specify the path of the pipeline file
  • mode: Specifies the read and write permissions of the file. The actual permission of the pipeline file = mode & umask, the system default mask is0002

[Return value]: 0 is returned if the creation is successful, and -1 is returned if it fails
[Named pipe opening rules]:

  • Open FIFO file as read-only
    1. O_NONBLOCKdisable: Block until a corresponding process opens the FIFO file for writing
    2. O_NONBLOCKenable: return success immediately
  • Open a FIFO file for writing only
    1. O_NONBLOCKdisable: Block until a corresponding process opens the FIFO file for reading
    2. O_NONBLOCKenable: open failed and return immediately, the error code isENXIO

[demo]:
1. Comm.hpp file

#include <iostream>
#include <cstring>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define FIFO_PATH "./FIFO"

using namespace std;

void CreateFifo()
{
    
    
    if(access(FIFO_PATH, F_OK) != 0)  // 管道文件不存在才需要创建
    {
    
    
        if(mkfifo(FIFO_PATH, 0666) != 0)  // 创建管道文件失败
        {
    
    
            perror("mkfifo");
        }
    }
}

2. ServiceFifo file

#include "Comm.hpp"

int main()
{
    
    
    CreateFifo();
    int fd = open(FIFO_PATH, O_RDONLY);
    while(true)
    {
    
    
        char buff[15] = {
    
    0};
        ssize_t ss = read(fd, buff, sizeof(buff));
        buff[ss] = '\0';
        cout << buff << endl;
    }
}

3.ClientFIFO.cc file

#include "Comm.hpp"

int main()
{
    
    
    int fd = open(FIFO_PATH, O_WRONLY);
    int cnt = 0;
    while(true)
    {
    
    
        char msg[15] = "hello world";
        write(fd, msg, sizeof(msg));
        sleep(1);
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/whc18858/article/details/128420485