setitimer用法说明
程序员文章站
2022-06-01 18:35:17
...
函数原型:
int setitimer(int which, const struct itimerval *new_value,struct itimerval *old_value)
函数作用:
可用来实现延时和定时的功能
头文件:
#include <sys/time.h>
参数详解
用一把:一个例子
#include "head.h"
#include <unistd.h>
#include <signal.h>
#include <sys/time.h>
void handler(int s)
{
printf("time out %d\n",s);
}
int main()
{
key_t key = ftok(".",1);
signal(SIGALRM,handler);
struct itimerval it;
memset(&it ,0x00,sizeof (it));
it.it_interval.tv_sec = 1;
it.it_value.tv_sec = 1;
setitimer(ITIMER_REAL,&it,NULL);
for(;;)
{
printf(".");
fflush(stdout);
sleep(1);
}
}
代码注释
signal(SIGALRM,handler);
//SIGALRM见另一篇文章
//1秒打一个点用来观看效果
for(;;)
{
printf(".");
fflush(stdout);
sleep(1);
}
更改值观察效果:
1.
19 it.it_interval.tv_sec = 1;
20 it.it_value.tv_sec = 1;
2.
19 it.it_interval.tv_sec = 3;
20 it.it_value.tv_sec = 1;
3.
19 it.it_interval.tv_sec = 3;
20 it.it_value.tv_sec = 2;