Linux IPC Practice (3)--named FIFO

Source: Internet
Author: User

FIFO named/Named Pipes

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.

Create a named pipe

1) Named pipes can be created from the command line:

$ Mkfifo <filename>

2) Named Pipes are created in the program:

       #include <sys/types.h>       #include <sys/stat.h>       int mkfifo (const char *pathname, mode_t mode);
Example int main () {    if (Mkfifo ("P2", 0644) = =-1)        err_exit ("Mkfifo error");}

The difference between FIFO and pipe:

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 FIFO (named pipe) and pipe (anonymous pipe) is that they are created and opened in a different way, but once these work is completed, they have the same semantics (theonly difference between pipes and FIFOs is the manner in which they is created and opened.

Once These tasks have been accomplished, I/O on pipes and FIFOs had exactly the same semantics. ).

Open rules for Named pipes

1) Read Open FIFO

O_nonblock Disable (blocked): block until a corresponding process has opened the FIFO for write

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

Example 1: Blocking, read-only open int main () {    int fd = open ("FIFO", o_rdonly);    if (fd = =-1)        err_exit ("FIFO open Error");    cout << "FIFO o_rdonly Open Success" << Endl;}
Example 2: Read-only, non-blocking open int main () {    int fd = open ("FIFO", o_rdonly| O_nonblock);    if (fd = =-1)        err_exit ("FIFO open Error");    cout << "FIFO o_rdonly Open Success" << Endl;}

2) write Open FIFO

O_nonblock Disable (blocking): blocks until the corresponding process has opened the FIFO for reading

O_nonblock Enable (non-blocking): return failure immediately, error code Enxio

Example 1: Blocking, write-only open int main () {    int fd = open ("FIFO", o_wronly);    if (fd = =-1)        err_exit ("FIFO open Error");    cout << "FIFO o_wronly Open Success" << Endl;}
Example 2: Non-blocking, write-only open int main () {    int fd = open ("FIFO", o_wronly| O_nonblock);    if (fd = =-1)        err_exit ("FIFO open Error");    cout << "FIFO o_wronly Open Success" << Endl;}

Read and write rules for named pipes

With Anonymous pipelines



2:readfifoint Main (int argc, char *argv[]) {    if (argc < 2)        Err_quit ("Usage:./writefifo <write-file-name > ");    int outfd = open (argv[1], o_wronly| O_creat| O_trunc, 0644);  Create and punch the file    int infd = open ("Myfifo", o_rdonly);    Open FIFO    if (infd = =-1 | | outfd = = 1)        err_exit ("Open File/fifo Error");    Char Buf[bufsiz];    int readbytes;    while ((readbytes = Read (INFD, buf, sizeof (BUF))) > 0)    {        write (outfd, buf, readbytes);    }    Close (OUTFD);    Unlink ("Myfifo");   Delete FIFO}

Linux IPC Practice (3)--named FIFO

Related Article

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.