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 create FIFO File under Linux

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

Share

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

In this article Xiaobian introduces in detail "how to create a FIFO file under Linux", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to create a FIFO file under Linux" can help you solve your doubts.

FIFO files are named pipes, by which data can be exchanged between unrelated processes. FIFO has a pathname associated with it and exists in the file system as a special device file.

The difference between FIFO and pipe is:

FIFO has a corresponding inode in the file system, which can be viewed through the ls command.

Sh-3.2# ls-lhF. / fifo_file100 prwxrwxrwx 1 root root 0 Jan 1 1970 . / fifo_file | sh-3.2#

Because it has a name, any process can access it, so FIFO can be used for communication between any two processes.

Pipe does not have a name and its existence cannot be seen in the existing file system.

It can only be used for inter-process communication such as father-son process, sibling process and so on.

The method to create FIFO is as follows:

\ 1. Call umask system call to set the permission to create a file

# include "sys/types.h" # include "sys/stat/h" mode_t umask (mode_t mask)

\ 2. Delete the existing fifo first by calling the unlink system call

# include "unistd.h" int unlink (const char * pathname)

\ 3. Call the mkfifo library function to create a FIFO file

# include "sys/types.h" # include "sys/stat.h" int mkfifo const char * pathname, mode_t mode)

Or you can create a FIFO file by calling mknod system call and specifying the parameter mode as S_IFIFO

# include "sys/types.h" # include "sys/stat.h" # include "fcntl.h" # include "unistd.h" int mknod (const char * pathname, mode_t mode, dev_t dev); Note:

\ 1. Use FIFO for communication, and the data transferred each time should be limited to PIPE_BUF.

\ 2. Access to FIFO is like accessing a regular file (regular file), which can be accessed using system calls such as open/read/write/close.

Applications that use FIFO include:

\ 1. Pure producer / consumer problem, one process reads data, one process writes data

\ 2. To achieve the client/server architecture of the program, the client and server-side communication through FIFO.

The above is the introduction of Linux's method of creating FIFO pipes. This article not only introduces the creation of FIFO pipes, but also briefly introduces the use of FIFO and its precautions, which should be paid special attention to when using.

Read here, this "how to create FIFO files under Linux" article has been introduced, want to master the knowledge of this article also need to practice and use to understand, if you want to know more about the article, welcome to follow the industry information channel.

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

Development

Wechat

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

12
Report