# Include <time. h>
# Include <signal. h>
# Include <stdio. h>
# Include <stdlib. h>
# Include <unistd. h>
# Include <string. h>
Void handle (Union sigval V)
{
Time_t T;
Char P [32];
Time (& T );
Strftime (p, sizeof (P), "% t", localtime (& T ));
Printf ("% s thread % lu, val = % d, signal captured. \ n", P, pthread_self (), V. sival_int );
Return;
}
Void handlec (Union sigval v) // The processing function called when Timer 2 generates a signal
{
Time_t T;
Char P [32];
Time (& T );
Strftime (p, sizeof (P), "% t", localtime (& T ));
Printf ("% s thread % lu, val = % d, signal captured. \ n", P, pthread_self (), V. sival_int );
Return;
}
Int main ()
{
Struct sigevent EVP, EVC;
Struct itimerspec ts, TSC;
Timer_t timer, timerc;
Int ret, RETC;
/* -------------------- Timer 2 ---------------------------------------*/
Memset (& EVC, 0, sizeof (EVC ));
EVC. sigev_value.sival_ptr = & timerc;
EVC. sigev_notify = sigev_thread;
EVC. sigev_policy_function = handlec;
EVC. sigev_value.sival_int = 2; // as the handle () parameter
RETC = timer_create (clock_realtime, & EVC, & timerc );
If (RETC)
Perror ("timerc_create ");
TSC. it_interval. TV _sec = 5;
TSC. it_interval. TV _nsec = 0;
TSC. it_value. TV _sec = 3;
TSC. it_value. TV _nsec = 0;
RETC = timer_settime (timerc, timer_abstime, & TSC, null );
If (RETC)
Perror ("timerc_settime ");
/* --------------------- Timer 4 -------------------------------------*/
Memset (& EVP, 0, sizeof (EVP ));
EVP. sigev_value.sival_ptr = & timer;
EVP. sigev_notify = sigev_thread;
EVP. sigev_policy_function = handle;
EVP. sigev_value.sival_int = 4; // as the handle () parameter
Ret = timer_create (clock_realtime, & EVP, & timer );
If (RET)
Perror ("timer_create ");
TS. it_interval. TV _sec = 1;
TS. it_interval. TV _nsec = 0;
TS. it_value. TV _sec = 3;
TS. it_value. TV _nsec = 0;
Ret = timer_settime (timer, timer_abstime, & TS, null );
If (RET)
Perror ("timer_settime ");
While (1 );
}