C/UNIX 每 x 毫秒执行一次函数

发布于 2024-11-07 14:02:16 字数 268 浏览 0 评论 0原文

如何使用 Alarm() 或 sleep 每 1000 毫秒执行一次函数?如果函数没有在 1000 毫秒内执行或完成,我希望程序执行其他操作。

编辑:添加了伪代码

while(true) {
alarm(1000);
execute function;
sleep(1000);
alarm(0);
}

现在如果alarm(1000)发出SIGALRM信号,我可以在哪里调用其他函数? 我对这类东西很陌生,所以甚至不确定我是否正确使用它。

How do I execute a function once every 1000 milliseconds using alarm() or sleep? I want the program to do something else if the function does not execute or complete in 1000 milliseconds.

EDIT: added Pseudocode

while(true) {
alarm(1000);
execute function;
sleep(1000);
alarm(0);
}

Now if alarm(1000) signals SIGALRM is this where I could call the other function?
I'm new to this sort of stuff so not even sure if I am using it right.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

很快妥协 2024-11-14 14:02:16

要求有多清晰,即您可以容忍多少抖动?

什么版本的 UNIX?

基本上,如果这是一个严格的截止日期(听起来像是一个截止日期),您将需要做一些特殊的事情,因为基本 UNIX 并不是真正的硬实时系统。

我们暂时假设您指的是 Linux。您需要

  • 使用nice(2)来提高进程优先级
  • 使用细粒度计时器,如 ualarm(3)

这可能会。如果您需要更细粒度或更可预测的计时,那么您可能需要编写一个内核扩展,可以用内核计时器驱动它。 (Solaris 对硬实时的支持有所改进,但它仍然不是真正的硬实时系统。)

如果您可以使用实时 Linux 或 UNIX,生活将会变得更加轻松。这是列表一些选项。这是 您可能会发现有用的文章。

更新

您还应该查看nanosleep(2)setitimer (2)。请注意,所有这些都表示间隔至少是参数中的间隔。如果您有严格的截止日期,则需要等待比实际间隔短一些的时间,然后弄清楚如何处理千毫秒内的任何变化。

How crisp is the requirement, ie, how much jitter can you tolerate?

What version of UNIX?

Basically, if this is a hard deadline -- it sounds like one -- you're going to need to do some special stuff, because basic UNIX isn't really a hard-real-time system.

Let's assume for the moment that you mean Linux. You'll want to

  • use nice(2) to raise the process priority
  • use a fine-grained timer, as with ualarm(3)

That will probably do. If you need finer grained, or more predictable, timing, then you probably need to write a kernel extension to it can be driven with a kernel timer. (Solaris has some improved support for hard-real-time, but it's still not really a hard real-time system.)

Life will get considerably easier if you can use a real-time Linux or UNIX. Here's a list of some options. Here's an article you might find usefui.

Update

You should also look at nanosleep(2) or setitimer(2). Notice that all of these say the interval is at least the one in the argument. If you have a hard deadline, you need to wait for somewhat less than the actual interval and then figure out what to do with any change on your thousand millisecs.

邮友 2024-11-14 14:02:16

我在Linux上使用这个函数以毫秒为单位“睡眠”:

void Sleep(unsigned int milliSeconds)
{
    struct timespec req = {0};

    time_t seconds  = (int) (milliSeconds / 1000);
    milliSeconds    = milliSeconds - (seconds * 1000);
    req.tv_sec      = seconds;
    req.tv_nsec     = milliSeconds * 1000000L;

    while (nanosleep(&req, &req) == -1)
        continue;
}

I have used this function on Linux to "sleep" in milliseconds:

void Sleep(unsigned int milliSeconds)
{
    struct timespec req = {0};

    time_t seconds  = (int) (milliSeconds / 1000);
    milliSeconds    = milliSeconds - (seconds * 1000);
    req.tv_sec      = seconds;
    req.tv_nsec     = milliSeconds * 1000000L;

    while (nanosleep(&req, &req) == -1)
        continue;
}
峩卟喜欢 2024-11-14 14:02:16
while (1) {
    sleep(1);
    /* Act */
}

如果您需要更严格的延迟,通常的方法是调用没有 fd 且超时时间较短的 select() 。

while (1) {
    sleep(1);
    /* Act */
}

If you need tighter delays, the normal way to do it is to call select() with no fds and a low timeout.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文