Linux POSIX 定時器 (timer_create)
阿新 • • 發佈:2019-01-13
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <time.h>
#include <string.h>
//#define CLOCKID CLOCK_REALTIME
#define CLOCKID CLOCK_MONOTONIC
#define SIG SIGRTMIN //實時訊號,可靠訊號
#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \
} while (0)
static void
print_siginfo(siginfo_t *si)
{
timer_t *tidp;
int or;
tidp = si->si_value.sival_ptr;
printf(" sival_ptr = %p; ", si->si_value.sival_ptr);
printf(" *sival_ptr = 0x%lx\n", (long) *tidp);
or = timer_getoverrun(*tidp);
if (or == -1)
errExit("timer_getoverrun");
else
printf(" overrun count = %d\n", or);
}
static void
handler(int sig, siginfo_t *si, void *uc)
{
/* Note: calling printf() from a signal handler is not
strictly correct, since printf() is not async-signal-safe;
see signal(7)
*/
printf("Caught signal %d\n", sig);
print_siginfo(si);
//signal(sig, SIG_IGN);
}
static void
handler2(union sigval v)
{
timer_t *tidp;
tidp=v.sival_ptr;
printf("Caught handler2 0x%lx\n",*(long *)tidp);//列印傳遞的引數
}
typedef void (*sigHandler)(int , siginfo_t *, void *);
typedef void (*pthHandler)(union sigval);
//定時器通知方式為訊號的處理方式
int testTimerSign(timer_t *pTimerid, struct itimerspec ts, int sig, sigHandler handler){
struct sigevent evp;
struct sigaction sa;
int ret=0;
printf("Establishing handler for signal %d\n", SIG);
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = handler;
sigemptyset(&sa.sa_mask);
//安裝訊號處理函式
if (sigaction(sig, &sa, NULL) == -1)
{
perror("testTimerSign:sigaction");
ret=-1;
}
memset(&evp,0,sizeof(evp));
evp.sigev_notify =SIGEV_SIGNAL;
evp.sigev_signo = sig;
evp.sigev_value.sival_ptr = pTimerid; //傳遞的引數,指標型
ret = timer_create(CLOCKID, &evp, pTimerid);
if(ret) {
perror("timer_create");
}
printf("timer ID is 0x%lx\n", (long) *pTimerid);
// ts.it_value.tv_sec = 1;
// ts.it_value.tv_nsec = 10000000;
// ts.it_interval.tv_sec = its.it_value.tv_sec;
// ts.it_interval.tv_nsec = its.it_value.tv_nsec;
printf("testTimerSign:start timer\n");
ret = timer_settime(*pTimerid, 0, &ts, NULL);
if(ret) {
perror("timer_settime\n");
}
return ret;
}
//定時器通知方式為執行緒方式
int testTimerThread(timer_t *pTimerid,struct itimerspec its,pthHandler handler)
{
struct sigevent sev;
int ret=0;
memset(&sev,0,sizeof(sev));
sev.sigev_notify = SIGEV_THREAD;
sev.sigev_notify_function=handler;//處理函式
sev.sigev_value.sival_ptr = pTimerid;//傳遞給處理函式的引數,指標型
//sev.sigev_value.sival_int=timerid;
if (timer_create(CLOCKID, &sev, pTimerid) == -1)
{
perror("timer_create\n");
ret=-1;
}
printf("timer ID is 0x%lx\n", (long) *pTimerid);
// its.it_value.tv_sec = 1;
// its.it_value.tv_nsec = 10000000;
// its.it_interval.tv_sec = its.it_value.tv_sec;
// its.it_interval.tv_nsec = its.it_value.tv_nsec;
if (timer_settime(*pTimerid, 0, &its, NULL) == -1)
{
perror("timer_settime\n");
ret=-1;
}
return ret;
}
int
main(int argc, char *argv[])
{
timer_t timerid1,timerid2;
struct itimerspec ts1,ts2;
if (argc != 2) {
fprintf(stderr, "Usage: %s <sleep-secs>\n",
argv[0]);
exit(EXIT_FAILURE);
}
ts1.it_value.tv_sec = 1; //定時器初次觸發時間
ts1.it_value.tv_nsec = 0;
ts1.it_interval.tv_sec = ts1.it_value.tv_sec; //定時器週期性間隔時間
ts1.it_interval.tv_nsec = ts1.it_value.tv_nsec;
ts2.it_value.tv_sec = 4; //定時器初次觸發時間
ts2.it_value.tv_nsec = 0;
ts2.it_interval.tv_sec = 2; //定時器週期性間隔時間
ts2.it_interval.tv_nsec = 0;
testTimerSign(&timerid1,ts1,SIG,handler);
testTimerThread(&timerid2,ts2,handler2);
/*
*
* Sleep for a while; meanwhile, the timer may expire
multiple times
*/
printf("Sleeping for %d seconds\n", atoi(argv[1]));
while(1)
sleep(atoi(argv[1]));
exit(EXIT_SUCCESS);
}