Subject: fifo & stdio ?
To: None <>
From: Hubert Feyrer <>
List: netbsd-users
Date: 06/13/1999 20:05:02

I'm currently debugging gqmpeg's ipc system, and there's something I don't
understand; maybe someone here can help me. I have an open file descriptor
from a fifo (open(2)'ed with O_NONBLOCK), and i get a file pointer via
fdopen(3) from that descriptor. I then want to check periodically if
there's some data in the pipe, and do something else if not ("oink", in
the program below).

What I don't understand now is why I do not get any data when (trying to)
read any available data with fgets(). feof() says we're at EOF after(!)
the first call to fgets(), and doesn't change it's mind even after I send
some data to the fifo ("echo bla >fifo.pipe"). 

Is it possible our stdio is mixing something up here WRT pipes,
buffering, eof, ...?

Here's my test program. Compile it, and try to "echo some data
>>fifo.pipe". The intention is to print out that data from the pipe.
Instead, I just get:

miyu% ./fifo 
fp=0x40093410, fd=3
oink! feof=0
oink! feof=1             <- "echo foo >>fifo.pipe" was done here
oink! feof=1

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>

#define FIFO "fifo.pipe"

int main(void)
	FILE *fp;
	int fd;

	if (mkfifo(FIFO, S_IRUSR | S_IWUSR) != 0)
                printf("Failed to mkfifo for %s\n", FIFO);
	fd = open(FIFO, O_RDONLY | O_NONBLOCK);

        if (fd == -1)
                printf("unable to open %s\n", FIFO);
        fp = fdopen (fd, "r");

	printf("fp=%p, fd=%d\n",fp,fd);

		char buf[1000];

		printf("oink! feof=%d\n",feof(fp));
		while (fgets(buf, 1024, fp)) {
			printf("buf=<%s>\n", buf);


Thanks for any help!

 - Hubert

NetBSD - Better for your uptime than Viagra