1.1 定时器-Timer
1.1.1 Timer的创建和删除
1.1.1.1 timer_create( )
#include<time.h>
#include<signal.h>
inttimer_create( clockid_t clockid, struct sigevent *event, timer_t *timerid );
timer不能够被子进程从父进程中继承,而且一旦当前进程调用了exec( ),之前所有的timer都会失效,并被删除。
成功返回0,失败返回-1。
各个系统会支持不同的clockid,但是都必须支持CLOCK_REALTIME。
参数timerid用来存放创建成功的timerID。
struct sigevent
{
int sigev_notify;
int sigev_signo; // signalnumber sent to current process when the timer expires
union sigval sigev_value; // info carried with signal
NotifyFUN sigev_notify_function; //typedef void (*NotifyFUN)( union sigval)
pthread_attr_t* sigev_notify_attributes;
}
sigev_notify的取值:
SIGEV_NONE :No notification will be delivered when the event ofinterest occurs.
SIGEV_SIGNAL :A queued signal will be generated when theevent of interest occurs.
SIGEV_THREAD :A notification function will be called toperform notification.
如果参数event是NULL,相当于:
sigev_notify = SIGEV_SIGNAL
sigev_signo =default signal number
sigev_value =timerid
1.1.1.2 timer_delete( )
#include <time.h>
int timer_delete( timer_t timerid );
成功返回0,失败返回-1。
1.1.2 Timer的运用
1.1.2.1 timer_settime()
#include<time.h>
int timer_settime
(timer_t timerid, int flag, const struct itimerspec *newValue, struct itimerspec*oldValue );
成功返回0,失败返回-1。
参数flag:TIMER_ABSTIME
如果设定,it_value指定的是绝对时间,也就是说timer将在it_value指定的时刻expires,如果it_value指定的时刻已经过了,立即成功返回,并且相当于发生了一次expires。
如果未设定,it_value指定的是相对时间,也就是说timer将在timer_settim( )调用之后,再经过it_value指定的时间之后expires。
参数newValue:
如果it_value设定为0,timer失效。
如果it_ interval设定为0,timer不重复。
参数oldvalue:
it_value返回timer离之前设定的expire还有多少时间,如果timer是失效的,返回0。
it_interval返回timer之前的reload value。
struct itimerspec
{
struct timespec it_interval; //reload value of timer
struct timespec it_value;
}
struct timespec
{
time_t tv_sec; // seconds
long tv_nsec; // nanoseconds
}
1.1.2.2 timer_gettime( )
#include<time.h>
inttimer_gettime( timer_t timerid, struct itimerspec *value );
it_value返回timer离expire还有多少时间,如果timer是失效的,返回0。
it_interval返回timer的reload value
成功返回0,失败返回-1。
1.1.2.3 timer_getoverrun( )
#include<time.h>
int timer_getoverrun( timer_t timerid );
Only one single signal will be queued tothe process for a given timer at any point in time. When a timer for which asignal is still pending expires, no signal will be queued, and a timer overrunoccurs.
成功返回timer expiration overrun count,返回的数目包括了extra timer expirations that occurred between the timesignal was generated (queued) and when it was delivered or accepted.
失败返回-1。
1.1.3 简化版的Timer
1.1.3.1 getitimer( )
#include<sys/time.h>
intgetitimer( int which, struct itimerval *value )
返回由which指定的timer的设定值。
成功返回0,失败返回-1。
1.1.3.2 setitimer( )
#include<sys/time.h>
intsetitimer( int which, const struct itimerval *newValue, struct itimerval *oldValue)
成功返回0,失败返回-1。
参数which指定定时器类型:
ITIMER_REAL:绝对时间计时,发送SIGALRM给本进程
ITIMER_VIRTUAL:进程执行时间计时,发送SIGVTALRM给本进程
ITIMER_PROF:进程执行+内核因本进程而消耗的时间,发送SIGPROF给本进程
it_value设定的是相对时间,也就是说timer将在setitimer( )调用之后,再经过it_value指定的时间之后expires。
参数newValue:
如果it_value设定为0,timer失效。
如果it_ interval设定为0,timer不重复。
参数oldvalue返回timer上次的设定值。
struct itimerval
{
struct timeval it_interval;
struct timeval it_value;
};
struct timeval
{
long tv_sec; // seconds
long tv_usec; // microseconds
};