hi,

I sent this question to -questions but got no response.  Perhaps someone
on -hackers can help...  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 / [EMAIL PROTECTED] / PGP: 0x99392F7D


To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message

Reply via email to