[erlang-questions] Using select in a Port

Michael Schreckenbauer <>
Fri Oct 19 10:48:44 CEST 2007


Am Freitag, 19. Oktober 2007 schrieb jm:
> 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, read_cmd() then fails to read the two byte header and returns -1.
> Simply put what's the fix? Have I missed something to do with select()?

imo you missed something to do with read ;)

> The read_cmd() and read_exact() are take from "Programming Erlang" and
> http://www.trapexit.org/How_to_use_ei_to_marshal_binary_terms_in_port_progr
>ams and should be correct.

They are, for reading from stdin. See below.

> Any help appreciated.
> Jeff.
> /**************** code extract ***************/
> int main() {
> .....
>   maxfd = setup_select(nlist, &readfds);
>    while ((retval = select(maxfd + 1, &readfds, NULL, NULL, NULL)) >= 0) {

This proceeds, if at least one(!) of the fds you gave won't block, when you do 
a read on it. That could be stdin or any other.

>      if FD_ISSET(0, &readfds) {
>        memset(buf, 0, BUFFER_SIZE);
>        read_cmd(buf, &size);

Here you check the right fd, but read_cmd always uses stdin.
The nonblocking fd is 3, your file, as you can see in the debug output. So 
read_cmd must fail. It needs an additional parameter holding the fd you want 
to use.


I don't want to bore you, but there's nobody else around for me to bore.

More information about the erlang-questions mailing list