下面是一些将在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确实有一个功能,做你所需要的。
当然,因为它发出了一个信号,你的孩子可能要处理,要关闭更优雅比默认的终止,但试试这个程序并运行ps
而它睡觉,看cat
运行,然后注意到猫死时父母退出。
通过同样的方法,我的意思是'scope(exit){wait(thisProcessID); kill(appPID)...' –
你想让它强行杀死它们还是让主程序保持活着直到孩子自然关闭? http://stackoverflow.com/a/23587108/1457000是一个杀的答案(相同的功能可以使用从D ....哦poop你使用高级功能,所以你不能注入该呼叫正确的地方,我需要重新考虑这一点之前发布作为答案)。你的等待将不起作用,因为spawnProcess返回一个魔术类,并且thisProcessId只返回一个int。等待期待班级。但除此之外,等待只对我认为的孩子,而不是父母... –
嗨,亚当,我不在乎如何接近,但软性更好。 –