[英]Killing a child process if it takes too much time
When using the fork
system call in C++, what is the easiest way to kill a child process if it takes too much time to execute what it is supposed to execute? 在C ++中使用
fork
系统调用时,如果执行它应该执行的内容需要太多时间,那么杀死子进程的最简单方法是什么?
Like if somehow it gets into an infinite loop.. What should the parent process do to set the timeout for the child process? 就像它以某种方式进入无限循环一样。父进程应该怎样设置子进程的超时?
Use WNOHANG
with waitpid
and sleep in between. 使用带有
waitpid
WNOHANG
并在其间睡觉。 Something like this should do it: 这样的事情应该这样做:
while (times < max_times) {
sleep(5); /* sleep 5 seconds */
rc = waitpid(-1, &status, WNOHANG);
if (rc < 0) {
perror("waitpid");
exit(1);
}
if (WIFEXITED(status) || WIFSIGNALED(status)) {
/* it's done */
break;
}
times++;
}
if (times == max_times) {
/* ... */
}
I think you need waitpid
with timeout and on timeout kill
child process (assuming that child is hung). 我认为你需要
waitpid
超时和超时kill
子进程(假设孩子挂了)。 Check this page for ideas: Waitpid equivalent with timeout? 检查此页面以获取建议: Waitpid等效于超时?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.