请帮助我,我想在 ubunto 中使用 c 实现计时器。我已经编写了代码,但它给出了两个错误。我正在使用 gcc 的 -lrt 选项编译它。我得到的错误是:timer1.c:在函数'main'中:timer1.c:18:52:错误:'SIG'未声明(在此函数中首次使用)timer1.c:18:52:注意:每个未声明的标识符对于它出现在 timer1.c:21:23 中的每个函数只报告一次:错误:未声明“处理程序”(在此函数中首次使用)
我的代码是:
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <time.h>
timer_t timerid;
int main(int argc, char *argv[])
{
struct sigevent sev;
struct itimerspec its;
long long freq_nanosecs;
sigset_t mask;
struct sigaction sa;
printf("Establishing handler for signal %d\n", SIG);
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = handler;
sigemptyset(&sa.sa_mask);
sev.sigev_notify = SIGEV_SIGNAL;
sev.sigev_signo = SIG;
sev.sigev_value.sival_ptr = &timerid;
printf("timer ID is 0x%lx\n", (long) timerid);
// timer_create(CLOCKID, &sev, &timerid);
/* Start the timer */
its.it_value.tv_sec = 1000;
its.it_value.tv_nsec =0;
its.it_interval.tv_sec = its.it_value.tv_sec;
its.it_interval.tv_nsec = its.it_value.tv_nsec;
timer_settime(timerid,0, &its, NULL);
sleep(10);
}
static void handler(int sig, siginfo_t *si, void *uc)
{
if(si->si_value.sival_ptr != &timerid)
{
printf("Stray signal\n");
}
else
{
printf("Caught signal from timer\n");
}
}