select() weirdness

chad fowler chadfowler@yahoo.com
Wed Nov 14 08:28:00 GMT 2001


I'm a little confused about the behavior of select(). 
It seems to behave differently in Cygwin than on other
systems.  This is specifically regarding the exception
fd_set when piping program output to another program. 
Here's an example (which might have a problem or two
but should demonstrate sufficiently):

<code_sample>
#include <sys/types.h>
#include <sys/time.h>
#include <fcntl.h>
#include <stdio.h>

#define TIMEOUT     1          

int openFile(char *);

int
main(int argc, char **argv)
{ 
	fd_set readfds, writefds, exceptfds;
	int n, fd, so, se, f;
	struct timeval tv;

	fd = openFile("select.c");

        FD_ZERO(&readfds);
        FD_ZERO(&writefds);
        FD_ZERO(&exceptfds);

	FD_SET(fd, &readfds);
       	FD_SET(fd, &exceptfds);
	FD_SET(1, &writefds);
	FD_SET(2, &writefds);
	FD_SET(1, &exceptfds);
	FD_SET(2, &exceptfds);

        tv.tv_sec = TIMEOUT;
        tv.tv_usec = 0;

        n = select(3, &readfds, &writefds, &exceptfds,
&tv);

        switch (n) {
        case -1:            
            perror("select");
            exit(1);
        case 0:            
            printf("\nTimeout expired.  Type
something!\n");
            break;
        default:            
		f = FD_ISSET(fd, &readfds); 
		so = FD_ISSET(1, &readfds), 
		se = FD_ISSET(2, &readfds);
		printf("READ - file: %d, stdout: %d, stderr: %d\n",
f, so, se);
		f = FD_ISSET(fd, &writefds), 
		so = FD_ISSET(1, &writefds), 
		se = FD_ISSET(2, &writefds);
		printf("WRITE - file: %d, stdout: %d, stderr: %d\n",
f, so, se);
		f = FD_ISSET(fd, &exceptfds), 
		so = FD_ISSET(1, &exceptfds), 
		se = FD_ISSET(2, &exceptfds);
		printf("EXCEPT - file: %d, stdout: %d, stderr:
%d\n", f, so, se);
            break;
        }
    }

int openFile(char *file)
{	 int fd;
	fd = -1;
        if ((fd = open(file, O_RDONLY)) < 0) {
            perror(file);
            exit(1);
        }
	return fd;
}
</code_sample>

<output_on_cygwin>
$ ./a.exe
READ - file: 0, stdout: 0, stderr: 0
WRITE - file: 0, stdout: 2, stderr: 4
EXCEPT - file: 0, stdout: 0, stderr: 0

$ ./a.exe |cat
READ - file: 0, stdout: 0, stderr: 0
WRITE - file: 0, stdout: 2, stderr: 4
EXCEPT - file: 0, stdout: 2, stderr: 0
</output_on_cygwin>


<output_on_linux>
cef@yawmp:~$ ./a.out
READ - file: 0, stdout: 0, stderr: 0
WRITE - file: 0, stdout: 1, stderr: 1
EXCEPT - file: 0, stdout: 0, stderr: 0

cef@yawmp:~$ ./a.out |cat
READ - file: 0, stdout: 0, stderr: 0
WRITE - file: 0, stdout: 1, stderr: 1
EXCEPT - file: 0, stdout: 0, stderr: 0
</output_on_linux>

Notice the "EXCEPT" lines.  AFAIK, the behavior on
Cygwin is incorrect, though I'm *far* from being an
expert.  

Any help would be greatly appreciated.

Thanks,
Chad Fowler

__________________________________________________
Do You Yahoo!?
Yahoo! GeoCities - quick and easy web site hosting, just $8.95/month.
http://geocities.yahoo.com/ps/info1

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/



More information about the Cygwin mailing list