是否有等待()系统调用设置一个超时的版本?

编程入门 行业动态 更新时间:2024-10-24 20:22:53
本文介绍了是否有等待()系统调用设置一个超时的版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有没有办法使用的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

更多推荐

是否有等待()系统调用设置一个超时的版本?

本文发布于:2023-11-05 12:32:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1560852.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:版本   系统

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!