다음은 Linux에서 수행 할 수있는 몇 가지 코드입니다. 그것은 stdlib의 spawnProcess의 모든 기능을 가지고 있지는 않습니다. 단지 기본만을 보여 주지만, 더 많이 필요하다면 여기에서 확장하는 것은 어렵지 않습니다.
import core.sys.posix.unistd;
version(linux) {
// this function is Linux-specific
import core.stdc.config;
import core.sys.posix.signal;
// we can tell the kernel to send our child process a signal
// when the parent dies...
extern(C) int prctl(int, c_ulong, c_ulong, c_ulong, c_ulong);
// the constant I pulled out of the C headers
enum PR_SET_PDEATHSIG = 1;
}
pid_t mySpawnProcess(string process) {
if(auto pid = fork()) {
// this branch is the parent, it can return the child pid
// you can:
// import core.sys.posix.sys.wait;
// waitpid(this_ret_value, &status, 0);
// if you want the parent to wait for the child to die
return pid;
} else {
// child
// first, tell it to terminate when the parent dies
prctl(PR_SET_PDEATHSIG, SIGTERM, 0, 0, 0);
// then, exec our process
char*[2] args;
char[255] buffer;
// gotta copy the string into another buffer
// so we zero terminate it and have a C style char**...
buffer[0 .. process.length] = process[];
buffer[process.length] = 0;
args[0] = buffer.ptr;
// then call exec to run the new program
execve(args[0], args.ptr, null);
assert(0); // never reached
}
}
void main() {
mySpawnProcess("/usr/bin/cat");
// parent process sleeps for one second, then exits
usleep(1_000_000);
}
그래서 하위 수준 기능을 사용해야하지만 Linux는 필요한 기능을 수행합니다. 물론
,이 신호를 송신 한 후, 자녀가 기본 종료보다 더 우아하게 종료하지만,이 프로그램을 시도하고 cat
실행을 볼 수자는 동안 ps
을 실행 한 후 고양이가 때 사망 통지하는 것을 처리 할 수 있습니다 부모가 종료합니다.
동일한 접근법으로 'scope (exit) {wait (thisProcessID); 죽여라. (appPID) ... ' –
강제로 죽이기를 원 하시겠습니까? 아니면 아이들이 자연스럽게 닫힐 때까지 메인 프로그램을 살아있게 하시겠습니까? http://stackoverflow.com/a/23587108/1457000 죽이기에 대한 대답입니다 (동일한 기능을 D에서 사용할 수 있습니다 .... 오, 당신은 높은 수준의 기능을 사용하고 있으므로 전화를 할 수 없습니다. 올바른 장소. 대답으로 게시하기 전에 이것을 재고해야합니다.) spawnProcess가 마법 클래스를 반환하고 thisProcessId가 int를 반환하기 때문에 기다림이 작동하지 않습니다. 기다리는 동안 수업이 기다리고 있습니다. 그러나, 기다리는 것 외에도, 부모님이 아니라, 생각하는 아이들에게만 작품을 기다리십시오 ... –
안녕하세요, 아담, 나는 가까이에 어떻게 신경 쓰이지 만, 부드러운 것이 좋습니다. –