是否有等待()系统调用设置一个超时的版本? [英] Is there a version of the wait() system call that sets a timeout?

查看:152
本文介绍了是否有等待()系统调用设置一个超时的版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法使用的wait()系统调用超时,除了使用忙等待或忙睡眠循环?

Is there any way to use the wait() system call with a timeout, besides using a busy-waiting or busy-sleeping loop?

我已经得到了叉父进程取值本身和 EXEC 是个孩子的可执行文件。然后等待孩子来完成,通过一切适当手段抓住它的输出,以及并进行进一步处理。如果该过程不的一段时间内完成,则认为其执行超时,并执行别的东西。不幸的是,这个超时检测是必要的,因为该问题的性质

I've got a parent process that forks itself and execs a child executable. It then waits for the child to finish, grabs its output by whatever means appropriate, and and performs further processing. If the process does not finish within a certain period of time, it assumes that its execution timed out, and does something else. Unfortunately, this timeout detection is necessary given the nature of the problem.

推荐答案

有不等待调用了超时。

你可以做的,而不是为安装设置SIGCHLD标志的信号处理程序,并使用select()来执行超时。选择()将由信号中断。

What you can do instead is install a signal handler that sets a flag for SIGCHLD, and use select() to implement a timeout. select() will be interrupted by a signal.

static volatile int punt;
static void sig_handler(int sig)
{
    punt = 1;
}

...

struct timeval timeout = {10,0};
int rc;

signal(SIGCHLD, sig_handler);

fork/exec stuff
//select will get interrupted by a signal

rc = select(0, NULL,NULL,NULL, &tv); 
if (rc == 0) {
// timed out
} else if (punt) {
    //child terminated
}

如果你有其他的信号,你需要处理以及需要更多的逻辑,虽然

More logic is needed if you have other signal you need to handle as well though

这篇关于是否有等待()系统调用设置一个超时的版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆