Linux System programming Pipeline (III) named pipe FIFO and MKFIFO functions

Source: Internet
Author: User
Tags anonymous file size file system socket

Interprocess communication must be provided through the kernel channel, and there must be a way to identify a channel provided by the kernel in the process, which is identified by an open file descriptor. If several processes to communicate with each other do not inherit file descriptors from a common ancestor, how do they communicate? The kernel provides a channel that is not a problem, and the question is how do you identify this channel so that all processes can access it? The path names in the file system are global and can be accessed by each process, so you can identify an IPC channel using the pathname in the file system.

FIFO and UNIX Domain sockets both of these IPC mechanisms are identified using special files in the file system.

The FIFO file has no data block on disk and is used only to identify a channel in the kernel, such as prw-rw-r--1 Simba Simba 0 may 10:13 P2, the file type is labeled P as FIFO and the file size is 0. Processes can open this file for Read/write, actually reading and writing to the kernel channel (the root cause is that the read, write, and regular files pointed to by this file structure are different), which enables interprocess communication. UNIX Domain sockets and FIFO principles are similar and require a special socket file to identify the channels in the kernel, such as the socket file with many system services in the/run directory:

Srw-rw-rw-1 root root 0 may 09:59 Acpid.socket

....................

The file type S represents the socket, and the files do not have data blocks on the disk.

First, named pipe (FIFO)

One limitation of anonymous pipe applications is that they can only communicate between processes that have a common ancestor (a kinship).

If we want to exchange data between unrelated processes, we can do this by using a FIFO file, which is often referred to as a named pipe.

Named pipes can be created from the command line using the following command:

$ mkfifo filename

Named pipes can also be created from a program, and the related functions are:

int Mkfifo (const char *filename,mode_t mode);

Second, named Pipes and anonymous pipes

Anonymous pipes are created and opened by the pipe function.

Named pipes are created by the Mkfifo function, opened with open.

The only difference between a FIFO (named pipe) and a pipe (anonymous pipe) is that they are created and opened differently, and they have the same semantics after they are completed.

The only difference between pipes and FIFOs are the manner in which, they are and created. Once These tasks have been accomplished, I/O on pipes and FIFOs has exactly the same.

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.