Famous Pipe Summary

Source: Internet
Author: User
Tags assert

For the creation of nameless pipes:

#include <sys/stat.h>

int Mkfifo (const char* pathname,mode_ mode);

If you are in a Linux environment, you can also use the MKFIFO directive to create the pipe file directly in the shell terminal.

FIFO Common uses:

(1) FIFO is used by the shell command to transfer data from one pipe line to another without creating intermediate temporary files.

(2) FIFO is used in the client process-server process application to pass data between the client process and the server process.

The second use is mainly discussed here. (because I have never used the first use)

The first embodiment uses Mkfifo to create a FIFO pipeline file, uses it as the relay, then can realize the communication between two irrelevant processes.

Program to write Data:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include <fcntl.h>


int main (int argc,char* argv[])
{
int FDW = open ("./fifo", o_wronly);//Open one end of FIFO in write form
Char buff[128] = {0};
while (1)
{
printf ("Please enter \ n");
Fgets (Buff,128,stdin);


if (strncmp (Buff, "End", 3) ==0)
{
Break
}
Write (Fdw,buff,strlen (buff));//data is written directly to FIFO


}
Close (FDW);
return 0;


}


Program to read data:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <assert.h>


int main (int argc,char* argv[])
{
int FDR = Open ("./fifo", o_rdonly);//opens FIFO in read form
while (1)
{
Char buff[128]= {0};
int n = read (fdr,buff,127);//Read the data in the FIFO to the buff.
if (n==0)
{
Break
}
printf ("Get buff =%s", buff);
}
Fflush (stdout);//ensure Print succeed
Close (FDR);
return 0;
}













Related Article

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.