#include <time.h>
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
/*
This function is called periodically.
The actual functionality has to be provided by yourself **/
void my_handler ( timer_t timerid)
{
printf("\nHello World\n");
}
int main()
{
// declare some variables
timer_t timerid;
struct itimerspec value;
struct sigevent sevp;
// initalize the sigevent variable
// the memset is necessary to avoid segmentation faults
memset (&sevp, 0, sizeof (struct sigevent));
sevp.sigev_notify=SIGEV_THREAD;
sevp.sigev_notify_function=(void *)my_handler;
// initialize the timer specification
// currently the offset and the period is 5 seconds // you have to adjust this to your actual
//needs
value.it_value.tv_sec = 5;
value.it_value.tv_nsec = 0;
value.it_interval.tv_sec = 5;
value.it_interval.tv_nsec = 0;
// create the timer
timer_create (CLOCK_REALTIME, &sevp, &timerid);
timer_settime (timerid, 0, &value, NULL);
// this loop has to be adapted, so that the program ends after some time
while(1)
{
sleep(100);
}
return 0;
}
我创建了一个计时器,用于每5秒调用一次任务并打印为hello
。我是否想为另一个任务创建另一个计时器(每10ms)?如何在这种情况下创建一个处理程序?如何计算执行任务的开始时间和停止时间??