GETITIMER(2) Linux Programmer?. Manual GETITIMER(2)
LINUX 编程手册
NAME
getitimer, setitimer - get or set value of an interval timer
getitimer, setitimer - 获取或设置一个间隔定时器
SYNOPSIS
#include <sys/time.h>
int getitimer(int which, struct itimerval *value);
int setitimer(int which, const struct itimerval *value,
struct itimerval *ovalue);
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 expira-
tion.
只有在进程执行时并且系统在执行时减少,
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 indicated by value with the current setting for the timer indicated by which (one of ITIMER_REAL,
ITIMER_VIRTUAL, or ITIMER_PROF).
getitimer函数的定时器结构由which所指定的定时器类型定时器设置
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 indicated timer to the value in value. If ovalue is non-zero, the old value of the timer is stored there.
元素 it_value被设置成定时器剩下的时间的总量,0表示定时器无效,类似的,it_interval被设置为定时器从新启动的值;setitimer设置定时器值到定时器中,若第三个参数为非空,则先前的定时器设置的值被存储在那里;
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.
定时器从it_value减小到0, 然后产生一个信号,然后从新设置it_interval, 一个定时器若被设置为0,it_value或者it_interval被置0,则定时器则停止工作。
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. (But see BUGS below.) Upon expiration, a signal will be generated and the timer reset. If the timer expires while the pro-
cess 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.
成功返回0, 失败返回-1
ERRORS
EFAULT value or ovalue are not valid pointers.
EINVAL which is not one of ITIMER_REAL, ITIMER_VIRTUAL, or ITIMER_PROF.
NOTES
A child created via fork(2) does not inherit its parent?. interval timers. Interval timers are preserved across an execve(2).
一个子进程通过fork函数不能从父进程哪里继承定时器。
CONFORMING TO
POSIX.1-2001, SVr4, 4.4BSD (this call first appeared in 4.2BSD).
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/time.h>
#include <signal.h>
void print_msg(int iNum)
{
printf("hello kitty\n");
return;
}
int main()
{
int iRet = -1;
struct itimerval tick;
tick.it_interval.tv_sec = 1;
tick.it_interval.tv_usec = 0;
tick.it_value.tv_sec = 1;
tick.it_value.tv_usec = 0;
signal(SIGALRM, print_msg);
iRet = setitimer(ITIMER_REAL, &tick, NULL);
if ( 0 != iRet )
{
return -1;
}
printf("Wait...\n");
getchar();
return 0;
}
执行结果:
[root@active test]# ./a.out
Wait...
hello kitty
hello kitty
hello kitty
hello kitty
hello kitty
hello kitty
hello kitty
chello kitty
hello kitty
[root@active test]#
修改上述任一值为0:
struct itimerval tick;
tick.it_interval.tv_sec = 1;
tick.it_interval.tv_usec = 0;
tick.it_value.tv_sec = 0;
tick.it_value.tv_usec = 0;
等若干时间没有任何打印;
[root@active test]# ./a.out
Wait...
修改:
struct itimerval tick;
tick.it_interval.tv_sec = 0;
tick.it_interval.tv_usec = 0;
tick.it_value.tv_sec = 1;
tick.it_value.tv_usec = 0;
打印一次,后面在没有打印
[root@active test]# gcc settimer.c
[root@active test]# ./a.out
Wait...
hello kitty