Read.c#include<stdlib.h>#include<stdio.h>#include<string.h>#include<sys/stat.h>#include<sys/types.h>#include<fcntl.h>#defineW "/tmp/myfifo"intMain () {if(Mkfifo (W, O_creat | O_EXCL) <0) {printf ("Mkfifo fail!\n"); Exit (1); } intFD; if(FD = open (W,o_rdonly | O_nonblock,0)) <0)//In the process of creating the pipeline I cannot open the pipe file as write-only, and return-1. {printf ("Open%s fail!\n", W); Exit (1); } Charbuff[1024x768]; memset (Buff,0,sizeof(Buff)); intRead_num; while(1) { if(Read_num = read (FD, Buff,1024x768)) <=0)//When the WRITE.C does not open the pipe file, Read_num is 0, and when write.c opens the pipe file and there is no input, Read_nu//m is-1; printf ("No data yet!\n"); Else{printf ("%s\n", buff);//Break ;} sleep (1); } unlink (W);
}
Write.c#include<stdlib.h>#include<stdio.h>#include<sys/stat.h>#include<sys/types.h>#include<fcntl.h>#defineFIFO "/tmp/myfifo"#include<string.h>intMain () {intFD; FD=open (FIFO, O_wronly | O_nonblock,0); if(FD = =-1) {printf ("Open%s fail!\n", FIFO); Exit (1); } Charwrite_buff[1024x768]; You can assign an array to a pointer, but you can't assign a pointer to an array, and you'll get an error! memset (Write_buff,' /',sizeof(Char)*1024x768); Gets (Write_buff); Using scanf here will result in the end of the input because of the spaceintWrite_num= Write (Fd,write_buff,1024x768 );}
Simple pipeline testing under Linux