Interprocess Communication--Named Pipes

Keywords: shell

Several terms

  • Ambiguity: When we write data to a pipe, such as a hello, when we write he, the reader already reads the data, so this is incorrect. This is ambiguity.
  • Critical resource: A common storage resource accessible by multiple streams
  • Critical Zones: Codes that access critical resources are called critical zones
  • Mutual exclusion: Only one process at any time can access a critical zone using critical resources, and it is accessed atomically
  • Atomicity: either access or no access, there is no access in progress
  • Hunger: A process that requires access to a resource in certain scenarios and is not accessible because of its low level creates a hunger problem
  • Synchronization: Access critical resources in a sequence

name pipes

The concept of named pipes

One disadvantage of anonymous pipes is that anonymous pipes can only be used for communication between processes that are related, but there are many times when communication between processes is not related. At this time, some other modes of communication are needed, followed by named pipes.Named pipes connect processes through a path, which can be accessed by only one process if it can access a path

Creation, Read and Write of Named Pipes

Under the shell we can use mknod and mkfifo to create pipes

Here we often use mkfifo, so we just introduce one mkfifo and the other without going into details
We can use mkfifo myfifo.pipe under the shell to create a named pipe, then remember its path, we can open a pipe directly to read and write when using it

Here is the mkfifo function used in the program. First look at the interface prototype

#include <sys/types.h>
#include <sys/stat.h>
int mkfifo(const char *filename, mode_t mode);

Explain some return values and parameters, create a successful return of 0, and fail return of -1.
The parameter const char *filename is a path plus a file name. The mode_t mode is the permission for the operation of the pipeline we created. We can set it to 0666 | S_IFIFO as follows, in which S_IFIFO is a fixed way to name the pipeline without regard to it.

We can use functions as follows

if(fifo("./pipe",0666 | S_IFIFO) < 0)
{
    printf("fifo error");
    return 1;
}

Here we use a simple program to achieve a write-side and a read-side operation. At this time, we can open two terminals and run the following programs separately, which will achieve interprocess communication, one write, one read

Writing end

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>

#define _PATH_ "/home/xiaoxu/code/fifo/log.pipe"

int main()
{
    umask(0);
    if(mkfifo(_PATH_,0666|S_IFIFO) < 0 )
    {
        printf("fifo error\n");
        return 1;
    }
    int fd = open(_PATH_,O_WRONLY);
    if(fd <= 0)
    {
        printf("open error\n");
        return 1;
    }
    else
    {
        char buf[100];
        memset(buf,'\0',sizeof(buf));
        while(1)
        {
            scanf("%s",&buf);
            int wr = write(fd,buf,sizeof(buf));
            if(wr <= 0)
            {
                printf("write error\n");
                break;
            }
        }

    }
    close(fd);
    return 0;
}

Reader

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>

#define _PATH_ "/home/xiaoxu/code/fifo/log.pipe"

int main()
{
    int fd = open(_PATH_,O_RDONLY);   //Open a pipe read-only
    if(fd < 0)    //Failed to open pipe
    {
        printf("read error\n");
        return 1;
    }
    else   //Pipeline opened successfully
    {
        char buf[100];
        memset(buf,'\0',sizeof(buf));
        while(1)
        {
            int red = read(fd,buf,sizeof(buf));  //Reads the contents of the pipe into buf, so it should be initialized with memset
            if(red <= 0)   //
            {
                printf("read error\n");
                break;
            }
            printf("%s\n",buf);
        }
        close(fd);
    }
    return 0;
}

Posted by fiddy on Fri, 21 Jun 2019 10:02:27 -0700