【发布时间】:2012-02-18 23:14:51
【问题描述】:
我正在开发一个简单的软件来检查我是否能够使用我研究过的关于 POSIX 计时器和信号的知识进行编程。
我正在尝试做一个简单的程序,它启动一个计时器并在一定的纳秒内发出一个信号
下面的程序运行不好,所以我写了一些关于我的代码的cmets,这样你就可以检查我是否正确地学习了。 您可以在页面底部找到完整的代码清单。
各种印花,如
prinf("1\n") 用于检查程序过早退出的位置。
我将 struct sigevent sigeventStruct作为计时器生成的到期事件的结构。 第一个参数设置为 SIGEV_SIGNAL 所以这是它将发出的信号类型。 /// 您可以在代码清单中阅读的各种 memset 是零初始化结构。
if(timer_create(_POSIX_MONOTONIC_CLOCK, &sigeventStruct, &timer1) == -1)
是创建一个 POSIX 计时器。 POSIX MONOTONIC CLOCK 是一种计时器,&sigeventStruct 是指向结构的指针,该结构描述了它是如何由计时器到期生成的事件。 &timer1 是指向特定计时器名称的指针。
if(timer_settime(timer1, NULL, &tempoIniziale, &tempoFinale) == -1)
通过此过程,计时器已启用,因此您可以使其生成到期时间。 timer1 是计时器的名称,0 是标志。 GAPIL 书说: &tempoIniziale 和 &tempoFinale 是指向 itimerspec 结构的指针。我不太明白&old_timer是什么意思。在 GAIL 书中,您可以阅读:
struct sigaction, oldSigAzione
将作为参数传递给 sigaction POSIX 信号处理程序的 sigaction 结构
sigaction (SIGEV_SIGNAL, NULL, &oldSigAzione)
SIGEV_SIGNAL 是它必须处理的信号类型,NULL 是可以放置指向 const struct sigaction 的指针的位置,&oldSigAzione 是指向我之前提到的 sigaction struct 的指针。在这里我又没有理解这两个指向 sigaction struct 的指针之间的区别。
我的问题是: 为什么程序在打印 printf("19\n") 的数字 19 之前退出;以及为什么不执行 printf("Timer scaduto\n");函数内部无效终止处理程序(int signum)?
这是我的代码:
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <time.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/fcntl.h>
#include <sys/wait.h>
#include <stdbool.h>
void termination_handler(int signum)
{
printf("Timer scaduto\n");
}
int main()
{
printf("Start\n");
printf("1\n");
struct sigevent sigeventStruct; // sigevent struct that will be used by timer1 timer
printf("2\n");
memset(&sigeventStruct, 0, sizeof sigeventStruct); // zero initialize struct
printf("3\n");
sigeventStruct.sigev_notify = SIGEV_SIGNAL; // kind of notification of timer1 expiration
printf("4\n");
sigeventStruct.sigev_signo = 10;
printf("5\n");
timer_t timer1; // create a timer identifier
printf("6\n");
if(timer_create(_POSIX_MONOTONIC_CLOCK, &sigeventStruct, &timer1) == -1)
{
printf( "Errore timer_create: %s\n", strerror( errno ) );
}
printf("7\n");
struct itimerspec tempoIniziale;
printf("8\n");
memset(&tempoIniziale, NULL, sizeof tempoIniziale); // zero initialize struct
printf("9\n");
tempoIniziale.it_value.tv_nsec = 100000000;
//tempoIniziale.it_interval.tv_nsec = 10000;
printf("10\n");
if(timer_settime(timer1, 0, &tempoIniziale, NULL) == -1) // timer armed
{
printf( "Errore timer_settime: %s\n", strerror( errno ) );
}
printf("11\n");
for(int i = 0; i< 10; i++)
{
printf("ciclo %d\n", i);
}
struct sigaction oldSigAzione;
printf("12\n");
memset(&oldSigAzione, 0, sizeof oldSigAzione);
printf("13\n");
oldSigAzione.sa_handler = termination_handler;
printf("14\n");
sigemptyset (&oldSigAzione.sa_mask);
printf("15\n");
oldSigAzione.sa_flags = 0;
printf("16\n");
sigaction (SIGEV_SIGNAL, NULL, &oldSigAzione);
printf("17\n");
if(oldSigAzione.sa_handler == SIG_IGN)
{
printf("Segnale ignorato\n");
}
printf("18\n");
for(int i = 0; i < 1000000000000; i++)
{
}
printf("19\n");
printf("number of expirations %d\n", timer_getoverrun(timer1));
return 0;
}
【问题讨论】:
-
空的 for 循环不是延迟的方法。现代编译器将其编译为根本没有代码。试试
sleep... -
另外,
printf不是异步信号安全的。除非您在代码中避免所有可能被信号中断的异步信号不安全函数调用,否则您不能从信号处理程序中使用它。