Interprocess communication II (Named pipes)

Source: Internet
Author: User

In the previous article, we saw how anonymous pipelines can be used to pass data between processes, a flaw in the way that these processes must be initiated by a common ancestor process, which can cause inconvenience in exchanging data between unrelated processes. Another way of communication-named pipes-can solve the communication problem between unrelated processes.

1. What is a named pipe?
A named pipe is also known as a FIFO file, which is a special type of file that exists as a file name in the filesystem, but behaves like a previously unnamed pipe (anonymous pipe).

Since everything in Linux can be treated as a file, the use of named Pipes has become very consistent with file operations, makes it easy to use, and can be used in commands like normal file names.

2. How do I create a named pipe?
#include <sys/types.h>
#include <sys/stat.h>
int Mkfifo (const char *filename, mode_t mode);

Returns 0 if successful, otherwise returns 1, the reason for the error is stored in errno.
This function creates a FIFO file, which is to create a file that is actually present in the file system, filename Specifies the file name, and mode specifies the read and write permissions for the files.

3. Accessing Named Pipes

3.1 Opening a FIFO file
Like opening other files, FIFO files can also be opened using the open switch. Note that the MKFIFO function simply creates a FIFO file to use a named pipe or open it.

But there are two points to note, 1, is that the program can not open the FIFO file in O_rdwr mode for read and write operations, and its behavior is not clearly defined, because if a pipe is opened in read/write mode, the process will read back to its own output, and we usually use FIFO only for one-way data transfer. 2, is passed to the open call is the FIFO path name, but not the normal file.

There are usually four ways to open a FIFO file,
[CPP] View plain copy print?
Open (const char *path, o_rdonly);//1
Open (const char *path, O_RDONLY | O_nonblock);//2
Open (const char *path, o_wronly);//3
Open (const char *path, O_WRONLY | O_nonblock);//4
In the second argument of the Open function call, you see an unfamiliar option O_nonblock, the option O_nonblock represents non-blocking, plus this option indicates that the open call is non-blocking, and if this option is not, the open call is blocked.

What is the blocking of the open call? Very simply, 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. If an open call is non-blocking (that is, the second argument is O_rdonly | O_nonblock), the open call succeeds and returns immediately, even if no other process opens the same FIFO file 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 If the open call is non-blocking (that is, the second argument is O_wronly | O_nonblock), open always returns immediately, but if no other process opens the same FIFO file in read-only mode, the open call returns 1, and the FIFO is not opened.

4. Example

Fifo_read.c

Include<sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FIFO "/tmp/myfifo"
Main (int argc,char** argv)
{
Char buf_r[100];
int FD;
int nread;
if (Mkfifo (fifo,o_creat| O_EXCL) <0) && (errno!=eexist))
printf ("Cannot create fifoserver\n");
printf ("Preparing for Reading bytes....\n");
memset (buf_r,0,sizeof (buf_r));
Fd=open (fifo,o_rdonly| o_nonblock,0);
if (fd==-1)
{
Perror ("open");
Exit (1);
}
while (1)

{
memset (buf_r,0,sizeof (buf_r));
if ((Nread=read (fd,buf_r,100)) ==-1)

{
if (Errno==eagain)
printf ("No Data yet\n");
}
printf ("Read%s from fifo\n", buf_r);
Sleep (1);
}
Pause ();
Unlink (FIFO);
}

Fifo_write.c

#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define Fifo_server "/tmp/myfifo"
Main (int argc,char** argv)
{
int FD;
int nwrite;
Char w_buf[100];
for (;;)
{
memset (&w_buf, 0, sizeof (W_BUF));
Fd=open (fifo_server,o_wronly| o_nonblock,0);
printf ("Please input your data:\n");
scanf ("%s", w_buf);
if ((Nwrite=write (fd,w_buf,100)) ==-1)
{
if (Errno==eagain)
printf ("The FIFO has not been read yet. Please try later\n ");
}
Else
printf ("Write%s to the fifo\n", w_buf);
}
}

4.1 Compiling the program

[[email protected] FIFO] $ls
FIFO_READ.C fifo_write.c
[[email protected] FIFO] $GCC fifo_read.c-o Read
[[email protected] FIFO] $GCC fifo_write.c-o Write
[[email protected] FIFO] $ls
FIFO_READ.C fifo_write.c Read Write
[Email protected] fifo]$

Operation of the program

Run read in the current console 1, run write in Console2

[[email protected] FIFO] $sudo./read
Preparing for reading bytes ....
Read from FIFO
Read from FIFO
No data yet
Read from FIFO
No data yet
Read from FIFO
No data yet
Read from FIFO
No data yet
Read from FIFO
No data yet
Read from FIFO
No data yet
Read from FIFO
No data yet
Read from FIFO
No data yet
Read from FIFO
No data yet
Read from FIFO
Read Hello from FIFO
Read FIFO from FIFO
No data yet
Read from FIFO
No data yet
Read from FIFO
No data yet
Read from FIFO
No data yet
Read from FIFO
^c[[email protected] fifo]$

Console2

[Email protected] Fifo]$./write
Please input your data:
Hello fifo!
Write Hello to the FIFO
Please input your data:
Write fifo! to the FIFO
Please input your data:
^c
[Email protected] Fifo]$./write
Please input your data:
Hello FIFO
Write Hello to the FIFO
Please input your data:
Write FIFO to the FIFO
Please input your data:
^c
[Email protected] fifo]$

Interprocess communication II (Named pipes)

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.