Message from discussion
managing OS processes
Received: by 10.205.122.65 with SMTP id gf1mr1582639bkc.2.1338290007735;
Tue, 29 May 2012 04:13:27 -0700 (PDT)
X-BeenThere: erlang-programming@googlegroups.com
Received: by 10.204.154.21 with SMTP id m21ls3684758bkw.2.gmail; Tue, 29 May
2012 04:13:27 -0700 (PDT)
Received: by 10.204.143.145 with SMTP id v17mr1578161bku.7.1338290007347;
Tue, 29 May 2012 04:13:27 -0700 (PDT)
Received: by 10.204.143.145 with SMTP id v17mr1578160bku.7.1338290007315;
Tue, 29 May 2012 04:13:27 -0700 (PDT)
Return-Path: <erlang-questions-boun...@erlang.org>
Received: from hades.cslab.ericsson.net (hades.cslab.ericsson.net. [192.121.151.104])
by gmr-mx.google.com with ESMTP id p5si14137647bks.1.2012.05.29.04.13.27;
Tue, 29 May 2012 04:13:27 -0700 (PDT)
Received-SPF: pass (google.com: domain of erlang-questions-boun...@erlang.org designates 192.121.151.104 as permitted sender) client-ip=192.121.151.104;
Authentication-Results: gmr-mx.google.com; spf=pass (google.com: domain of erlang-questions-boun...@erlang.org designates 192.121.151.104 as permitted sender) smtp.mail=erlang-questions-boun...@erlang.org; dkim=neutral (body hash did not verify) header...@googlemail.com
Received: from hades.cslab.ericsson.net (hades [192.121.151.104])
by hades.cslab.ericsson.net (Postfix) with ESMTP id C27AB5C0BD;
Tue, 29 May 2012 13:13:19 +0200 (CEST)
X-Original-To: erlang-questi...@erlang.org
Delivered-To: erlang-questi...@erlang.org
Received: from mail-gg0-f181.google.com (mail-gg0-f181.google.com
[209.85.161.181])
by hades.cslab.ericsson.net (Postfix) with ESMTP id A10135C005
for <erlang-questi...@erlang.org>; Tue, 29 May 2012 13:13:17 +0200 (CEST)
Received: by ggnv5 with SMTP id v5so2821028ggn.40
for <erlang-questi...@erlang.org>; Tue, 29 May 2012 04:13:16 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=googlemail.com; s=20120113;
h=mime-version:in-reply-to:references:date:message-id:subject:from:to
:content-type; bh=M+5wSdPbLpTtbXeTCGT170ynrBpnVbdTMBvVe2Q4TxA=;
b=ybPxasq3gBf7GI4gTMTXMya6V6KkLgCohUaceLEU1Dp6NqdhXKa6hGtBP16XvsmFMC
qk1SzJU1v3dqbSSUS+249q62vvpEOSJZNpf4+qmjiIoyTI/9sKFWEQxVFDgdSXOqy9ep
2PkcpDnlrgrTfMiy1hQ9vfkWeZcetN1hdTQjLm74yRqjePuyHOf/X/tYlbih6RjDgmG1
UIO1id9d1h8Pi9+YDs/CCjwXCjris7MW8Hua3TPFytWBPvaopHpdJXLdkXUv9xYDUBiO
uwMJ6tf6tFNimeNy46wSs0O/SJ5X0FSrV2QBQnu4rsikJV6xY5ZXc6C6Ji1G6e7UizAg
2VIw==
MIME-Version: 1.0
Received: by 10.50.183.200 with SMTP id eo8mr2749891igc.63.1338289996396; Tue,
29 May 2012 04:13:16 -0700 (PDT)
Received: by 10.231.32.69 with HTTP; Tue, 29 May 2012 04:13:16 -0700 (PDT)
In-Reply-To: <CA+Tk8fzRFMicCr5jMyyTSwQxFJsz2XFc5-VW1iBpcNRvJYo...@mail.gmail.com>
References: <CAJNb-9pg8s5qJ3p-qzqSwhA47XycP+GfxCRNo0ELP0KENdu...@mail.gmail.com>
<CADOS1qKshuW-5AiVCbDbfJFOcoCJ0w=_nyDZ6isxb_HvTQ9...@mail.gmail.com>
<CA+Tk8fzRFMicCr5jMyyTSwQxFJsz2XFc5-VW1iBpcNRvJYo...@mail.gmail.com>
Date: Tue, 29 May 2012 12:13:16 +0100
Message-ID: <CAH+xwdBT1MY3i7MZNi7qGAL+S5MAt7dK9kozRX5-NPhXDbu...@mail.gmail.com>
From: Robert Raschke <rtrli...@googlemail.com>
To: erlang-questi...@erlang.org
Subject: Re: [erlang-questions] managing OS processes
X-BeenThere: erlang-questi...@erlang.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: General Erlang/OTP discussions <erlang-questions.erlang.org>
List-Unsubscribe: <http://erlang.org/mailman/options/erlang-questions>,
<mailto:erlang-questions-requ...@erlang.org?subject=unsubscribe>
List-Archive: <http://erlang.org/pipermail/erlang-questions>
List-Post: <mailto:erlang-questi...@erlang.org>
List-Help: <mailto:erlang-questions-requ...@erlang.org?subject=help>
List-Subscribe: <http://erlang.org/mailman/listinfo/erlang-questions>,
<mailto:erlang-questions-requ...@erlang.org?subject=subscribe>
Content-Type: multipart/mixed; boundary="===============5600555872926310467=="
Errors-To: erlang-questions-boun...@erlang.org
Sender: erlang-questions-boun...@erlang.org
--===============5600555872926310467==
Content-Type: multipart/alternative; boundary=14dae9340435d7775704c12aeb34
--14dae9340435d7775704c12aeb34
Content-Type: text/plain; charset=UTF-8
On Tue, May 29, 2012 at 10:21 AM, Ciprian Dorin Craciun <
ciprian.crac...@gmail.com> wrote:
> Hello all!
>
> I don't think its the right way (although it seems to be suggested
> by the Erlang manual or somewhere else I've read it), but the way I
> did it was to write a small C program that is the one "babysitting"
> the real process, and a small Erlang wrapper.
>
> The code is here (Apache 2.0 licensed):
>
> https://github.com/cipriancraciun/mosaic-node/tree/development/applications/mosaic-harness/sources
>
> https://github.com/cipriancraciun/mosaic-node/blob/development/applications/mosaic-harness/sources/mosaic_harness_backend.erl
>
> https://github.com/cipriancraciun/mosaic-node/blob/development/applications/mosaic-harness/sources/mosaic_harness.c
>
> The C program allows the following:
> * it uses a small protocol based on JSON to communicate with the
> Erlang wrapper, using solely `stdin` / `stdout` between itself and
> Erlang, and between itself and the "babysitted" program;
> * it allows a single process to be executed at a time (controlling
> environment, arguments, and working directory); (it could be extended
> to multiple process in parallel if wanted;) (but you can run it again
> if you want;)
> * it allows a signal to be sent;
> * it allows "exchange" messages to be sent to and from Erlang and
> the process;
> * it allows brutal termination; (i.e. SIGKILL;)
> * if `stdin` is closed by the Erlang side it kills the process;
> * if `stdin` is closed by the process side it informs the Erlang side;
>
> The C part is a self-contained `.c` file (of about 2k lines, of
> which 25% are structure definitions), written in an event driven
> manner (no threads), statically linkable, depending only on the
> `jansson` library (for JSON), using mostly POSIX syscalls.
>
> I've successfully used it for about a year now (in non-production
> systems) and it behaves nicely.
>
> If someone finds it useful I could try to extract it in a
> standalone project.
>
> Ciprian.
>
>
I have a similar "proxy" to run killable external programs on Windows. It's
part of a larger project though, so I'd need to extract it. Would there be
interest for me to do that?
The approach is:
Start the proxy program using open_port/2 and send it the command to run,
the proxy then uses DuplicateHandle() and CreateProcessW() to kick off the
command in a separate OS process but with stdin/stdout/stderr forwarded
from the proxy to the new external command, prints the OS process id back
to Erlang and waits for the external OS process to finish.
Thus you now have your command running in the backgroud, able to
communicate using stdio with the Erlang process that started the proxy.
If you want to stop the command, you invoke the proxy with the process id
you got earlier and it kills it using TerminateProcess().
Robby
PS I vaguely remember getting the idea of passing back a process id for
later kill purposes from some open source app that uses a similar approach
to running killable commands from Erlang. Not that I can remember which one
at the mooment, sorry.
--14dae9340435d7775704c12aeb34
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<br><div class=3D"gmail_quote">On Tue, May 29, 2012 at 10:21 AM, Ciprian Do=
rin Craciun <span dir=3D"ltr"><<a href=3D"mailto:ciprian.crac...@gmail.c=
om" target=3D"_blank">ciprian.crac...@gmail.com</a>></span> wrote:<br><b=
lockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px =
#ccc solid;padding-left:1ex">
=C2=A0=C2=A0 Hello all!<br>
<br>
=C2=A0 =C2=A0I don't think its the right way (although it seems to be =
suggested<br>
by the Erlang manual or somewhere else I've read it), but the way I<br>
did it was to write a small C program that is the one "babysitting&quo=
t;<br>
the real process, and a small Erlang wrapper.<br>
<br>
=C2=A0 =C2=A0The code is here (Apache 2.0 licensed):<br>
=C2=A0 =C2=A0 =C2=A0<a href=3D"https://github.com/cipriancraciun/mosaic-no=
de/tree/development/applications/mosaic-harness/sources" target=3D"_blank">=
https://github.com/cipriancraciun/mosaic-node/tree/development/applications=
/mosaic-harness/sources</a><br>
=C2=A0 =C2=A0 =C2=A0<a href=3D"https://github.com/cipriancraciun/mosaic-no=
de/blob/development/applications/mosaic-harness/sources/mosaic_harness_back=
end.erl" target=3D"_blank">https://github.com/cipriancraciun/mosaic-node/bl=
ob/development/applications/mosaic-harness/sources/mosaic_harness_backend.e=
rl</a><br>
=C2=A0 =C2=A0 =C2=A0<a href=3D"https://github.com/cipriancraciun/mosaic-no=
de/blob/development/applications/mosaic-harness/sources/mosaic_harness.c" t=
arget=3D"_blank">https://github.com/cipriancraciun/mosaic-node/blob/develop=
ment/applications/mosaic-harness/sources/mosaic_harness.c</a><br>
<br>
=C2=A0 =C2=A0The C program allows the following:<br>
=C2=A0 =C2=A0* it uses a small protocol based on JSON to communicate with =
the<br>
Erlang wrapper, using solely `stdin` / `stdout` between itself and<br>
Erlang, and between itself and the "babysitted" program;<br>
=C2=A0 =C2=A0* it allows a single process to be executed at a time (contro=
lling<br>
environment, arguments, and working directory); (it could be extended<br>
to multiple process in parallel if wanted;) (but you can run it again<br>
if you want;)<br>
=C2=A0 =C2=A0* it allows a signal to be sent;<br>
=C2=A0 =C2=A0* it allows "exchange" messages to be sent to and f=
rom Erlang and<br>
the process;<br>
=C2=A0 =C2=A0* it allows brutal termination; (i.e. SIGKILL;)<br>
=C2=A0 =C2=A0* if `stdin` is closed by the Erlang side it kills the proces=
s;<br>
=C2=A0 =C2=A0* if `stdin` is closed by the process side it informs the Erl=
ang side;<br>
<br>
=C2=A0 =C2=A0The C part is a self-contained `.c` file (of about 2k lines, =
of<br>
which 25% are structure definitions), written in an event driven<br>
manner (no threads), statically linkable, depending only on the<br>
`jansson` library (for JSON), using mostly POSIX syscalls.<br>
<br>
=C2=A0 =C2=A0I've successfully used it for about a year now (in non-pr=
oduction<br>
systems) and it behaves nicely.<br>
<br>
=C2=A0 =C2=A0If someone finds it useful I could try to extract it in a<br>
standalone project.<br>
<span class=3D"HOEnZb"><font color=3D"#888888"><br>
=C2=A0 =C2=A0Ciprian.<br>
</font></span><div class=3D"HOEnZb"><div class=3D"h5"><br></div></div></blo=
ckquote><div><br>I have a similar "proxy" to run killable externa=
l programs on Windows. It's part of a larger project though, so I'd=
need to extract it. Would there be interest for me to do that?<br>
<br>The approach is:<br><br>Start the proxy program using open_port/2 and s=
end it the command to run, the proxy then uses DuplicateHandle() and Create=
ProcessW() to kick off the command in a separate OS process but with stdin/=
stdout/stderr forwarded from the proxy to the new external command, prints =
the OS process id back to Erlang and waits for the external OS process to f=
inish.<br>
<br>Thus you now have your command running in the backgroud, able to commun=
icate using stdio with the Erlang process that started the proxy.<br><br>If=
you want to stop the command, you invoke the proxy with the process id you=
got earlier and it kills it using TerminateProcess().<br>
<br>Robby<br><br>PS I vaguely remember getting the idea of passing back a p=
rocess id for later kill purposes from some open source app that uses a sim=
ilar approach to running killable commands from Erlang. Not that I can reme=
mber which one at the mooment, sorry.<br>
<br></div></div>
--14dae9340435d7775704c12aeb34--
--===============5600555872926310467==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
_______________________________________________
erlang-questions mailing list
erlang-questi...@erlang.org
http://erlang.org/mailman/listinfo/erlang-questions
--===============5600555872926310467==--