Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to realize the FIFO of Linux process Communication

Shulou Source: shulou.com Published: 2022-06-01 20:40:50 09月16日 Update

This article is about how to implement FIFO for Linux process communication. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

FIFO Communications (first in first out)

FIFO famous pipeline to achieve unrelated process communication.

Create a pseudo file for the pipe

A.mkfifo testfifo command creation

b. You can also use the function int mkfifo (const char * pathname, mode_t mode)

The kernel will open up a buffer for fifo files, manipulate fifo files, manipulate buffers, and achieve inter-process communication-in fact, reading and writing files

Man 3 mkfifo

# include # include 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 the fifo file, the read side will block and wait for the write side open, and the write side will also block and wait for another segment to open.

Code example:

File_w.c writing end

# include # include 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,'\ 0mm, 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 reader

# include # include 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,'\ 0mm, 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 What is Linux system Linux is a free-to-use and free-spread UNIX-like operating system, is a POSIX-based multi-user, multi-tasking, multi-threaded and multi-CPU operating system, using Linux to run major Unix tools, applications and network protocols.

Thank you for reading! This is the end of this article on "how to realize the FIFO of Linux process communication". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Tags: Communications files processes systems operating systems content more pipes articles buffers buffers blocking good practical famous free matters code tasks kernel Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS NVidia Docker MariaDB Microsoft