linux下setitimer的使用
时间:2010-11-21 来源:shenhailuanma
getitimer, setitimer - get or set value of an interval timer
SYNOPSIS
#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);
DESCRIPTION
The system provides each process with three interval timers, each decrementing in a distinct time domain. When any timer expires, a signal is
sent to the process, and the timer (potentially) restarts.
系统为进程提供三种类型的计时器,每一类以不同的时间域递减其值。当计时器到时,信号被发送给进程,之后计时器重启动。
ITIMER_REAL decrements in real time, and delivers SIGALRM upon expiration.以系统真实的时间来计算,计时器到时后发送信号SIGALRM。
ITIMER_VIRTUAL decrements only when the process is executing, and delivers SIGVTALRM upon expiration.只以该进程在执行的时间来计算,计时器到时后发送信号SIGVTALRM。
ITIMER_PROF decrements both when the process executes and when the system is executing on behalf of the process. Coupled with ITIMER_VIRTUAL,
this timer is usually used to profile the time spent by the application in user and kernel space. SIGPROF is delivered upon expi‐
ration.
Timer values are defined by the following structures:
struct itimerval {
struct timeval it_interval; /* next value间隔时间 */
struct timeval it_value; /* current value第一次发送信号时间 */
};
struct timeval {
long tv_sec; /* seconds */
long tv_usec; /* microseconds */
};
The function getitimer() fills the structure pointed to by curr_value with the current setting for the timer specified by which (one of
ITIMER_REAL, ITIMER_VIRTUAL, or ITIMER_PROF). The element it_value is set to the amount of time remaining on the timer, or zero if the timer is
disabled. Similarly, it_interval is set to the reset value.
The function setitimer() sets the specified timer to the value in new_value. If old_value is non-NULL, the old value of the timer is stored
there.
Timers decrement from it_value to zero, generate a signal, and reset to it_interval. A timer which is set to zero (it_value is zero or the timer
expires and it_interval is zero) stops.
Both tv_sec and tv_usec are significant in determining the duration of a timer.
Timers will never expire before the requested time, but may expire some (short) time afterwards, which depends on the system timer resolution and
on the system load; see time(7). (But see BUGS below.) Upon expiration, a signal will be generated and the timer reset. If the timer expires
while the process is active (always true for ITIMER_VIRTUAL) the signal will be delivered immediately when generated. Otherwise the delivery will
be offset by a small time dependent on the system loading.
RETURN VALUE
On success, zero is returned. On error, -1 is returned, and errno is set appropriately.
例子:
下面函数的作用是设置间隔定时器,自定义显示文件内容的速度,例如执行./test /etc/passwd把文件passwd中的内容按自定义的速度显示出来。
#include <stdio.h> |