IPC FIFO (famous pipe) detailed

Source: Internet
Author: User


Basic Concepts:

Pipelines have no names, so their greatest disadvantage is that they can only be used between processes that have a common ancestor process. We cannot create a pipe between unrelated processes and use it as an IPC pipe (regardless of descriptor delivery).

FIFO refers to first In,first, which is a one-way (Half-duplex) data stream. Unlike pipelines, each FIFO has a path name associated with it, allowing unrelated processes to access the same FIFO. FIFO is also known as a known pipe (named pipe).


function Description:

FIFO created by Mkfifo function

Mkfifo (3)                  Linux Programmer ' s Manual                 Mkfifo (3)

NAME
       mkfifo-make a FIFO special file (a named pipe)

s Ynopsis
       #include <sys/types.h>
       #include <sys/stat.h>

       int mkfifo (const char *pathname, mode_t mode);
Return: If the success is 0, if the error is-1


Where pathname is an ordinary pathname, it is the name of the FIFO.

The mode parameter specifies a file permission bit, similar to the third argument of open.

The MKFIFO function has implicitly specified O_creat | O_excl. That is, it either creates a new FIFO or returns a eexist error (if the specified name of the FIFO already exists). To open an existing FIFO or create a new FIFO, call Mkfifo First, and then check whether it returns a eexist error, or call open if you return the error.

After creating a FIFO, it must be opened to read, or open to write, using either the Open function or a standard I/O open function, such as open. The FIFO cannot be opened to read and write because it is half-duplex.


If Lseek is invoked on the pipe or FIFO, the Espipe error is returned.


Note: If no process is currently writing to open a FIFO, then the process of reading to open the FIFO will block.

Similarly, if no process is currently read to open a FIFO, the process that writes the FIFO Open will block.


Example:

Starting two different processes and communicating over two channels requires that two pipes are read and write in two processes.

The open file descriptor here is blocked mode by default.

gcc fifo_server.c-o Server

#include <stdio.h> #include <errno.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <fcntl.h> #include <string.h> #include <stdlib.h> #define Maxline 1024 #defi
    Ne FIFO1 "/tmp/fifo.1" #define FIFO2 "/tmp/fifo.2" void perror (const char *s) {perror (s);
Exit (Exit_failure);
    } void Server (int readfd, int writefd) {/* send MSG */int i = 0;
        for (i; i<3; i++) {char Buff[maxline] = {0};
        sprintf (Buff, "Hello World%d", i);
        int n = write (WRITEFD, buff, strlen (buff));
    Sleep (1);
    } char Buff[maxline] = {0};
    int n = read (READFD, buff, maxline);
    if (n > 0) {printf ("read from client:%s\n", buff);

    int main () {int READFD, WRITEFD; /* Create two FIFO; OK if they already exist */if (Mkfifo (FIFO1, 0777) < 0) && (errno!= eexist)) perror ("can" t creat
    e FIFO1 "); if ((Mkfifo (FIFO2, 0777) < 0) && (errno!= eexist)) {unlink (FIFO1);/* RM FIFO1/perror ("Can ' t create FIFO2");

    printf ("Create FIFO success\n");
    /* To pay attention to the open order * * READFD = open (FIFO2, o_rdonly, 0);
    WRITEFD = open (FIFO1, o_wronly, 0);
    
    printf ("Open FIFO success\n");
    /* Let FIFO automatically delete/unlink (FIFO1) after the process is finished;

    Unlink (FIFO2);

    Server (READFD, WRITEFD);
return 0; }

gcc fifo_client.c-o Client

#include <stdio.h> #include <errno.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <fcntl.h> #include <string.h> #include <stdlib.h> #define Maxline 1024 #defi
    Ne FIFO1 "/tmp/fifo.1" #define FIFO2 "/tmp/fifo.2" void perror (const char *s) {perror (s);
Exit (Exit_failure);
    } void Client (int readfd, int writefd) {/* read MSG */int i = 0;
        for (i; i<3; i++) {char Buff[maxline] = {0};
        int n = read (READFD, buff, maxline);
        if (n > 0) {printf ("read from server:%s\n", buff);
    } char *buff = "Goodby server";
Write (WRITEFD, buff, strlen (buff));

    int main () {int READFD, WRITEFD; /* Create two FIFO; OK if they already exist */if (Mkfifo (FIFO1, 0777) < 0) && (errno!= eexist)) perror ("can" t creat
    e FIFO1 "); if ((Mkfifo (FIFO2, 0777) < 0) && (errno!= eexist)) {unlink (FIFO1);/* RM FIFO1 * * perror ("can ' t create FIFO2");
    /* To pay attention to the order of open/writefd = open (FIFO2, o_wronly);

    READFD = open (FIFO1, o_rdonly);

    Client (READFD, WRITEFD);
return 0;
 }

Run Result:



About the size of the FIFO:
After testing, the FIFO size under unubtu 14 is 64KB, consistent with the size of the pipe.



If the requested data is written with a byte number less than or equal to Pipe_buf, then the write operation guarantees the atomic. This means that if two processes are written almost simultaneously to the same pipe or FIFO, then either write all the data from the first process, write all the data from the second process, or reverse it. The system does not mix data from the two processes. However, the write operation is not guaranteed to be atomic if the requested data is written with a byte number greater than PIPE_BUF.

The size of the PIPE_BUF can be viewed through the getconf command



about data persistence: when the last shutdown of a pipe or FIFO occurs, data that is still on the pipeline or FIFO is discarded.


more on : IPC pipes and extra attributes for FIFO


Reference: "UNIX Network Programming" • Volume 2


End;

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.