[erlang-questions] erlang port and C executable

Per Hedeland <>
Sun Nov 23 22:30:30 CET 2008


Michael McDaniel <> wrote:
>
> In the original post I forgot to include the command I use to echo back
> the PID;
>
> I get the PID using my port/0 command ...
>
>-define(IF, "/dev/ttyUSB0").
>-define(DD, "/bin/dd if=" ++ ?IF ++ " bs=1 oflag=dsync& pid=$!; echo $pid").

That's generally fine, and actually pretty much exactly what I suggested
earlier in the thread that Edwin pointed to (but it wasn't good enough
*there*:-). There are a couple of gotchas though:

1. Since you are running the "real" program in the background, you won't
   get its exit status, i.e. this

>   Port = open_port({spawn, ?DD },
>		    [eof, exit_status, use_stdio, binary]) ,

   will (eventually) give you the exit status of 'echo'. Could perhaps
   be handled by ending the commandline with 'wait'.

2. If the program starts producing output immediately, there's no
   guarantee that

>   receive
>     {Portb, {data, Pid_with_lf}} ->

   will have only the pid in Pid_with_lf - worst case it could have
   output from the program, and not include the pid at all.

In your case I think neither of those is an actual problem, but it might
be misleading for future readers of your code to include the exit_status
option.

--Per



More information about the erlang-questions mailing list