[erlang-questions] port_close/1 problem

Per Hedeland <>
Wed Oct 31 17:38:23 CET 2007


Serge Aleynikov <> wrote:
>
>Per Hedeland wrote:
>> I guess not, i.e. you probably have some
>> wrapper around it - and if so, can't you send the pid from the wrapper
>> code *before* doing all the loading?
>
>I don't have a wrapper - the interpreter does some heavy weight 
>initialization (loading a bunch (~ 200!!!) of shared objects) and at 
>some point will evaluate my script that would communicate with Erlang 
>through a pipe. 

Ah, OK.

>> Alternatively, you could experiment with something along the lines of
>> 
>>  {spawn, Exe ++ " & echo $!"}
>> 
>> That obviously won't work as-is with {packet, 2}, so it has to be a bit
>> more complex, and the backgrounding *might* mess up the pipe plumbing,
>> but I don't really think it should.
>
>Thanks, I'll try, though I am doubtful that sending a process in the 
>background won't hurt the piping setup.

Actually I'm pretty sure it would work, the backgrounded process should
just inherit the pipe file descriptors - and a quick test seems to
confirm it. But you definitely lose the exit_status as your interpreter
process is no longer a child of the VM process. So, here's a wrapper for
you, with "more complex" and all:

1> Exe = "sleep 1000".
"sleep 1000"
2> Wrapper = "echo $$ | awk '{printf \"%c%c%s\",0,length($1),$1}' >&4; exec ".
"echo $$ | awk '{printf \"%c%c%s\",0,length($1),$1}' >&4; exec "
3> Settings = [binary, {packet, 2}, nouse_stdio, exit_status].
[binary,{packet,2},nouse_stdio,exit_status]
4> Port = open_port({spawn, Wrapper ++ Exe}, Settings).
#Port<0.95>
5> Pid = receive {Port, {data, P}} -> binary_to_list(P) end.
"97122"
6> os:cmd("kill " ++ Pid).
[]
7> Status = receive {Port, {exit_status, S}} -> S end.
143
8> 

--Per



More information about the erlang-questions mailing list