[erlang-questions] Using select in a Port

jm <>
Thu Oct 25 02:50:09 CEST 2007


Per Hedeland wrote:

>>>> Is the port owner process still running? The port gets automatically
>>>> closed when the owner exits.
> 
> I.e. I'm referring to the Erlang process that opened the port (unless
> you have changed the port owner with e.g. port_connect/2). That process
> exiting is the most likely cause of the port getting closed from the
> Erlang side. Possibly the port could also be closed by the runtime if
> you have specifed some more "advanced" "protocol" than
> stream/packet/line and the incoming data doesn't follow it (any random
> data can be interpreted as being either of those three), but in that
> case the port owner should get an exit signal.


The erlang process which owns the port dies. I've checked to see that I 
didn't do something so that it's not tail recursive and it seems 
correct. Also checked to make sure I wasn't doing something stupid like 
printing debug info to stdout, checked that all messages sent to erlang 
are generated by ei and sent using write_cmd.

The port is created via

start() ->
     spawn(fun() ->
                   register(?MODULE, self()),
                   process_flag(trap_exit, true),
                   Port = open_port({spawn, "./inotify"},
                                    [{packet, 2}, binary, exit_status]),
                   loop(Port)
           end).

so nothing funny there. I wrote a stripped down port which just sat 
there and sent {ok, Count} messages once a second for 5 second where 
Count is the number of seconds minus one. This worked which turned my 
attention to getting more info out of erlang about what it was seeing.


Next, based on an email from  Subject: Re: 
[erlang-questions] turning up debugging on ports,

4> dbg:tracer().
{ok,<0.38.0>}
5> dbg:p(all,call).
{ok,[{,25}]}
6> dbg:tpl(inotify,loop, [{'_',[],[{return_trace}]}]).
{ok,[{,1},{saved,1}]}
7> inotify:start().
<0.43.0>(<0.43.0>) call inotify:loop(#Port<0.98>)

8> inotify:open().
  ......
9> inotify:add(3, "file", all).
read_cmd start curpos = 0
read_cmd curpos = 2
read_cmd len = 24 desired=24
read_cmd result: 2, size: 100, cmdpos: 26
read_cmd returned message
add
atom = all
add: fd=3 pathname=file mask = FFF
wd = 1
note_read
note_read end while for fd = 3
note_read return
(<0.43.0>) call inotify:loop(#Port<0.98>)
call_port: {ok,1}
{ok,1}

%%% run "cat file" in shell

10> note_read
note_read fd = 3
encoding event wd=1, mask=32, cookie=0, len=0, name=
note_read send event
encoding event wd=1, mask=32, cookie=0, len=0, name=
note_read send event
note_read end while for fd = 3
note_read return 
    10> read_cmd start curpos = 0
read_cmd result: 0, size: 100, cmdpos: 0
read_cmd returned 0: Success(0)
10>

I'm not familar with the dbg module so I'm off to read the man page.

Jeff.



More information about the erlang-questions mailing list