[comp.unix.xenix.sco] select system call

mcm@siva.UUCP (Mark Marsh) (03/21/91)

Dear coders:

I have been trying to create a test program that uses the select() system
call as provided by SCO in Xenix 2.3.2 and Sequent in Dynix. The particulars
seem to vary, but one thing is constant: I am not getting what I expect,
and am asking for pointers. (Whew!)

I am attempting to make a blocking select() (don't return until there 
are characters to be read) and both examples do this - the first time.
After that the loops complete without waiting for characters to appear on
the input stream. resetting the read file descriptor mask within the loop
does not appear to have any effect.

What am I doing wrong?
How do I do it right?
Thanks!

Example 1) Dynix (either att or ucb universe)
----------------------------------------------------------------------
#include <sys/time.h>

main()
{
	  int i = 0, nfd = 1;
	  int rfd = (1 << 0); /* wait on stdin */

	  while(++i < 5) {
		 printf("[%d]",
				  _select(nfd, &rfd, (int *)0, (int *)0, (struct timeval *)0)
				 );
		 puts("x");
	  }
}

Example 2) SCO Xenix 2.3.2 - (using the example from the man page)
----------------------------------------------------------------------
typedef long time_t;
#define bzero(s, n) memset(s, 0, n)
#include <fcntl.h>
#include <sys/select.h>
#include <sys/times.h>
#include <sys/types.h>

main()
{
	int nfound, nfds = 4;
	int fd, i = 0, ret;
	fd_set readfds;
	char buf[2048];

	system("mknod S_pipe p");

	if((fd = open("S_pipe", O_RDONLY,  0)) == -1) { 
		printf("error open"); exit(1);
	}

	while(i++ < 5) {

		FD_ZERO(&readfds);
		FD_SET(fd, &readfds);
		nfound = 0;

		nfound = select(nfds, &readfds, 0, 0, (struct timeval *)0);

		printf("select [%d] ready streams fd%d [%d]\n",
				nfound, fd, FD_ISSET(fd, &readfds));

		if(FD_ISSET(fd, &readfds)) {
			ret = read(fd, buf, sizeof(buf));
			if(ret <= 0) {
				printf("%d[no data]\n", fd);
			} else  {
				printf("%d[%*.*s]\n", fd, ret, ret, buf);
			}
		}
	}

	close(fd);
	exit(0);
}
-- 
mark@siva -or- {ames!pacbell!sactoh0}||{ucbvax!ucdavis!csusac!unify}!siva!mark
     "It's a hearty instrument that blames its faults on its player."