[erlang-questions] Using select in a Port

Per Hedeland per@REDACTED
Fri Oct 19 08:36:52 CEST 2007


jm <jeffm@REDACTED> wrote:
>
>What I think is the relevent bits of code is below. It appears that 
>select() is returning saying that something is waiting to be read on 
>stdin,

select() (or poll()) never says that - it says "If you try to read()
from (e.g.) stdin, it won't block even if the file descriptor is in
blocking mode".

>int read_cmd(byte *buf, int *size)
>{
>   int len;
>
>   if (read_exact(buf, 2) != 2) {
>     fprintf(stderr, "read_cmd return(-1)\r\n");
>     return(-1);
>   }

This is plain broken - the return value must be checked properly:

   > 0 but less than you wanted: No error, just that all the data hasn't
   arrived yet - save what you got and try again for the rest the next
   time select/poll triggers.

   0: end-of-file - not an error (the other end of a socket or pipe was
   closed, or someone hit ^D on a tty), but you don't want to read from
   this descriptor ever again - select/poll will keep reporting the fd
   as "ready".

   < 0: Error - depending on the context of your program you may need to
   check for errno EINTR or EAGAIN, which could be non-fatal in which
   case you just ignore the whole thing. Otherwise fatal, and the same
   processing as for EOF typically applies. (This is pretty rare and
   most likely a bug in your program, e.g. trying to read from a closed
   descriptor).

Of course your problem could be something else too...

--Per Hedeland



More information about the erlang-questions mailing list