Spawning a process, then killing it on SIGINT
aliak
something at something.com
Sun Nov 24 14:41:23 UTC 2019
On Saturday, 23 November 2019 at 10:09:51 UTC, mipri wrote:
> On Saturday, 23 November 2019 at 09:54:48 UTC, aliak wrote:
>> Is there a way to go about killing a process after spawning it
>> on a SIGINT?
>>
>> I can't do this for e.g. because kill is not @nogc.
>
> Well, this works:
>
> import std;
> import core.stdc.signal;
>
> extern(C) int kill(int pid, int sig) nothrow @nogc @system;
>
> int currentSpawnedPid;
> extern(C) void killCurrentPidHandler(int sig) nothrow @nogc
> @system {
> if (currentSpawnedPid > 1)
> kill(currentSpawnedPid, sig);
> }
>
> int main() {
> auto pid = spawnProcess(["sleep", "50s"], stdin, stdout,
> stderr);
> currentSpawnedPid = pid.processID;
> signal(SIGINT, &killCurrentPidHandler);
> return wait(pid);
> }
Thanks, looks like I'll have to go that route.
More information about the Digitalmars-d-learn
mailing list