Linux FIFO Learning

Source: Internet
Author: User

FIFO, also known as named pipe, is a method used for inter-process communication in Linux.
The difference between FIFO and pipe is:
FIFO has corresponding inode in the file system, which can be viewed using the ls command.
Sh-3.2 # ls-lhF./export o_file
100 prwxrwxrwx 1 root 0 Jan 1 1970./export o_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 has no name and cannot be viewed in the existing file system.
It can only be used for communications between parent-child processes, sibling processes, and other kinship-related processes.

The method for creating a FIFO is as follows:
1. Call the umask system call to set the file creation permission,
# Include <sys/types. h>
# Include <sys/stat/h>

Mode_t umask (mode_t mask );

2. Call the unlink system call to first Delete the existing fifo,
# Include <unistd. h>

Int unlink (const char * pathname );

3. Call the mkfifo database function to create a FIFO file,
# Include <sys/types. h>
# Include <sys/stat. h>

Int mkfifo (const char * pathname, mode_t mode );

You can also create a FIFO file by calling the mknod system and specifying the mode parameter 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. The data transmitted each time must be limited to PIPE_BUF;
2. For FIFO access, just like accessing a regular file (regular file), you can use open/read/write/close and other system calls to access the file.

Applications that use FIFO include:
1. Simple producer/consumer issues: one process reads data and one process writes data;
2. Implement the client/server architecture program. The client and server communicate with each other through FIFO.
There may be many other application scenarios, and the specific problem will be analyzed later.

There may be many details about FIFO. Let's take a look!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.