Http://blog.sina.com.cn/s/blog_590be5290100izdf.html
Usage:
#include <sys/time.h>
int Getitimer (int which, struct itimerval *value);
int Setitimer (int which, const struct Itimerval *value, structitimerval *ovalue);
Function Description:
Gets or sets the value of the intermittent timer. The system provides three types of timers for the process, each of which decrements its value in different time domains. When the timer expires, the signal is sent to the process, and then the timer is restarted.
Parameters:
Which: Intermittent timer type with three options
The Itimer_real//value is 0, calculated in the real time of the system, and the signal sent is SIGALRM.
The itimer_virtual//value is 1, which is calculated as the time spent by the process in the user state, and the signal sent is SIGVTALRM.
The Itimer_prof//value is 2, which is calculated as the time taken by the process in the user state and the kernel state, and the signal sent is sigprof.
Value,ovalue: Time parameter, prototype as follows
struct Itimerval {
Structtimeval It_interval;
Structtimevalit_value;
};
struct Timeval {
Longtv_sec;
Longtv_usec;
};
The it_value in the itimerval structure is the reduced time, which is signaled when the value is 0. The it_value is then set to the It_interval value. That is, the value set in the It_value is processed first, and the signal is sent after 0 ( According to the which to determine what signal to send, then all are based on the value of It_interval to send a signal. If the It_value is 0, there is no corresponding signal generated.
Getitimer () fills in the structure that value points to with the current value of the timer.
Setitimer () sets the struct that value points to the current value of the timer, and returns the original value of the timer if Ovalue is not null.
Return Description:
When executed successfully, returns 0. The failed return -1,errno is set to one of the following values
Efault:value or ovalue is not a valid pointer
EINVAL: Its value is not one of itimer_real,itimer_virtual or itimer_prof
Example:
1 voidSigroutine (intSigno)2 {3 Static intcount1=0, count2=0;4 5 Switch(Signo)6 {7 CaseSIGALRM:8count1++;9printf"Catch A signal--SIGALRM \ n");Tenprintf"count1 =%d\n\n", count1); One signal (SIGALRM, sigroutine); A Break; - CaseSIGVTALRM: -count2++; theprintf"Catch A signal--SIGVTALRM \ n"); -printf"Count2 =%d\n\n", count2); - signal (SIGVTALRM, sigroutine); - Break; + } - return; + } A at - - intMain () - { - structitimerval value, Ovalue, value2; -printf"process ID is%d\n", Getpid ()); in - signal (SIGALRM, sigroutine); to signal (SIGVTALRM, sigroutine); + - the *Value.it_value.tv_sec =2; $Value.it_value.tv_usec =0;Panax NotoginsengValue.it_interval.tv_sec =3; -Value.it_interval.tv_usec =0; theSetitimer (Itimer_real, &value, &ovalue); + A the +Value2.it_value.tv_sec =1; -Value2.it_value.tv_usec =0; $Value2.it_interval.tv_sec =3; $Value2.it_interval.tv_usec =0; -Setitimer (Itimer_virtual, &value2, &ovalue); - the - Wuyi while(1); the}