Named Pipes (FIFO) in Linux

Source: Internet
Author: User
Tags function prototype

Pipelines can only be used for communication between relationships, whereas in a FIFO, a path is accessible so that communication is possible.

FIFO communicates in the first-in, first-out principle, and the data that is written is first read from the pipeline.

There are two system functions that create a named pipe: Mknod and Mkfifo. Two functions are defined in the header file Sys/stat.h, the function prototype is as follows:

#include <sys/types.h>
#include <sys/stat.h>
int Mknod (const char *path,mode_t mod,dev_t dev);
int Mkfifo (const char *path,mode_t mode);

mknod parameter path the full path name of the named pipe created: mod the schema of the named pipe to be created, indicating its access rights, dev is the device value, depending on the kind of file creation, It is only used when creating a device file. Both function calls successfully return 0, the failure is returned-1. Use mknod function creates a named pipe:

Umask (0);
if (Mknod ("/tmp/fifo", S_ififo | 0666) = =-1)
{
Perror ("Mkfifo error");
Exit (1);
}

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

Umask (0);
if (Mkfifo ("/tmp/fifo", s_ififo|0666) = =-1)
{
Perror ("Mkfifo error!");
Exit (1);
}

"s_ififo|0666" , that is, the creator, the user who is in the same group as the creator, and other users ' access to the named pipe are readable and writable ( This requires attention to the umask of the generated pipe file permissions .

calling open () the process that opens the named pipe may be blocked. However, if you open it in both read and write mode ( o_rdwr), it will not cause blocking, and if you open it as read-only ( o_rdonly), the process that calls the open () function is blocked until a write party opens the pipeline Also open in write mode ( o_wronly) will block until you open the pipeline with Read mode.

For file systems, anonymous pipelines are not visible, and their role is limited to communicating between the parent process and the two processes of a child process. A named pipe is a visible file, so it can be used for communication between any two processes, regardless of whether the two processes are parent-child or not, regardless of whether the two processes have a relationship.

FIFO Read side:

 #include  <stdio.h> #include  <sys/types.h> #include  <sys/stat.h> #include  <unistd.h> #include  <fcntl.h> #include  <string.h > #define  _PATH_  "/tmp/file.tmp" #define  _size_ 100int main () {int fd =  open (_path_, o_rdonly);if  (fd < 0) {printf ("open file error!\n"); return  1;} Char buf[_size_];memset (buf,  ',  sizeof (BUF));while  (1) {Int ret = read (fd ,  buf, sizeof (BUF));if  (ret <= 0)//error or end of file{printf ("read end or error!\n"); break;} printf ("%s\n",  buf);if  (strncmp (buf,  "quit",  4)  == 0) {break;}} Close (FD); return 0;} 

FIFO write side:

#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <string.h> #include <fcntl.h> #define _PATH_ "/tmp/file.tmp" #define _SIZE_ 100int Main () {int ret = Mkfifo (_ Path_, 0666|s_ififo); if (ret = =-1) {printf ("Mkfifo error\n"); return 1;} int fd = open (_path_, o_wronly), if (FD < 0) {printf ("open error\n");} Char Buf[_size_];memset (buf, ' n ', sizeof (BUF)), while (1) {scanf ("%s", buf), int ret = write (FD, buf, strlen (BUF) + 1); if (r ET < 0) {printf ("Write error\n"); if (strncmp (buf, "Quit", 4) = = 0) {break;}} Close (FD); return 0;}


Named Pipes (FIFO) in Linux

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.