select query

dav at ucb-vax.ARPA dav at ucb-vax.ARPA
Tue Feb 5 20:47:26 AEST 1985


	Gee, since no one else has gotten this right yet I guess I'll chip
	in my two cents worth.

>From jsol at BBNCCV.ARPA Tue Jan 29 18:12:59 1985 remote from ucivax
>boolean io_haschar(infile)
>FILE *infile;
>{
>  int readfds, nfound;
>  struct timeval timeout;
>
>  timeout.tv_sec = 10;
>  timeout.tv_usec = 0;
>
>  readfds = (1<<fileno(infile));
>    
>  nfound = select(1, &readfds, 0, 0, &timeout);  <-----  Vital line!
>
>  if (nfound == 0)
>	return(FALSE);
>  if (readfds&(1<<fileno(infile)))
>	return (TRUE);
>  else
>	return(FALSE);
>}

>The problem is: When I call this peice of code, it times out,
>claiming infile has no characters ready to read. Examination of the
>data stream (with a line monitor) indicates that a character is being
>sent to the program, but somehow the program doesn't notice.

	Your problem has nothing to do with DEC support (but I'm not
	surprised that everyone jumped on that as a possible reason
	instead of looking for code errors).  Your problem has to do
	with the first argument to select on the line I flagged above.
	This argument is documented as "nfds", the number of file
	descriptors to look at.  Your choice of 1 means that select only
	looks at lines "0 through N-1", or just 0.  Of course, your
	readfds mask tells select to ignore all but "fileno(infile)",
	which probably isn't 0.  I suggest changing that first arg to
	32 (or better yet, fileno(infile)+1).  This should solve your
	problem.

			David L. Markowitz
			Rockwell International
			...!ucbvax!{ucivax,trwrb}!csuf!dav



More information about the Comp.unix.wizards mailing list