c - Should a read from FIFO block after all the data was just read from that FIFO? -
i'm learning pipe programming in linux, , having trouble understanding pipe / fifo management.
i wrote small program opens fifo created (i did mkfifo newfifo
in terminal before executing program). repeatedly read , dump character buffer. i'm filling fifo using echo "message" > newfifo
terminal's cmd line.
the problem when write fifo, can read data in buffer, read doesn't block anymore. understanding after read data fifo, fifo should empty , read should block. thinking wrong, or incorrectly managing fifo?
code below:
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <sys/types.h> #define newpipe "./newfifo" void main() { int great_success = 0; int fd; char buffer[20]; fd = open(newpipe, o_rdonly); while (1) { great_success = read(fd, buffer, 20); if (great_success < 0) { printf("pipe failed\n"); } else { printf("buffer : %s\n", buffer); printf("great_success = %d\n", great_success); great_success = 0; } } }
your understanding of how fifos works incorrect. pipes: if write end closed (the echo command has terminated), read end read end-of-file (eof), i.e. return 0.
note when open fifo, isn't read blocking. blocking system call open() system call, explained in http://linux.die.net/man/4/fifo
Comments
Post a Comment