From owner-freebsd-hackers Mon Feb 26 18:40:40 2001 Delivered-To: freebsd-hackers@freebsd.org Received: from bazooka.unixfreak.org (bazooka.unixfreak.org [63.198.170.138]) by hub.freebsd.org (Postfix) with ESMTP id 21FD037B401 for ; Mon, 26 Feb 2001 18:40:37 -0800 (PST) (envelope-from dima@unixfreak.org) Received: from hornet.unixfreak.org (hornet [63.198.170.140]) by bazooka.unixfreak.org (Postfix) with ESMTP id 46EB03E09; Mon, 26 Feb 2001 18:40:36 -0800 (PST) To: Marc W Cc: freebsd-hackers@freebsd.org Subject: Re: Why does a named pipe (FIFO) give me my data twice ??? In-Reply-To: Message from Marc W of "Mon, 26 Feb 2001 12:58:36 PST." <200102262058.MAA39013@akira.lanfear.com> Date: Mon, 26 Feb 2001 18:40:36 -0800 From: Dima Dorfman Message-Id: <20010227024036.46EB03E09@bazooka.unixfreak.org> Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG If the first program calls open(2) before the second one calls close(2) the former will not block because there's already a writer on the pipe. A possible workaround would be to unlink and recreate the fifo in program one, like so: for (;;) { int fifo; mkfifo(fifo_path, fifo_mode); fifo = open(fifo_path, O_RDONLY); read(fifo, ...); printf(...); close(fifo); unlink(fifo_path); } As for why this actually happens, I don't know. None of my Stevens books explain this. Hope this helps Dima Dorfman dima@unixfreak.org > > hello! > > I've got a program that creates a named pipe, and then spawns a > thread > which sits in a loop: > > // error checking snipped. > // > while (1) { > int fifo = open(fifoPath, O_RDONLY); // this blocks > fprintf(stderr, "somebody opened the other end!\n"); > read(fifo, buf, sizeof(buf)); > fprintf(stderr, "got the following data: %s\n", buf); > close(fifo); > } > > i then have another instance of the same program do the following: > > fifo = open(fifoPath, O_WRONLY); > write(fifo, buf, strlen(buf); > > > now, the problem is that the first process succeeds suddenly for > the > open, reads the data, closes the fifo, and IMMEDIATELY SUCCEEDS THE > open(2) > again, reading in the same data. > After that, all is as expected. > > Note that this doesn't happen ALL the time -- only about 80% of the > time. > > Any idea why this would happen? To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message