Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the FIFO of Linux process Communication

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report