I have two programs to write and read a FIFO. One is read(O_RDONLY) a FIFO. Another is write data into this FIFO. This is code:
Read: The executable file is named read.
#include<errno.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdio.h>
#include<string.h>
int main(int argc, char *argv[])
{
int fd, nread;
const char *pipe_file = "/tmp/test_fifo";
char buf[0x100] ;
fd = open(pipe_file, O_RDONLY);
while(1) {
printf("\n"); /* */
memset(buf, 0, 100);
nread = read(fd, buf, 0x100);
printf("data is %s", buf);
sleep(1);
}
return 0;
}
Write: The executable file is named write.
#include<errno.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdio.h>
#include<string.h>
int main(int argc, char *argv[])
{
int fd;
int num = 1234;
char *s = "changzhi";
const char *pipe_file = "/tmp/test_fifo";
fd = open(pipe_file, O_WRONLY);
write(fd, s, strlen(s));
return 0;
}
The FIFO named /tmp/test_fifo
is already exists. When I run read
to read FIFO and run write
to write the FIFO, everything goes ok. But, the read
can not read data when there is no printf("\n");
in read code. I don not know why. Could someone help me ? Thanks a lot!