A brief description of the naming pipeline FIFO for Linux interprocess communication

Source: Internet
Author: User
Tags function prototype

Talking to the pipe above, you can communicate between the processes that are related.

How do I communicate with unrelated processes? This article will talk about named pipe FIFO.

First, the concept

Named pipe FIFO, which provides a path name associated with it, stored as a file in the file system.

One process opens in R, and the other program opens in W to create a pipeline between two processes.

By using a FIFO file as the medium, you can enable any two processes to communicate through the file.

Named Pipes (FIFO) features are similar to pipes (pipe) and do not need to be mentioned.

Let's look at the FIFO how to communicate inter-process, first of all to introduce the function used:

Second, function prototype

function Mknod Parameters:

Path: The full path name of the named pipe that was created:

MoD: The mode of the named pipe to be created, indicating its access rights;

Dev: is the device value, which depends on the type of file creation, which is only used when creating a device file.

function Mkfifo The meaning of the first two parameters is the same as Mknod.

Both function invocations return 0, and the failure returns-1.

Let me take a look at a set of examples, server and client as an example, server as the receiving end, client as the sender.

Iii. Examples of communication

Server side:

#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h>int main () {    umask (0);   Set Umask to 0    //Create Pipeline file, "s_ififo|0666" indicates that a named pipe is created and access permission is 0666    if (Mkfifo ("./fifo", s_ififo|0666) ==-1) {        Perror ("Mkfifo");        return 1;    }    Open a pipe file as read-only    int fd = open ("./fifo", o_rdonly);    if (fd = =-1) {        perror ("open");        return 2;    }    Char buff[1024] = {0};    int i = 0;    Accept the message    while (1) {        ssize_t s = Read (fd,buff,sizeof (Buff)-1)        ; Buff[s] = 0;        printf ("Server receive#%s\n", buff);    }    Close (FD);    return 0;}

Client side:

#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h>int main () {    int fd = open ("./fifo", o_wronly);    if (fd = =-1) {        perror ("open");        return 2;    }    Char buff[1024] = {0};    int i = 0;    for (i=0; i<5; ++i) {        printf ("Client send#");        Fflush (stdout);        ssize_t s = Read (0,buff,sizeof (Buff)-1);        BUFF[S-1] = 0;        Write (fd,buff,sizeof (buff));    }    Close (FD);    return 0;}

(The code seems to have some minor problems, to be fixed ...)

Iv. Additional Instructions

For FIFO files opened in read-only mode (o_rdonly), if the open call is blocked (that is, the second parameter is o_rdonly), it will not return unless there is a process that opens the same FIFO in write mode;

For FIFO files opened in write-only mode (o_wronly), if the open call is blocked (that is, the second parameter is o_wronly), the open call is blocked until a process opens the same FIFO file as read-only;

A program cannot open a FIFO file for read and write operations in O_RDWR mode, and the consequences of doing so are not clearly defined, and if a pipe is opened in read/write, the process will read back its own output from this pipeline.

V. Comparison of piping (pipe) with Named Pipes (FIFO)

Pipelines (pipes) are used for inter-process communication that is related to one another, while Named Pipes (FIFO) can be used to communicate between any two processes.

A brief description of the naming pipeline FIFO for Linux interprocess communication

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.