Problem with select() call

Mark Delany markd at iti.org
Sun Sep 30 15:00:18 AEST 1990


Sorry if this is an oft repeated query, but...

I'm having trouble using select() and I was wondering whether:

	a) I'm doing something wrong

	b) There is a problem with this on Xenix at my rev

	c) There is a known fix


The system is Xenix SysV version 2.3.1 The Development system is
2.2 (I think).

All I'm trying to do is select on two file units, stdin and a
pipe created with pipe().

What happens is select() is always indicating "readability" on the pipe
even when there is nothing to be read.

As a simple demonstration, the attached prog does a select on stdin only.

It works fine if stdin is the terminal, but as soon as I pipe to it,
select() always returns the bit set saying there is something to read.

In other words, "./prog" works, but: "cat | ./prog" doesn't!

In desperation I used poll() as a work-around and it returns POLLNVAL on
the pipe - sigh!

Can anyone offer enlightenment?

-------------------------------------------------------------

/*
 * Trivial prog to test select on stdin.
 */

#include <stdio.h>

#include <sys_2.3/types.h>	/* Don't ask me why this is in sys_2.3! */
#include <sys/select.h>

main()

{
    fd_set	fdset;
    struct	timeval	timeout;
    char	buf[100];
    int		result;
    int		bytes;

    while (1) {

	timeout.tv_sec = 3;
	timeout.tv_usec = 0;

	FD_ZERO(&fdset);
	FD_SET(fileno(stdin), &fdset);
	
	result = select(fileno(stdin) + 1, &fdset, NULL, NULL, &timeout);

	printf("SELECT Result gave %d with %x\n",
		result, FD_ISSET(fileno(stdin), &fdset));

	if (result < 0) {
	    perror("Select failed");
	    sleep(2);	/* Slow the spin if error return */
	    continue;
	}


	if ((result >= 1) && FD_ISSET(fileno(stdin), &fdset)) {
	    printf("Reading a single byte from STDIN\n");
	    bytes = read(fileno(stdin), buf, 1);
	    if (bytes == 0) {
		printf("Eof\n");
		break;
	    }
	    printf("SIN Read gave %d\n", bytes);
	}
    }
}


--

------------         -----------------     ---------------------
Mark Delany          markd at Aus.Sun.COM     ...!sun!sunchat!markd
------------         -----------------     ---------------------



More information about the Comp.unix.xenix.sco mailing list