linux 关闭setitimer_linux中setitimer函数用法

(一) 函数原型:

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.

ITIMER_VIRTUAL decrements only when the process is executing, and delivers SIGVTALRM upon expiration.

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.  SIG-

PROF is delivered upon expiration.

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 */

};

参数:

which:间歇计时器类型,有三种选择

ITIMER_REAL //数值为0,计时器的值实时递减,发送的信号是SIGALRM。

ITIMER_VIRTUAL //数值为1,进程执行时递减计时器的值,发送的信号是SIGVTALRM。

ITIMER_PROF //数值为2,进程和系统执行时都递减计时器的值,发送的信号是SIGPROF。

value,ovalue:时间参数,原型如下

struct itimerval {

struct timeval it_interval; /* 计时器重启动的间歇值 */

struct timeval it_value;    /* 计时器安装后首先启动的初始值 */

};

struct timeval {

long tv_sec;                /* 秒 */

long tv_usec;               /* 微妙(1/1000000) */

};

getitimer()用计时器的当前值填写value指向的结构体。

setitimer()将value指向的结构体设为计时器的当前值,如果ovalue不是NULL,将返回计时器原有值。

返回说明:

成功执行时,返回0。失败返回-1,errno被设为以下的某个值

EFAULT:value或ovalue是不有效的指针

EINVAL:其值不是ITIMER_REAL,ITIMER_VIRTUAL 或 ITIMER_PROF之一

(二)setitimer用法示例:

#include 

#include 

#include 

#include 

#include 

#include 

void PrintMsg(int Num)

{

printf("%s\n", "Hello World");

return;

}

int main(int argc, char* argv[])

{

signal(SIGALRM, PrintMsg);

struct itimerval tick;

tick.it_value.tv_sec = 10;  //十秒钟后将启动定时器    tick.it_value.tv_usec = 0;

tick.it_interval.tv_sec  =1; //定时器启动后,每隔1秒将执行相应的函数    tick.it_interval.tv_usec = 0;

//setitimer将触发SIGALRM信号    int ret = setitimer(ITIMER_REAL, &tick, NULL);

if ( ret != 0)

{

printf("Set timer error. %s \n", strerror(errno) );

return -1;

}

printf("Wait!\n");

getchar();

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值