Spawning a process, then killing it on SIGINT

mipri mipri at minimaltype.com
Sat Nov 23 10:09:51 UTC 2019


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);
}



More information about the Digitalmars-d-learn mailing list