Linux pipelines (named FIFO)

Source: Internet
Author: User

Overview

One limitation of (anonymous) pipeline applications is that they can only communicate between processes that have a common ancestor (affinity).

If we want to exchange data between unrelated processes , you can do this with a FIFO file, which is often referred to as a named pipe; a named pipe is a special type of file.

Piping Applications

1) Create a named pipe

Named pipes can be created from the command line:

$ Mkfifo <filename>

Named Pipes are created in the program:

       #include <sys/types.h>       #include <sys/stat.h>        int mkfifo (const char *pathname, mode_t mode);


anonymous pipes VS. Named Pipes

1) anonymous pipes are created and opened by the pipe function.

Named pipes are created by the Mkfifo function and opened with open

2) The only difference between a FIFO (named pipe) and a pipe (anonymous pipe) is that they are created and opened in a different way, but they have the same semantics after the work is done.

Open rules for Named pipes

1) Read Open FIFO

O_nonblock Disable(blocking): blocks until a corresponding process is written to open the FIFO

O_nonblock Enable(non-blocking): Return to success immediately

2) write Open FIFO

O_nonblock disable: block until a corresponding process opens the FIFO for reading

O_nonblock enable: return failure immediately, error code Enxio

Read and write rules for named pipes

With Anonymous pipelines

FIFO Programming Practice

/** Description: The use of pipelines, two of processes for file replication. Process Writefifo: Read Input.txt file write pipeline FIFO process Readfifo: Read pipeline FIFO write Output.txt file */

1:writefifo//write FIFO, read from Fileint main () {    //create FIFO    if (Mkfifo ("./myfifo", 0644) = =-1)    {        err _exit ("Mkfifo error");    }    Open pipe    int fifofd = open ("./myfifo", o_wronly);    if (FIFOFD = =-1)    {        err_exit ("Open FIFO error");    }    Open file    int filefd = open ("./input.txt", o_rdonly);    if (FILEFD = =-1)    {        err_exit ("Open file Error");    }    const int bufsiz = 1024x768;    Char Buf[bufsiz];    int readcount = 0;    while ((Readcount = Read (filefd,buf,bufsiz)) > 0)    {        write (fifofd,buf,readcount);    }    Close (FIFOFD);    Close (FILEFD);    cout << "Write FIFO ok ..." << Endl;    return 0;}

2:readfifo//read FIFO, write to Fileint main () {    //open FIFO    int fifofd = open ("./myfifo", o_rdonly);    if (FIFOFD = =-1)    {        err_exit ("Open FIFO error");    }    Open file    int filefd = open ("./output.txt", o_wronly| o_creat,0644);    if (FILEFD = =-1)    {        err_exit ("Open file Error");    }    Char Buf[bufsiz];    int readcount = 0;    while ((Readcount = Read (fifofd,buf,bufsiz)) > 0)    {        write (filefd,buf,readcount);    }    Close (FIFOFD);    Close (FILEFD);    cout << "Read FIFO ok ..." << Endl;    return 0;}

-fifo

Mkfifo (3) Linux Programmer ' s Manual mkfifo (3) NAME Mkfifo-make A FIFO Special file (a named pipe) synopsis #include <sys/types.h> #include <sys/stat.h> int m  Kfifo (const char *pathname, mode_t mode);D escription Mkfifo () makes a FIFO special file with name pathname.  mode specifies the FIFO ' s permissions.       It is modified by the process's umask in the usual way:the permissions of the created file was (mode & ~umask).       A FIFO special file is similar to a pipe, except that it's created in a different the. Instead of being an anonymous communications channel, a FIFO special file was entered into the filesystem by calling Mkfifo       ().  Once you has created a FIFO special file in this to, any process can open it for reading or writing, in the same as  An ordinary file. However, it have to is open at both ends simultaneously before you can proceed to does any input or OUTPUT operations on it.  Opening a FIFO for reading normally blocks until some other process opens the same FIFO for writing, and vice versa. See FIFO (7) for nonblocking handling of FIFO special files.  RETURN VALUE on Success Mkfifo () returns 0. In the case of a error,-1 is returned (in which case, errno is set appropriately).


Linux pipelines (named 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.