X-Recipient: archive-cygwin@delorie.com
X-SWARE-Spam-Status: No, hits=-1.9 required=5.0 	tests=AWL,BAYES_00
X-Spam-Check-By: sourceware.org
Date: Tue, 16 Feb 2010 02:20:55 +0100
From: Enrico Forestieri <forenr@lyx.org>
To: cygwin@cygwin.com
Subject: Re: select() and named pipes
Message-ID: <20100216012055.GA7936@GIOVE>
References: <20091222224309.GA3504@GIOVE>  <20091223003714.GA4491@ednor.casa.cgf.cx>  <20091223021219.GA5836@GIOVE>  <20100103165846.GA3284@GIOVE>  <20100103191121.GA17057@ednor.casa.cgf.cx>  <20100215015427.GB5207@ednor.casa.cgf.cx>
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="u3/rZRmxL6MmkK24"
Content-Disposition: inline
In-Reply-To: <20100215015427.GB5207@ednor.casa.cgf.cx>
User-Agent: Mutt/1.5.20 (2009-06-14)
Mailing-List: contact cygwin-help@cygwin.com; run by ezmlm
Precedence: bulk
List-Id: <cygwin.cygwin.com>
List-Unsubscribe: <mailto:cygwin-unsubscribe-archive-cygwin=delorie.com@cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe@cygwin.com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin@cygwin.com>
List-Help: <mailto:cygwin-help@cygwin.com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner@cygwin.com
Mail-Followup-To: cygwin@cygwin.com
Delivered-To: mailing list cygwin@cygwin.com

--u3/rZRmxL6MmkK24
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

On Sun, Feb 14, 2010 at 08:54:27PM -0500, Christopher Faylor wrote:

> I just checked in YA in my series of attempts to get this working right.
> It will behave marginally better now but there are still problems if you
> attempt to write to a fifo before anything is reading it and then try to
> do a non-blocking read.

Thank you for your efforts. It indeed works better with the 2010-02-15
snapshot.

In order to help you pinpointing another glitch, I attach here a slightly
slightly modified version of the original test case and a shell script.
Launching the program in a terminal and then the shell script in another
one, it seems that the program hangs on the write() call.
Apparently, the problem is the O_RDWR flag, as replacing it with O_WRONLY,
everything works. The test case works with O_RDWR both in cygwin 1.5 and
linux.

-- 
Enrico

--u3/rZRmxL6MmkK24
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="fifotest.c"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#define FIFO_IN "/tmp/pipe.in"
#define FIFO_OUT "/tmp/pipe.out"
#define ACK "Message received\n"

int main(void)
{
    int infd;
    int outfd;
    int nsel;
    fd_set readfds;
    FD_ZERO(&readfds);

    if (mkfifo(FIFO_IN, 0600) < 0 || mkfifo(FIFO_OUT, 0600) < 0) {
	perror("mkfifo");
	remove(FIFO_IN);
	remove(FIFO_OUT);
	exit(1);
    }

    infd = open(FIFO_IN, O_RDONLY | O_NONBLOCK);
    outfd = open(FIFO_OUT, O_RDWR);

    if (infd < 0 || outfd < 0) {
	perror("open");
	if (infd >= 0)
	    close(infd);
	if (outfd >= 0)
	    close(outfd);
	remove(FIFO_IN);
	remove(FIFO_OUT);
	exit(2);
    }

    FD_SET(infd, &readfds);
    do {
	nsel = select(infd + 1, &readfds, 0, 0, 0);
    } while (nsel == -1 && (errno == EINTR || errno == EAGAIN));

    if (nsel == -1) {
	perror("select");
	exit(3);
    }

    if (FD_ISSET(infd, &readfds)) {
	char buf[100];
	int status;
	while ((status = read(infd, buf, sizeof(buf) - 1))) {
	    if (status > 0) {
		buf[status] = '\0';
		printf("%s", buf);
	    } else {
		printf("\n");
		if (errno != EAGAIN)
		    perror("read");
		break;
	    }
	}
    }

    if (write(outfd, ACK, strlen(ACK)) < 0)
	perror("write");

    sleep(1);
    close(infd);
    close(outfd);
    remove(FIFO_IN);
    remove(FIFO_OUT);
    return 0;
}

--u3/rZRmxL6MmkK24
Content-Type: application/x-sh
Content-Disposition: attachment; filename="fifotest.sh"
Content-Transfer-Encoding: quoted-printable

#!/bin/sh
echo "foo" > /tmp/pipe.in
cat /tmp/pipe.out


--u3/rZRmxL6MmkK24
Content-Type: text/plain; charset=us-ascii

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
--u3/rZRmxL6MmkK24--
