setitimer() 定时函数
来源:互联网 发布:像素软件 新三板 编辑:程序博客网 时间:2024/06/04 19:00
setitimer()为Linux的API,并非C语言的Standard Library.
setitimer()有两个功能,一是指定一段时间后,才执行某个function,二是每间格一段时间就执行某个function.
以下程序demo如何使用setitimer()。
view plaincopy to clipboardprint?
/*
Filename : timer.cpp
Compiler : gcc 4.1.0 on Fedora Core 5
Description : setitimer() set the interval to run function
Synopsis : #include <sys/time.h>
int setitimer(int which, const struct itimerval *value, struct itimerval *ovalue);
struct itimerval {
struct timerval it_interval;
struct timerval it_value;
};
struct timeval {
long tv_sec;
long tv_usec;
}
Release : 11/25/2006
*/
#include <stdio.h> // for printf()
#include <unistd.h> // for pause()
#include <signal.h> // for signal()
#include <string.h> // for memset()
#include <sys/time.h> // struct itimeral. setitimer()
void printMsg(int);
int main() {
// Get system call result to determine successful or failed
int res = 0;
// Register printMsg to SIGALRM
signal(SIGALRM, printMsg);
struct itimerval tick;
// Initialize struct
memset(&tick, 0, sizeof(tick));
// Timeout to run function first time
tick.it_value.tv_sec = 1; // sec
tick.it_value.tv_usec = 0; // micro sec.
// Interval time to run function
tick.it_interval.tv_sec = 1;
tick.it_interval.tv_usec = 0;
// Set timer, ITIMER_REAL : real-time to decrease timer,
// send SIGALRM when timeout
res = setitimer(ITIMER_REAL, &tick, NULL);
if (res) {
printf("Set timer failed!!/n");
}
// Always sleep to catch SIGALRM signal
while(1) {
pause();
}
return 0;
}
void printMsg(int num) {
printf("%s","Hello World!!/n");
}
/*
Filename : timer.cpp
Compiler : gcc 4.1.0 on Fedora Core 5
Description : setitimer() set the interval to run function
Synopsis : #include <sys/time.h>
int setitimer(int which, const struct itimerval *value, struct itimerval *ovalue);
struct itimerval {
struct timerval it_interval;
struct timerval it_value;
};
struct timeval {
long tv_sec;
long tv_usec;
}
Release : 11/25/2006
*/
#include <stdio.h> // for printf()
#include <unistd.h> // for pause()
#include <signal.h> // for signal()
#include <string.h> // for memset()
#include <sys/time.h> // struct itimeral. setitimer()
void printMsg(int);
int main() {
// Get system call result to determine successful or failed
int res = 0;
// Register printMsg to SIGALRM
signal(SIGALRM, printMsg);
struct itimerval tick;
// Initialize struct
memset(&tick, 0, sizeof(tick));
// Timeout to run function first time
tick.it_value.tv_sec = 1; // sec
tick.it_value.tv_usec = 0; // micro sec.
// Interval time to run function
tick.it_interval.tv_sec = 1;
tick.it_interval.tv_usec = 0;
// Set timer, ITIMER_REAL : real-time to decrease timer,
// send SIGALRM when timeout
res = setitimer(ITIMER_REAL, &tick, NULL);
if (res) {
printf("Set timer failed!!/n");
}
// Always sleep to catch SIGALRM signal
while(1) {
pause();
}
return 0;
}
void printMsg(int num) {
printf("%s","Hello World!!/n");
}
当setitimer()所执行的timer时间到了,会呼叫SIGALRM signal,所以在第30行用signal()将要执行的function指定给SIGALRM。
在第43行呼叫setitimer()设定timer,但setitimer()第二个参数是sturct,负责设定timeout时间,所以第36行到第 40行设定此struct。itimerval.it_value设定第一次执行function所延迟的秒数, itimerval.it_interval设定以后每几秒执行function,所以若只想延迟一段时间执行function,只要设定 itimerval.it_value即可,若要设定间格一段时间就执行function,则it_value和it_interval都要设定,否则 funtion的第一次无法执行,就别说以后的间隔执行了。
第36行和第39行的tv_sec为sec,第37行和40行为micro sec(0.001 sec)。
第43行的第一个参数ITIMER_REAL,表示以real-time方式减少timer,在timeout时会送出SIGALRM signal。第三个参数会存放旧的timeout值,如果不需要的话,指定NULL即可。
第47 行的pause(),命令系统进入sleep状态,等待任何signal,一定要用while(1)无穷循环执行pause(),如此才能一直接收 SIGALRM signal以间隔执行function,若拿掉while(1),则function只会执行一次而已。
setitimer()是比较精准的定时器,是Linux的API
以下代码是个倒计时例子:
view plaincopy to clipboardprint?
#include <signal.h>
#include <sys/time.h>
static int time_count=30;
static void sigalrm_handler(int sig)
{
time_count--;
printf("timer signal.. %d/n", time_count);
}
struct itimerval v; //定时器结构体
time_count=10; //再次初始化
signal(SIGALRM, sigalrm_handler);//绑定信号函数
v.it_value.tv_sec=1;
v.it_value.tv_usec=0;
v.it_interval.tv_sec=1;//间隔1s
v.it_interval.tv_usec=0;
setitimer(ITIMER_REAL,&v,NULL); //结构体关联,start
...
...
//=========结束此定时器=====================
v.it_value.tv_sec=0;
v.it_value.tv_usec=0;
v.it_interval.tv_sec=0;
v.it_interval.tv_usec=0;
setitimer(ITIMER_REAL,&v,NULL);
#include <signal.h>
#include <sys/time.h>
static int time_count=30;
static void sigalrm_handler(int sig)
{
time_count--;
printf("timer signal.. %d/n", time_count);
}
struct itimerval v; //定时器结构体
time_count=10; //再次初始化
signal(SIGALRM, sigalrm_handler);//绑定信号函数
v.it_value.tv_sec=1;
v.it_value.tv_usec=0;
v.it_interval.tv_sec=1;//间隔1s
v.it_interval.tv_usec=0;
setitimer(ITIMER_REAL,&v,NULL); //结构体关联,start
...
...
//=========结束此定时器=====================
v.it_value.tv_sec=0;
v.it_value.tv_usec=0;
v.it_interval.tv_sec=0;
v.it_interval.tv_usec=0;
setitimer(ITIMER_REAL,&v,NULL);
原理既是 1s发送SIGALRM信号一次,将其值置零即为关闭,不在发送信号
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ling1874/archive/2009/09/28/4603696.aspx
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/fanwenbo/archive/2008/07/13/2645362.aspx
- setitimer() 定时函数
- setitimer() 定时函数
- setitimer定时函数
- 定时函数setitimer()
- 定时函数setitimer
- Linux下定时函数setitimer
- Linux下定时函数setitimer
- Linux下定时函数setitimer
- setitimer函数
- setitimer函数
- setitimer函数
- linux上定时函数 setitimer 的使用介绍
- linux上定时函数 setitimer 的使用介绍
- 如何使用setitimer来定时
- setitimer函数--signal
- linux 中的setitimer()函数
- Linux定时器函数setitimer
- linux中定时信号setitimer和alarm
- 如何控制怒气并表达意见?
- Word Process Using OLE Automation
- 搜索引擎检索技术
- C/C++笔试面试题目汇总3——各种排序算法
- 开发Web 2.0的9条注意事项 清晰的目标是核心
- setitimer() 定时函数
- 又是几个MIS_表
- 快速排序
- Python基础教程:第2版(经典教程的全新改版,10个项目引人入胜 )
- 学习嵌入式工程师的经历
- 两篇高考作文
- 层固定,不随滚动条滚动
- 学习C++计划
- 从屏幕拷贝字符到vi中,很乱的解决