Anonymous pipe pipes have been said before, the next is named pipe FIFO;
We can use one of the following functions to create a named pipe whose prototype is as follows:
#include <sys/types.h>
#include <sys/stat.h>
int Mkfifo (const char *filename, mode_t mode);
int Mknod (const char *filename, mode_t mode | S_ififo, (dev_t) 0);Both of these functions can create aFifo,Note is to create a file that is actually present in the file system,FileName specify filename, modemknodmkfifo try to use mkfifo and not mknod
< Span style= "Font-family:baskerville;font-size:12pt;color:rgb (50,51,50);" > mkfifofifo function, named pipe. The pipes in front of them all have no names, so they are called anonymous pipes, or pipes for short. For file systems, Tao is a visible file, so it can be used for Any two processes is the parent-child process, regardless of whether the two processes are related.
server . C Read
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
int main ()
{
if (Mkfifo ("./fifo", s_ififo|0666) < 0)
{
Perror ("Mkfifo");
return 1;
}
int Fd=open ("./fifo", o_rdonly);
if (fd<0)
{
Perror ("open");
return 2;
}
Char buf[1024];
while (1)
{
memset (buf, ' n ', sizeof (BUF));
Read (fd,buf,sizeof (BUF)-1);
printf ("client#%s", buf);
}
Close (FD);
return 0;
}
client . C Write
The code is as follows:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
int main ()
{
int Fd=open ("./fifo", o_wronly);
if (fd<0)
{
Perror ("open");
return 2;
}
Char buf[1024];
while (1)
{
printf ("Please enter#");
Fflush (stdout);
ssize_t Sz=read (0,buf,sizeof (BUF)-1);
if (sz>0)
{
buf[sz]= ' + ';
}
Write (Fd,buf,strlen (BUF));
}
Close (FD);
return 0;
}
/span>
This article is from the "Stream Wind" blog, please be sure to keep this source http://xiexiankun.blog.51cto.com/10785425/1835133
Named pipe FIFO in Linux