Linux system Programming-interprocess communication: Named Pipes (FIFO)

Source: Internet
Author: User

Overview of Named Pipes

Nameless pipes, because there is no name, can only be used for inter-process communication of affinity (for more details, see the Nameless pipe). to overcome this shortcoming, a named pipe (FIFO), also known as a pipeline, FIFO file, is proposed.


A named pipe (FIFO) differs from an unnamed pipe in that it provides a pathname associated with it that exists in the file system as a FIFO file , so that, even if the process is not related to the FIFO's creation process, it can be accessed through a FIFO if it has access to the path Communicate with each other, so that processes that are not related to FIFO can also exchange data .


Named Pipes (FIFO) and nameless pipes (pipe) have some characteristics that are identical and differ in the following areas :

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

2. When the FIFO process exits, the FIFO file continues to be saved in the file system for later use.

3. FIFO has a name, and unrelated processes can communicate by opening a named pipe.


Named Pipes are created

Required header file :

#include <sys/types.h>

#include <sys/stat.h>


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

features :

Named Pipes are created.

Parameters :

pathname: The generic pathname, which is the name of the FIFO after creation.

mode: The permissions of the file are the same as the mode parameter in the open () function that opens the normal file, please click this link for instructions.

return value :

Success: 0
Failure: If the file already exists, an error occurs and returns-1.


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 pipe if (ret! = 0) {//Error perror ("Mkfifo");} return 0;}

The results of the operation are as follows:



default actions for Named Pipes

Later operations, the named pipe as a normal file operation: Open (), write (), read (), close (). however , as with nameless pipes, the operation named Pipes must consider their blocking characteristics by default .


The following verification is the default feature, that is, open () does not specify a non-blocking flag (O_nonblock).

1)

Open () when opening FIFO as read-only, to block to a process that opens this FIFO for write
Open () when opening FIFO in write-only mode, to block to a process that opens this FIFO for read.

A simple sentence , read-only waiting to write only, only write to read only, only two are executed, will be executed down.


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 to write 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 Read only if (FD < 0) {perror ("open FIFO");} printf ("After open\n"); return 0;}

We open two terminals, respectively compiled the above code, read-end program and write-end program each run, such as, we self-verify its characteristics, because the light look at the results of the graph is no effect, we need to analyze how its operation process changes.



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

Open int fd = open ("My_fifo", O_RDWR) in readable writable mode;

2) If there is no data in the FIFO, the Read () function will block when it reads the data from the FIFO. This feature is the same as the nameless pipe.


The write-side 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 Read only if (FD < 0) {perror ("open FIFO");} printf ("After open\n");p rintf ("before write\n");//5s to the named pipe before writing data, no data, read the end reading () block sleep (5); char send[100] = "Hello Mike" Write (fd, send, strlen (send));p rintf ("Write to My_fifo buf=%s\n", send); return 0;}

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);//Create Named pipe if (ret! = 0) {perror ("Mkfifo");} printf ("Before open\n"); fd = open ("My_fifo", o_rdonly);//wait to write only if (FD < 0) {perror ("open FIFO");} printf ("After open\n");p rintf ("before read\n"), char recv[100] = {0};//read data, the named pipe will block when no data is available, take it out when there is data read (FD, recv, sizeof ( recv)); printf ("read from My_fifo buf=[%s]\n", recv); return 0;}

Run validation According to your own compilation:



3) in the communication process , if the write process first exits, even if there is no data in the named pipe, the call read () function reads the data from the FIFO without blocking; If the write process is rerun, the call to read () function resumes blocking when it reads the data from the FIFO.


The write-side 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 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 to ensure that the read and write process keeps the communication process return 0;}

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);//Create Named pipe if (ret! = 0) {perror ("Mkfifo");} FD = open ("My_fifo", o_rdonly);//wait to write 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;}

Run validation According to your own compilation:



5) During the communication process, when the read process exits, the write process writes the data to the named pipe, and the writer processes (receives the sigpipe signal) exits.


6) Call the Write () function to write data to the FIFO, and write () will block when the buffer is full.


5) and 6) These two features are the same as the nameless pipes, which are no longer verified here, see the Nameless pipe for details.


named pipe non-blocking flag operation

Named pipes can be opened as non-blocking flags (O_nonblock):

FD = open ("My_fifo", o_wronly| O_nonblock); fd = open ("My_fifo", o_rdonly| O_nonblock);


The non-blocking flags (o_nonblock) Open Named pipes have the following characteristics:
1. Open as read-only, if no process has opened a FIFO for writing, read-only open () succeeds, and open () does not block.


2, first opened as write-only, if no process has opened a FIFO for reading, write only open () will error return-1.


3. Read (), write () reads the data in the named pipe without blocking.


Compile your own run validation according to the following code.


The write-side 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 Named pipe if (ret! = 0) {perror ("Mkfifo");} Write only and specify non-blocking mode to open FD = opened ("My_fifo", o_wronly| O_nonblock); if (fd<0) {perror ("open FIFO");} Char send[100] = "Hello Mike"; write (fd, send, strlen (send));p rintf ("Write to My_fifo buf=%s\n", send), and while (1); return 0;}

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);//Create Named pipe if (ret! = 0) {perror ("Mkfifo");} Read-only and specify non-blocking mode for opening 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));p rintf ("read from My_fifo buf=[%s]\n", recv); sleep (1);} return 0;}

For this tutorial sample code download please click here.

Linux system Programming-interprocess communication: Named Pipes (FIFO)

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.