[Date Prev] [Date Index] [Date Next] [Thread Prev] [Thread Index] [Thread Next]

Re: [SPAM] Re: Console taking 100% CPU

Thor Simon tls@coyotepoint.com
Tue, 14 Dec 2010 22:25:01 GMT


On Tue, Dec 14, 2010 at 05:15:05PM -0500, Chris Marget wrote:
> 
> I've added two lines here.  Seems to do what I need.  Am I on the right track?

Not really.  There is nothing "screwy" about read returning 0; it just means
end-of-file.  And negative values other than -1 are not allowed by POSIX.
Bearing that in mind, a better structure for the code would be:

if (FD_ISSET(STDIN_FILENO, &rmask)) {
	nc = read(0, acMesg, sizeof(acMesg);
	switch (nc) {
	  case -1:
	    /* handle error */
	    break;   
	  case 0:
	    /* handle end-of-file */
	    break;
	  default:
	    /* do whatever is usual;
	    continue;
	}
	break;	/* THIS one gets you out of the outer loop.
		   Strategic use of 'goto' may make the code more clear. */
}

Generally speaking testing particular individual file descriptors against
the returned descriptor set is a sign of questionable program structure,
though.  Usually, programs calling select should loop over all the returned
descriptors, handling each in turn.

Thor