Subject: fifo & stdio ?
To: None <netbsd-users@netbsd.org>
From: Hubert Feyrer <feyrer@rfhs8012.fh-regensburg.de>
List: netbsd-users
Date: 06/13/1999 20:05:02
Hello,

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
^C


#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);
                return;
                }
                           
	fd = open(FIFO, O_RDONLY | O_NONBLOCK);

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

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

	while(1){
		char buf[1000];

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

		sleep(1);
	}
}


Thanks for any help!


 - Hubert

-- 
NetBSD - Better for your uptime than Viagra