create a timer
#include <signal.h> #include <time.h> timer_t timer_create( clockid_t clock_id, struct sigevent *evp );
The timer_create() function creates a per-process timer using the specified clock source, clock_id, as the timing base. The only supported clock id is CLOCK_REALTIME.
The returned timer id is used in subsequent calls to timer_gettime(), timer_settime() and timer_delete().
The timer is created in the disabled state, and is not enabled until timer_settime() is called.
The sigevent structure pointed to by evp contains at least the following member:
If sigev_signo is negative then it's interpreted as a proxy to trigger when the timer expires. If evp is NULL, then a signal of SIGALRM is set when the timer expires.
If sigev_signo is 0, the process sleeps until the timer expires.
If you need a timer that fires off every 50 milliseconds and you wish to execute some code on each pulse, the qnx_hint_attach() function is a more efficient, but less portable, alternative.
The timer_create() function returns a timer ID (small positive integer) on success. On error, it returns (-1), and errno is set.
clock_getres(), clock_gettime(), clock_setres(), clock_settime(), errno, nanosleep(), sleep(), timer_delete(), timer_gettime(), timer_settime(), ticksize utility
#include <stdio.h> #include <signal.h> #include <time.h> #include <sys/proxy.h> #include <sys/kernel.h> void main() { pid_t proxy; timer_t id, i; struct itimerspec timer; struct sigevent event; /* Get a proxy for the timer to kick */ proxy = qnx_proxy_attach( 0, 0, 0, -1 ); if( proxy == -1 ) { printf( "Unable to attach proxy." ); return; } /* Attach to the timer */ event.sigev_signo = -proxy; id = timer_create( CLOCK_REALTIME, &event ); if( id == -1 ) { printf( "Unable to attach timer." ); return; } /* * 4 seconds before initial firing , * 1 second repetitive timer afterwards. */ timer.it_value.tv_sec = 4L; timer.it_value.tv_nsec = 0L; timer.it_interval.tv_sec = 1L; timer.it_interval.tv_nsec = 0L; timer_settime( id, 0, &timer, NULL ); /* Wait for the proxy */ for( i = 0 ; i < 10 ; ++i ) { Receive( proxy, 0, 0 ); printf( "Tick.\n" ); } /* Set an absolute timer to go off in 20 sec */ timer.it_value.tv_sec = time( NULL ) + 20; timer.it_value.tv_nsec = 0L; timer.it_interval.tv_sec = 0L; timer.it_interval.tv_nsec = 0L; timer_settime( id, TIMER_ABSTIME, &timer, NULL ); /* Clear pending proxies */ while( Creceive( proxy, 0, 0 ) == proxy ) ; /* * Count down as we wait for next proxy * ( about 20 seconds ) */ while( Creceive( proxy, 0, 0 ) != proxy ) { timer_gettime( id, &timer ); printf( "Remaining %2ld\r", timer.it_value.tv_sec ); } printf( "\n" ); timer_delete( id ); }
POSIX 1003.4
QNX