如何限制在C / POSIX的函数的执行时间? [英] How to limit the execution time of a function in C/POSIX?

查看:169
本文介绍了如何限制在C / POSIX的函数的执行时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与此类似<一个href=\"http://stackoverflow.com/questions/366682/how-to-limit-execution-time-of-a-function-call-in-python\">question,我想限制功能的执行时间 - preferably用微秒精度的 - 在C.我想,C ++异常可用于实现类似的结果为<一个href=\"http://stackoverflow.com/questions/366682/how-to-limit-execution-time-of-a-function-call-in-python/366763#366763\">this Python的解决方案。虽然它的效果并不理想,这样的做法是纯C完全不可用。

Similar to this question, I'd like to limit the execution time of a function--preferably with microsecond accuracy--in C. I imagine that C++ exceptions could be used to achieve a result similar to this Python solution. Though it's not ideal, such an approach is wholly unavailable in plain C.

我不知道的话,我怎么可能会中断功能在C Posix的系统上一定的时间间隔后执行?对于比较简单的情况有点傻业务工作得很好,但增加了$相当数量的C $ç垂直于问题的解决。比方说,我有一个函数,像这样:

I wonder, then, how might I interrupt the execution of a function after a certain time interval in C on a Posix system? For relatively simple situations a bit of silly business works just fine, but that adds a fair amount of code orthogonal to the problem solution. Let's say I have a function like so:


void boil(egg *e) {
    while (true)
    do_boil(e);
}

我要一个鸡蛋*都跑熬,打断了它的每一个50μs的检查做类似这样:

I want to run run boil on an egg*, interrupting it every 50μs to check do something like so:


egg *e = init_egg();
while (true) {
    preempt_in(50, (void) (*boil), 1, e);
    /* Now boil(e) is executed for 50μs, 
       then control flow is returned to the
       statement prior to the call to preempt_in.
     */
    if (e->cooked_val > 100)
        break;
}

我意识到pthreads的可用于做到这一点,但我宁愿更感兴趣的是避免其使用。我的可能的ucontext_t在一个SIGALRM的处理程序,但POSIX标准音符之间切换,使用setcontext / swapcontext的是不要在信号处理程序使用,而事实上,我注意到Linux和Solaris之间的不同行为这样,当系统。

I realize that pthreads could be used to do this, but I'm rather more interested in avoiding their use. I could switch between ucontext_t's in a SIGALRM handler, but the POSIX standard notes that the use of setcontext/swapcontext is not to be used in a signal handler and, indeed, I note differing behaviors between Linux and Solaris systems when doing so.

这是效果的可能的实现?如果是这样,在一个可移植的方式?

Is this effect possible to achieve? If so, in a portable manner?

推荐答案

只是要注意,你正在寻找这里的一般功能被称为的执法成本的。例如看到这篇文章由威林斯或在<一个href=\"http://books.google.com/books?id=Bz7DTDZQaGIC&lpg=SA12-PA9&ots=pHnUyFtZ55&dq=real-time%20operating%20system%20%22cost%20enforcement%22&pg=SA12-PA9#v=onepage&q&f=false\"相对=nofollow>梁等。人的伟大的书。答案以上都集中在用户空间实现它;一些RTOS或语言支持(而不是Linux)作为一般机制。

Just to note that the general capability you're looking for here is called cost enforcement. For example see this article by Wellings or in Leung, et. al.'s great book. The answers above are focused on achieving it in userspace; some RTOS or languages support it (not linux) as a general mechanism.

一个例子OS提供,这是 AUTOSAR OS(规格链接)。请注意,该操作系统提供的执行时间,执行的,这是从的期限执行。执行时间执行状态越来越难,因为它依赖于测量实际成本一定的能力消耗略有不同(通常与硬件合作)。随着今天的处理器的复杂性,它的难度和成本来衡量这一&MDASH;更不用说测量(由于非线性执行和各种其他很酷的东西)的含义是很难间preT&MDASH;因而难以事件来计算的code的执行某一特定时间段的(最差或共)情况的估计。

One example OS that provides this is the AUTOSAR OS (spec linked). Note that this OS provides execution time enforcement, which is slightly different from deadline enforcement. Execution time enforcement keeps getting harder, as it relies on some ability to measure the actual cost expended (usually with hardware cooperation). With the complexity of today's processors, it's difficult and costly to measure this — not to mention that the meaning of the measurements (due to nonlinear execution and all sorts of other cool stuff) are hard to interpret — and thus difficult event to compute a (worst- or common-) case estimate of a particular section of code's execution time.

稍微偏离主题,但阿达提供了更加严格的一套在语言级别这里,这不利于你的能力,但你可以弄清楚如何将这些阿达要求已在Linux中得到落实。 Ada语言规范是提供一个理由独特的文件,请参见上的实时preemptive中止作为出发点。

Slightly off-topic, but Ada provides a more rigorous set of capabilities at the language level here, which doesn't help you, but you could figure out how these Ada requirements have been implemented in Linux. The Ada language spec is unique in providing a rationale document, see the section on real-time preemptive abort as a departure point.

这篇关于如何限制在C / POSIX的函数的执行时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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