c - C中的多个警报?

标签 c operating-system signals alarm

这可能是一个非常基本的问题,但我正在使用下面的代码来运行一个简单的警报。它按我想要的方式工作,但我想知道是否有可能同时运行多个警报,每个警报在完成时触发不同的功能。有办法吗?

#include <signal.h>
#include <sys/time.h>
#include <stdio.h>
#include <time.h>

void alarm_handler(int signum){
    printf("five seconds passed!!\n");
}
int main(){
    signal(SIGALRM, alarm_handler);
    alarm(5);

    pause();
    return 0;
}

最佳答案

没有。根据this source :

Alarm requests are not stacked; only one SIGALRM generation can be scheduled in this manner. If the SIGALRM signal has not yet been generated, the call shall result in rescheduling the time at which the SIGALRM signal is generated.

一种替代方法是创建一个 priority queue在其中放置任务,然后始终为当前时间与队列顶部任务之间的时差安排闹钟。

但一定要看this所以问题:你在信号处理程序中可以做的事情种类有限。

关于c - C中的多个警报?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44122139/

相关文章:

configuration - AUTOSAR 操作系统配置示例

eventfd 和线程的关键部分

terminal - 进程接收 SIGTTYIN/TTOU 而不是阻塞的历史原因是什么?

class - pyqt-如何为 QLabel 类创建点击信号?

c - 基本组装题

c - 播种和随机数生成

c - 我如何在 C 中检测是否插入了 linux 的 USB

linux - 为什么可以在 MacOS 上的 docker 上运行 Linux 容器

c - 修复发送信号中断系统调用时的竞争条件

c - 如何使用 C 打印给定的 ZIG ZAG 图案?