Implementation of Named Pipes (FIFO)

Source: Internet
Author: User

Pipeline with process named pipe is a device file that is present on the hard disk file with Mkfifo () to create a named pipe that can be used for communication between any two processes client.c (write end) #include <stdio.h> #include < stdlib.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);    //create fifo    if (ret==-1)     {         perror ("Mkfifo");         return 1;    }    int fd=open (_PATH_,O_WRONLY);   //open by the way of write    if (fd<0)      {        printf ("open error\n");     }     char buf[_size_];     memset (buf, '     while ', strlen (BUF) +1);     {(1) &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%s", buf);         int ret=write (Fd,buf,strlen (BUF) +1);         if (ret<0)         {             printf ("write error!\n");             break;        }         if (strncmp (buf, "Quit", 4) ==0)         {             break;        }     }    close (FD);     return 0;} SERVER.C (read end) #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, ' n ', sizeof (BUF));     while (1)     {         int ret=read (fd,buf,sizeof (BUF));         if (ret<=0)   //error or read the end of file         {            printf ( "Error or read the end of file\n ");             break;        }         printf ("%s\n", buf);         if (strncmp (buf, "Quit", 4) ==0)         {             break;        }    }     close (FD);     return 0;} Results:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7F/16/wKioL1cTSGeQ2-o7AAAiihA6GWo898.png "title=" Image.png "alt=" Wkiol1ctsgeq2-o7aaaiiha6gwo898.png "/>

This article is from the "Voice of the Heart" blog, be sure to keep this source http://ljy789.blog.51cto.com/10697684/1764766

Implementation of Named Pipes (FIFO)

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.