Linux system programming-inter-process communication: Named Pipe (FIFO), linuxfifo

Source: Internet
Author: User

Linux system programming-inter-process communication: Named Pipe (FIFO), linuxfifo
Overview of named MPs queue

Unknown pipeline, because there is no name, can only be used for inter-process communication of kinship (for more details, see "unknown Pipeline"). To overcome this shortcoming, a named pipe (FIFO) is proposed, which is also called famous pipe and FIFO file.


A named pipe (FIFO) is different from an unnamed pipe in that it provides a path name associated with it,In the file system as a FIFO fileIn this way, even if the process does not have a kinship with the FIFO creation process, as long as the path can be accessed, the process can communicate with each other through FIFO,Therefore, data can be exchanged through FIFO unrelated processes..


The named pipe (FIFO) has the same characteristics as the named pipe (pipe). The differences are as follows:

1. FIFO exists as a special file in the file system, but the content in the FIFO is stored in the memory.

2. After a FIFO process exits, the FIFO file will be stored in the file system for future use.

3. FIFO has a name. Unrelated processes can communicate by opening a named pipeline.


Create a named pipe

Required header file:

# Include <sys/types. h>

# Include <sys/stat. h>


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

Function:

Create a named pipe.

Parameters:

Pathname: common path name, that is, the name of the first-in-first-out after creation.

Mode: File Permission, which is the same as the mode parameter in the open () function for opening a common file. Click this link for instructions.

Return Value:

Success: 0
Failed: if the file already exists, an error occurs and-1 is returned.


The sample code is as follows:

# Include <stdio. h> # include <sys/types. h> # include <sys/stat. h> int main (int argc, char * argv []) {int ret; ret = mkfifo ("my_fifo", 0666); // create a named pipeline if (ret! = 0) {// error perror ("mkfifo");} return 0 ;}

The running result is as follows:



Default operations for named MPs queues

In later operations, the named pipe is operated like a common file: open (), write (), read (), close (). However, like an unnamed pipe, the blocking feature of a named pipe must be considered by default.


The following describes the default features, that is, the non-blocking mark (O_NONBLOCK) is not specified during open ).

1)

When open () is enabled in read-only mode, it is necessary to block a process to open the FIFO for writing.
When open () is enabled in write-only mode, it is blocked to a process to open the FIFO for read.

In a simple sentence, read-only requests can be read-only requests. Write requests can only be read-only requests. Only two requests can be read-only requests.


The read-only code is as follows:

# Include <stdio. h> # include <string. h> # include <unistd. h> # include <sys/types. h> # include <sys/stat. h> # include <fcntl. h> int main (int argc, char * argv []) {int fd; int ret; ret = mkfifo ("my_fifo", 0666); if (ret! = 0) {perror ("mkfifo");} printf ("before open \ n"); fd = open ("my_fifo", O_RDONLY ); // wait until only if (fd <0) {perror ("open fifo");} printf ("after open \ n"); return 0 ;}

The write-only code is as follows:

# Include <stdio. h> # include <string. h> # include <unistd. h> # include <sys/types. h> # include <sys/stat. h> # include <fcntl. h> int main (int argc, char * argv []) {int fd; int ret; ret = mkfifo ("my_fifo", 0666); if (ret! = 0) {perror ("mkfifo");} printf ("before open \ n"); fd = open ("my_fifo", O_WRONLY ); // wait for the read-only if (fd <0) {perror ("open fifo");} printf ("after open \ n"); return 0 ;}

You can open two terminals, compile the above Code, and run both the read and write programs. For example, you can verify the features on your own because the result graph is ineffective, you need to analyze how the running process changes.



If you do not want to block open (), you can open the FIFO file in a readable and writable manner, so that the open () function will not block.

// Enable int fd = open ("my_fifo", O_RDWR) in readable and writable mode );

2) if there is no data in the FIFO, the read () will also be blocked when the read () function is called to read data from the FIFO. This feature is the same as the nameless pipeline.


The write code is as follows:

# Include <stdio. h> # include <string. h> # include <unistd. h> # include <sys/types. h> # include <sys/stat. h> # include <fcntl. h> int main (int argc, char * argv []) {int fd; int ret; ret = mkfifo ("my_fifo", 0666 ); // create a named pipe if (ret! = 0) {perror ("mkfifo");} printf ("before open \ n"); fd = open ("my_fifo", O_WRONLY ); // wait for the read-only if (fd <0) {perror ("open fifo");} printf ("after open \ n"); printf ("before write \ n "); // write data to the named pipeline after 5s. Before there is no data, read () blocks sleep (5); char send [100] = "Hello Mike "; write (fd, send, strlen (send); printf ("write to my_fifo buf = % s \ n", send); return 0 ;}

The read code is as follows:

# Include <stdio. h> # include <string. h> # include <unistd. h> # include <sys/types. h> # include <sys/stat. h> # include <fcntl. h> int main (int argc, char * argv []) {int fd; int ret; ret = mkfifo ("my_fifo", 0666 ); // create a named pipe if (ret! = 0) {perror ("mkfifo");} printf ("before open \ n"); fd = open ("my_fifo", O_RDONLY ); // wait until only if (fd <0) {perror ("open fifo");} printf ("after open \ n "); printf ("before read \ n"); char recv [100] = {0}; // read data. When the named pipe has no data, it will be blocked, read (fd, recv, sizeof (recv); printf ("read from my_fifo buf = [% s] \ n", recv ); return 0 ;}

Verify the execution according to your own Compilation:



3) if the write process exits first in the communication process, even if there is no data in the named pipeline, the read () function is called to read data from the FIFO; if the write process re-runs, the block is restored when the read () function is called to read data from the FIFO.


The write code is as follows:

# Include <stdio. h> # include <string. h> # include <unistd. h> # include <sys/types. h> # include <sys/stat. h> # include <fcntl. h> int main (int argc, char * argv []) {int fd; int ret; ret = mkfifo ("my_fifo", 0666 ); // create a named pipe if (ret! = 0) {perror ("mkfifo");} fd = open ("my_fifo", O_WRONLY); // wait for read-only if (fd <0) {perror ("open fifo");} char send [100] = "Hello Mike"; write (fd, send, strlen (send )); // write data printf ("write to my_fifo buf = % s \ n", send); while (1); // blocking, ensure that the read/write Process maintains the communication process return 0 ;}

The read code is as follows:

# Include <stdio. h> # include <string. h> # include <unistd. h> # include <sys/types. h> # include <sys/stat. h> # include <fcntl. h> int main (int argc, char * argv []) {int fd; int ret; ret = mkfifo ("my_fifo", 0666 ); // create a named pipe if (ret! = 0) {perror ("mkfifo");} fd = open ("my_fifo", O_RDONLY); // wait for only if (fd <0) {perror ("open fifo");} while (1) {char recv [100] = {0}; read (fd, recv, sizeof (recv )); // read data printf ("read from my_fifo buf = [% s] \ n", recv); sleep (1) ;}return 0 ;}

Verify the execution according to your own Compilation:



5) during communication, when the read process exits and the write process writes data to the named pipeline, the write process also exits (receiving the SIGPIPE signal.


6) Call the write () function to write data to the FIFO. When the buffer zone is full, write () will also be blocked.


5) and 6) These two features are the same as those of the unknown pipeline. We will not verify them here. For details, see "unknown Pipeline".


Operation of the non-blocking mark of the named MPs queue

The named pipe can be opened with a non-blocking sign (O_NONBLOCK:

fd = open("my_fifo", O_WRONLY|O_NONBLOCK);fd = open("my_fifo", O_RDONLY|O_NONBLOCK);


The named pipe opened by the non-blocking sign (O_NONBLOCK) has the following features:
1. open in read-only mode first. If no process has opened a FIFO for writing, read-only open () is successful, and open () is not blocked.


2. open in write-only mode first. If no process has opened a FIFO for read, write-only open () will return-1 error.


3. read () and write () are not blocked when reading data in the named pipe.


Compile and run the verification code according to the following code.


The write code is as follows:

# Include <stdio. h> # include <string. h> # include <unistd. h> # include <sys/types. h> # include <sys/stat. h> # include <fcntl. h> int main (int argc, char * argv []) {int fd; int ret; ret = mkfifo ("my_fifo", 0666 ); // create a named pipe if (ret! = 0) {perror ("mkfifo");} // write only and specify the non-blocking mode to open fd = open ("my_fifo", O_WRONLY | O_NONBLOCK ); if (fd <0) {perror ("open fifo");} char send [100] = "Hello Mike"; write (fd, send, strlen (send )); printf ("write to my_fifo buf = % s \ n", send); while (1); return 0 ;}

The read code is as follows:

# Include <stdio. h> # include <string. h> # include <unistd. h> # include <sys/types. h> # include <sys/stat. h> # include <fcntl. h> int main (int argc, char * argv []) {int fd; int ret; ret = mkfifo ("my_fifo", 0666 ); // create a named pipe if (ret! = 0) {perror ("mkfifo");} // read-only and specify non-blocking mode to open fd = open ("my_fifo", O_RDONLY | O_NONBLOCK); if (fd <0) {perror ("open fifo");} while (1) {char recv [100] = {0}; read (fd, recv, sizeof (recv )); printf ("read from my_fifo buf = [% s] \ n", recv); sleep (1) ;}return 0 ;}

For sample code downloading in this tutorial, click here.

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.