Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 1 Jun 2000 17:06:56 +0100
From:      Ben Smithurst <ben@scientia.demon.co.uk>
To:        questions@FreeBSD.org
Subject:   readers missing EOF on FIFOs.
Message-ID:  <20000601170656.X99925@strontium.scientia.demon.co.uk>

next in thread | raw e-mail | index | archive | help
hi,

I'm being puzzled by how to use FIFOs properly.  I've written a small test
program,

#include <sys/stat.h>

#include <err.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define _PATH_FIFO "fifo"

int
main(void) {
	struct stat sb;
	FILE *fp;
	struct sigaction sa;

	sa.sa_handler = SIG_IGN;
	sa.sa_flags = SA_RESTART;
	sigemptyset(&sa.sa_mask);
	sigaction(SIGPIPE, &sa, NULL);

	if (stat(_PATH_FIFO, &sb) != 0)
		err(1, "stat " _PATH_FIFO);
	if (!S_ISFIFO(sb.st_mode))
		errx(1, _PATH_FIFO " is not a FIFO");

	for (;;) {
		if ((fp = fopen(_PATH_FIFO, "w")) == NULL)
			err(1, _PATH_FIFO);

		fprintf(fp, "Hello!\n");

		fclose(fp);
	}
}

But when I do a ``cat fifo'', I get odd results sometimes:

$ cat fifo
...
Hello!
Hello!
Hello!
Hello!
Hello!
Hello!
Hello!
Hello!
Hello!
Hello!
Hello!
Hello!
Hello!
Hello!
Hello!
Hello!
Hello!
Hello!
Hello!
Hello!
Hello!
Hello!
Hello!
Hello!
Hello!

For some reason, 'cat' doesn't see EOF when my 'fifotest' program closes
the FIFO.  Can someone tell me what I'm doing wrong here please?  This
doesn't always happen.  If I run 'cat' like that a few times in a row,
it will normally happen, and if I run 'cat' again straight away it
normally happens again.  If I leave it for a while it generally takes a
few attempts to make it happen again.  If I add a sleep(2) after the fclose
it seems to work fine, but this seems like an ugly kludge.

FWIW I get similar results on FreeBSD 3, 4 and 5.

-- 
Ben Smithurst / ben@scientia.demon.co.uk / PGP: 0x99392F7D


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20000601170656.X99925>