主函数中未调用信号处理程序
我正在尝试研究信号处理程序是如何工作的。我编写了代码,使警报信号每 100us 发出一次。但是,信号没有发出。这是代码:
#include <signal.h>
#include <ucontext.h>
#include <sys/time.h>
#include<unistd.h>
#include<setjmp.h>
#include<stdio.h>
void handler(int signum, siginfo_t *ptr, ucontext_t *old_context)
{
printf("inside handler");
}
int main()
{
struct itimerval itv;
struct sigaction act;
act.sa_handler = handler;
act.sa_flags=SA_RESTART|SA_SIGINFO;
sigaction(SIGVTALRM, &act, 0);
itv.it_interval.tv_sec=0;
itv.it_interval.tv_usec=100;
itv.it_value.tv_sec = 0;
itv.it_value.tv_usec = 100;
setitimer(ITIMER_VIRTUAL, &itv, NULL); //engage timer
int i=0;
while(i<=100)
{
printf("main\n");
i++;
}
}
有人可以解释一下我做错了什么吗?
谢谢
I am trying to study how signal handlers work. I have written code where i cause an alarm signal to raise once in every 100us. But, the signal is not raised. Here is the code :
#include <signal.h>
#include <ucontext.h>
#include <sys/time.h>
#include<unistd.h>
#include<setjmp.h>
#include<stdio.h>
void handler(int signum, siginfo_t *ptr, ucontext_t *old_context)
{
printf("inside handler");
}
int main()
{
struct itimerval itv;
struct sigaction act;
act.sa_handler = handler;
act.sa_flags=SA_RESTART|SA_SIGINFO;
sigaction(SIGVTALRM, &act, 0);
itv.it_interval.tv_sec=0;
itv.it_interval.tv_usec=100;
itv.it_value.tv_sec = 0;
itv.it_value.tv_usec = 100;
setitimer(ITIMER_VIRTUAL, &itv, NULL); //engage timer
int i=0;
while(i<=100)
{
printf("main\n");
i++;
}
}
can some one explain what i am doing wrong?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的循环运行时间可能不到 100us,试试这个:
我删除了 printf,这样输出就不会被淹没,并使 i 变得易失,这样编译器就不会优化循环。
Your loop is probably taking less than 100us to run, try this:
I removed the printf so the output is not flooded, and made i volatile so the compiler won't optimize the loop.