Named pipe FIFO in Linux

Source: Internet
Author: User
Tags anonymous

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

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.