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-relationship interprocess communication (many other details. See the Nameless pipe). in order to overcome this shortcoming. A named pipe (FIFO) is proposed. Also known as pipeline, FIFO file.


A named pipe (FIFO) differs from an unnamed pipe in that it provides a pathname associated with it, which exists in the file system in the form of a FIFO file . Even if there are no affinity processes with the FIFO creation process, you can simply access the path. The line communicates with each other through FIFO, 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 the same, not the same place :

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

2. When the process using FIFO exits. The FIFO file will continue to be saved in the file system for later use.


3. FIFO has a name. 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);

Function:

Named Pipes are created.

Number of references:

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

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

return value:

Success: 0
Failure: Assume that the file already exists. Error and return-1.


The demo 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 execution are as follows:



default actions for Named Pipes

Later operations. Operate the named pipe as a normal file: open (), write (), read (), close ().

but . As with nameless pipes, the operation of named Pipes is sure to consider its clogging characteristics by default .


The following verifies the characteristics of the default case. That is, open () does not specify a non-clogging flag (o_nonblock).

1)

Open () when opening FIFO in a read-only manner, to block a process from opening this FIFO for write
Open () When the FIFO is opened in a write-only manner, it is blocked to a process that opens this FIFO for reading.

In a simple sentence , just read and wait just to write, just write and just read, only two of them are running until they run down.


Read-only code such as the following:

#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;}

Write-only code such as the following

#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 just read if (FD < 0) {perror ("open FIFO");} printf ("After open\n"); return 0;}

We open two terminals. Compile the above code separately, read-end program and write-end program each execution, for example, we verify its characteristics, because the light look at the results of the graph is not effective, we need to analyze how its implementation process changes.



Let's say you don't want to plug in open (). We are able to open FIFO files in a readable and writable manner. This way the open () function does not clog.

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

2) If there is no data in the FIFO, the Read () function reads the data from the FIFO while reading () is blocked. This feature is the same as the nameless pipe.


Write-side code such as the following:

#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 just read 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 () 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;}

read-only code such as the following:

#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, blocked when the named pipe has no data. Take out the data when read (FD, recv, sizeof (recv)); printf ("read from My_fifo buf=[%s]\n", recv); return 0;}

perform the 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 to the read () function reads the data from the FIFO without clogging, and if the write process is executed again, the read () function is called from the FIFO to resume blocking.


Write-side code such as the following:

#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 just read 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); Blocked to ensure that the read and write process keeps the communication process return 0;}

read-only code such as the following:

#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 for just write 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;}

Perform the 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 and are no longer verified here. For details, see the Nameless pipe.


named pipe non-clogging flag operation

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

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


The non-clogging flag (O_nonblock) opens a named pipe with the following features:
1. Open with read-only mode first. Suppose no process has opened a FIFO for writing, only read open () succeeds. and open () does not clog.


2, the first to open the only way to write. Suppose no process has opened a FIFO for reading. Just write open () to return an error-1.


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



Use the following code to compile your own validation.


Write-side code such as the following:

#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");} Simply write and specify non-clogging mode to open FD = Opening ("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;}

read-only code such as the following:

#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-clogging mode to open FD = Opening ("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;}

This tutorial demonstrates the sample code download 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.