[erlang-questions] Stop C-port in case of erlang process killed

Michael Santos michael.santos@REDACTED
Mon Nov 2 18:36:56 CET 2015

On Mon, Nov 02, 2015 at 07:59:18PM +0300, Kirill Ratkin wrote:
> Hi,
> I'm developing C-based program which should work as port.
> Issue is when erlang process hardly killed (by exit(Pid,kill)) from
> observer for example. In this case C-process doesn't see what happen in
> Erlang node and continues to work. Is there some way to catch such
> situation in C?

In the port, check for EOF when doing the read on stdin and exit. A port
is basically written like a component in a shell pipeline:

    1> P = open_port({spawn, "/bin/cat"}, [binary]).

    2> erlang:port_info(P).

    3> erlang:port_command(P, "foobar\n").
    4> flush().
    Shell got {#Port<0.749>,{data,<<"foobar\n">>}}

    4> self().
    % shell process owns the port: killing the Erlang process kills the port
    5> a = b.
    ** exception error: no match of right hand side value b
    6> self().

    7> erlang:port_command(P, "foobar\n").
    ** exception error: bad argument
         in function  port_command/2
            called as port_command(#Port<0.749>,"foobar\n")

> It seems C-process sees nothing when erlang processe killed. I tried to
> catch signals (SIGPIPE and others).

The port will receive a SIGPIPE if it tries to write to stdout.

> I run C-process under 'strace' and
> tried to gdb it. There are no messages/signals come to C-process excepting
> some read/write periodical messages which looks like heartbeat.
> Maybe I need to make some 'monitor' or 'link' of Erlang process from
> C-process ...
> BR
> Kirill

> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions

More information about the erlang-questions mailing list