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

CSS clear float clear:both example code

Today I will talk to you about clearing floats. B...

Web Standard Application: Redesign of Tencent QQ Home Page

Tencent QQ’s homepage has been redesigned, and Web...

Docker deployment RabbitMQ container implementation process analysis

1. Pull the image First, execute the following co...

Solution to forgetting mysql password under linux

The problem is as follows: I entered the command ...

The principle and direction of JavaScript this

How to determine what this points to? ①When calle...

How to switch directories efficiently in Linux

When it comes to switching directories under Linu...

Learn Node.js from scratch

Table of contents url module 1.parse method 2. fo...

Detailed explanation of setting resource cache in nginx

I have always wanted to learn about caching. Afte...

Examples of using && and || operators in javascript

Table of contents Preface && Operator || ...

A quick solution to accidentally delete MySQL data (MySQL Flashback Tool)

Overview Binlog2sql is an open source MySQL Binlo...

Docker data storage tmpfs mounts detailed explanation

Before reading this article, I hope you have a ba...

Installation and configuration tutorial of MongoDB under Linux

MongoDB Installation Choose to install using Yum ...

Security configuration and detection of SSL after the website enables https

It is standard for websites to enable SSL nowaday...

VMware15/16 Detailed steps to unlock VMware and install MacOS

VMware version: VMware-workstation-full-16 VMware...