[erlang-questions] Issues with stdin on ports

Richard A. O'Keefe ok@REDACTED
Thu Aug 1 01:38:31 CEST 2013


On 31/07/2013, at 8:10 PM, David Welton wrote:
> Erlang's handling of external programs leaves something to be desired
> compared to other languages:
> 
> * You can't kill them.

You really can't _always_ kill them (or not _simply_)
in other languages either.

Consider the following.

% cat foobar.c
#include <unistd.h>
#include <stdio.h>

int main(void) {
    int i;

    (void)setsid();
    for (i = 0; i < 10; i++) {
        fprintf(stderr, "Iteration %d\n", i);
        sleep(30);
    }
    fprintf(stderr, "Done\n");
    return 0;
} 
% cc -o foobar foobar.c
% csh -c "foobar"

Set foobar running your favourite programming language.
If you get a process ID back, it will be the process ID
of the shell, _not_ the process ID of the foobar process.
Kill the process ID you got back, and you just kill the
shell.  foobar keeps on running.

Even if you are using a PTY to connect to the the
script, a process that calls setsid() has no controlling
terminal, so won't be interrupted by the equivalent of ^C.

If the scheme that you use doesn't go through a shell,
just change foobar to start with

    i = fork();
    if (i != 0) return i < 0;
    setsid();

In the MAJORITY of cases, killing the first process that
got started will in fact make the others drop dead too.
Just be aware that it won't ALWAYS work.





More information about the erlang-questions mailing list