The following is an excerpt from the man file under Linux: (Man Getitimer)
#include <sys/time.h>
int Getitimer (int which, struct itimerval * curr_value);
int Setitimer (int which, const struct itimerval * new_value, struct itimerval * old_value);
Describe:
Linux systems provide three interval timers per process, each decreasing in different time domains.
When any timer expires, a signal is sent to the process, and the timer (possibly) restarts.
Itimer_real: Real-time decrement to send SIGALRM signal;
Itimer_virtual: decrements only if the process is executing and can provide SIGVTALRM after expiration
Itimer_prof: When the process is executed or the system is scheduled for the process, reduce the count, time to, emit sigprof signal, this and itimer_virtual Union, commonly used to calculate the system kernel time and user time.
The value of the timer is defined in the following result body:
struct Itimerval
{
struct Timerval it_interval; Next value;
struct Timerval it_value; Current value;
};
struct Timeval
{
Long tv_sec; Seconds
Long tv_usec; microseconds;
}
It_interval is used to specify how often the task is performed, and how long It_value is used to save the current time from the execution of the task. For example, you specify a it_interval of 2 seconds (microseconds is 0), the beginning of the time we put It_value is also set to 2 seconds (microseconds 0), when a second, It_value reduced to 1, and then 1 seconds, then it_value reduced 1, into 0, This time the signal (tell the user time, can perform the task), and the system automatically resets the It_value time to the value of It_interval, that is, 2 seconds, and then re-count.
1#include <sys/time.h>2#include <stdio.h>3#include <unistd.h>4#include <signal.h>5#include <string.h>6 Ten //callback function for receiving signal One voidPrompt_info (intSigno) A { -printf"Hello world\n"); - } the - //establishing a signal processing mechanism - voidinit_sigaction () - { + structsigaction tact; - //this process receives the signal to execute the processing function Prompt_info +Tact.sa_handler =Prompt_info; ATact.sa_flags =0; at - //initializing the signal set -Sigemptyset (&tact.sa_mask); - - //establishing a signal processing mechanism -Sigaction (SIGALRM, &tact, NULL); in } - to voidinit_time () + { - //set the time interval for the task to execute at 2 seconds 0 microseconds the structitimerval value; *Value.it_value.tv_sec =2; $Value.it_value.tv_usec =0;Panax Notoginseng - //set the initial time count to 2 seconds and 0 microseconds theValue.it_interval =Value.it_value; + A //Set Timer itimer_real theSetitimer (Itimer_real, &value, NULL); + } - $ intMain () $ { - init_sigaction (); the - init_time ();Wuyi while(1) the ; - Wu return 0; -}
With the itmer_real timer, a SIGALRM signal is sent every 2 seconds, and when the main function receives the signal, the signal processing function is prompt_info in the output time is running out of the string.
For the use of itimer_virtual and itimer_prof similar, in the Setitimer set the timer for the itimer_virtual, and sigaction inside the SIGALRM changed to Sigvtalarm,
And itimer_prof corresponds to Sigprof.
1 /*2 * Timed by the way you calculate the time difference3 * Get accurate calculation time difference, change time function to Gettimeofday4 */5 6#include <signal.h>7#include <string.h>8#include <unistd.h>9#include <time.h>Ten#include <stdio.h> One A Statictime_t lasttime; - - voidShow_msg (intSigno) the { -printf"Hello world\n"); - } - + intMain () - { + structSigaction Act; A Union Sigval TSval; atAct.sa_handler =show_msg; -Act.sa_flags =0; - -Sigemptyset (&act.sa_mask); -Sigaction ( -, &Act, NULL); -Time (&lasttime); in - while(1) to { + time_t nowtime; - //Get current Time theTime (&nowtime); * $ if(nowtime-lasttime>=2)Panax Notoginseng { -Sigqueue (Getpid (), -, tsval); theLasttime =Nowtime; + } A } the + return 0; -}