Implementation of FIFO in Linux process communication

Implementation of FIFO in Linux process communication

FIFO communication (first in first out)

FIFO named pipe, which enables communication between non-related processes.

  • Create a pseudo file for a pipe
    • a. Create a testfifo command using mkfifo
    • b. You can also use the function int mkfifo(const char *pathname, mode_t mode);
  • The kernel will open a buffer for the fifo file and operate the fifo file. It can operate the buffer and realize inter-process communication - in fact, it is file reading and writing.

man 3 mkfifo

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

Note:

FIFOs
Opening the read or write end of a FIFO blocks until the other end is also opened (by another process or thread). See
fifo(7) for further details.

When opening a fifo file, the read end will block waiting for the write end to open, and the write end will also block waiting for another end to open.

Code example:
file_w.c write end

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

int main(int argc, char *argv[]) {
  if(argc != 2) {
    printf("./a.out filename1\n");
    return -1;
  }
  printf("begin open w\n");
  int o_ret = open(argv[1], O_WRONLY);
  printf("end open w\n");
  char buf[256];
  int num = 0;
  while (1) {
    memset(buf, '\0', sizeof(buf));
    sprintf(buf, "xiaoming--%d", num++);
    printf("strlen(buf) = %d\n", strlen(buf));
    write(o_ret, buf, strlen(buf));
    sleep(1);
  }
  close(o_ret);
  return 0;
}
 

file_r.c Read side

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

int main(int argc, char *argv[]) {
  if(argc != 2) {
    printf("./a.out filename1\n");
    return -1;
  }
  printf("begin open r\n");
  int o_ret = open(argv[1], O_RDONLY);
  printf("end open r\n");
  char buf[256];
  int num = 0;
  while (1) {
    memset(buf, '\0', sizeof(buf));
    read(o_ret, buf, sizeof(buf));
    printf("strlen(buf) = %d\n", strlen(buf));
    printf("read is%s\n", buf);
  }
  close(o_ret);
  return 0;
}

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Linux inter-process communication via anonymous pipes
  • Linux inter-process communication method socket usage example
  • A brief discussion on Linux inter-process communication methods and their advantages and disadvantages
  • Detailed explanation of Linux message queue implementation of inter-process communication
  • Linux inter-process communication - using signals
  • Linux interprocess communication - using stream sockets
  • Detailed explanation of Linux inter-process communication - using semaphores
  • Detailed explanation of Linux inter-process communication - using shared memory
  • Linux applet about inter-process communication

<<:  4 ways to modify MySQL root password (summary)

>>:  jQuery implements shopping cart function

Recommend

Implement QR code scanning function through Vue

hint This plug-in can only be accessed under the ...

Example of using JS to determine whether an element is an array

Here are the types of data that can be verified l...

Detailed explanation of NodeJS modularity

Table of contents 1. Introduction 2. Main text 2....

Introduction to Royal Blue Color Matching for Web Design

Classical color combinations convey power and auth...

mysql charset=utf8 do you really understand what it means

1. Let's look at a table creation statement f...

Native JS to achieve sliding button effect

The specific code of the sliding button made with...

Analysis of the Neglected DOCTYPE Description

doctype is one of them: <!DOCTYPE HTML PUBLIC &...

How to use cookies to remember passwords for 7 days on the vue login page

Problem Description In the login page of the proj...

How to build SFTP server and image server on Linux cloud server

First of all, you can understand the difference b...

Form submission page refresh does not jump

1. Design source code Copy code The code is as fol...

CSS achieves highly adaptive full screen

When writing my own demo, I want to use display:f...

Detailed explanation of CSS weight value (cascading) examples

•There are many selectors in CSS. What will happe...