[erlang-questions] erlang port and C executable

Per Hedeland per@REDACTED
Sun Nov 23 20:08:46 CET 2008


Johnny Billquist <bqt@REDACTED> wrote:
>
>Per Hedeland wrote:
>> "Edwin Fine" <erlang-questions_efine@REDACTED> wrote:
>>> One of the most annoying aspects is that under
>>> circumstances that are not clear to me, the C program that is spawned
>>> sometimes does not seem to be sent a signal for end of file when you close
>>> the port, so if it is reading stdin, it's just going to sit there until
>>> killed manually.
>> 
>> This should never happen - i.e. there is no "signal" for end of file,
>> it's just the result of reading when there is nothing more to read - but
>> a port program that actually reads its stdin should always get end of
>> file when the port is closed (after having read what there is to read,
>> if anything). "You're probably doing something wrong."
>
>The most common "wrong" is that someone opens/creates a pipe/socket, 
>forks, and then writes to the fd, and eventually closes it. The reader 
>will not receive an eof, since another process still have the fd open as 
>well. eof will only be signalled once *all* processes having the 
>pipe/socket open for writing have closed it.

You're talking about using pipes in general, but this is not relevant
for the pipes set up by open_port/2, since it is done correctly.:-) Only
the Erlang runtime has the relevant pipe open for writing, so the
closing of the port is enough. Easy enough to verify that forking
another process doesn't cause a problem:

1> Cmd = "sleep 100 & while read x; do : ; done; echo got EOF >&2".
"sleep 100 & while read x; do : ; done; echo got EOF >&2"
2> Port = open_port({spawn, Cmd}, []).
#Port<0.95>
3> port_close(Port).
got EOF
       true

(How's that for a weird return value btw.:-)

--Per



More information about the erlang-questions mailing list