Linux 用户下定时器的实现
来源:互联网 发布:同时删除多个表的数据 编辑:程序博客网 时间:2024/05/16 10:40
注:原文头文件缺失,有时间再添加
Linux下的定时器有两种,以下分别介绍:
1、alarm
如果不要求很精确的话,用alarm()和signal()就够了
unsigned int alarm(unsigned int seconds)
函数说明: alarm()用来设置信号SIGALRM在经过参数seconds指定的秒数后传送给目前的进程。如果参数seconds为0,则之前设置的闹钟会被取消,并将剩下的时间返回。
返回值: 返回之前闹钟的剩余秒数,如果之前未设闹钟则返回0。
alarm()执行后,进程将继续执行,在后期(alarm以后)的执行过程中将会在seconds秒后收到信号SIGALRM并执行其处理函数。
#include #include #include void sigalrm_fn(int sig){printf("alarm!\n");alarm(2);return;}int main(void){signal(SIGALRM, sigalrm_fn);alarm(1);while(1) pause();}
2、setitimer()
int setitimer(int which, const struct itimerval *value, struct itimerval *ovalue));
setitimer()比alarm功能强大,支持3种类型的定时器:
ITIMER_REAL : 以系统真实的时间来计算,它送出SIGALRM信号。
ITIMER_VIRTUAL : -以该进程在用户态下花费的时间来计算,它送出SIGVTALRM信号。
ITIMER_PROF : 以该进程在用户态下和内核态下所费的时间来计算,它送出SIGPROF信号。
setitimer()第一个参数which指定定时器类型(上面三种之一);第二个参数是结构itimerval的一个实例;第三个参数可不做处理。
setitimer()调用成功返回0,否则返回-1。
下面是关于setitimer调用的一个简单示范,在该例子中,每隔一秒发出一个SIGALRM,每隔0.5秒发出一个SIGVTALRM信号:
#include #include #include #include #include #include int sec;void sigroutine(int signo){switch (signo){case SIGALRM:printf("Catch a signal -- SIGALRM \n");signal(SIGALRM, sigroutine);break;case SIGVTALRM:printf("Catch a signal -- SIGVTALRM \n");signal(SIGVTALRM, sigroutine);break;}return;}int main(){struct itimerval value, ovalue, value2; //(1)sec = 5;printf("process id is %d\n", getpid());signal(SIGALRM, sigroutine);signal(SIGVTALRM, sigroutine);value.it_value.tv_sec = 1;value.it_value.tv_usec = 0;value.it_interval.tv_sec = 1;value.it_interval.tv_usec = 0;setitimer(ITIMER_REAL, &value, &ovalue); //(2)value2.it_value.tv_sec = 0;value2.it_value.tv_usec = 500000;value2.it_interval.tv_sec = 0;value2.it_interval.tv_usec = 500000;setitimer(ITIMER_VIRTUAL, &value2, &ovalue);for(;;);}
(1) struct itimerval
struct itimerval {struct timeval it_interval; /* timer interval */struct timeval it_value; /* current value */};itimerval: i --> intervalval --> value
itimerval结构中的it_value是减少的时间,当这个值为0的时候就发出相应的信号了. 然后再将it_value设置为it_interval值.
(2) setitimer()
setitimer()为其所在进程设置一个定时器,如果itimerval.it_interval不为0(it_interval的两个域都不为0),则该定时器将持续有效(每隔一段时间就会发送一个信号)
注意:Linux信号机制基本上是从Unix系统中继承过来的。早期Unix系统中的信号机制比较简单和原始,后来在实践中暴露出一些问题,因此,把那些建立在早期机制上的信号叫做"不可靠信号",信号值小于 SIGRTMIN(SIGRTMIN=32,SIGRTMAX=63)的信号都是不可靠信号。这就是"不可靠信号"的来源。它的主要问题是:进程每次处理信号后,就将对信号的响应设置为默认动作。在某些情况下,将导致对信号的错误处理;因此,用户如果不希望这样的操作,那么就要在信号处理函数结尾再一次调用signal(),重新安装该信号。
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
setitimer()为Linux的API,并非C语言的Standard Library,setitimer()有两个功能,一是指定一段时间后,才执行某个function,二是每间格一段时间就执行某个function,以下程序demo如何使用setitimer()。
- /*
- 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.cppCompiler : gcc 4.1.0 on Fedora Core 5Description : setitimer() set the interval to run functionSynopsis : #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只会执行一次而已。
---------------------------------------------------------------------------------------------------------------------------------------------------------
linux下还有一种高精度的定时器,那就是posix_timer.我记得以前看代码的时候CLOCK_REALTIME的定时器似乎用的就是rdtsc指令,不过现在不确定了,先放到一边。原理上来说,可以在变频的时候也使用rdtsc指令,因为CPU的频率我们也是知道的,变频的时候内核也是知道的。
下面是我的timer_create的例子,编译的时候要加上rt库,这是linux的realtime库:
gcc -o test test.c
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <time.h>
#define rdtsc(low,high) __asm__ __volatile__("rdtsc" : "=a" (low), "=d" (high))
timer_t tt;
void handler (int sig, siginfo_t * extra, void *cruft)
{
static last_i=0;
unsigned int i, j;
rdtsc(i,j);
printf ("time:%u, %u, [%u] %uHZ ", j, i, i-last_i, (i-last_i)*10/1000000);
last_i = i;
}
int main ()
{
int i=0;
sigset_t sigset;
sigfillset (&sigset);
sigdelset (&sigset, SIGRTMIN);
sigprocmask (SIG_SETMASK, &sigset, NULL);
struct sigaction sa;
sigfillset (&sa.sa_mask);
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = handler;
if (sigaction (SIGRTMIN, &sa, NULL) < 0)
{
perror ("sigaction failed ");
exit (-1);
}
struct sigevent timer_event;
struct itimerspec timer;
timer.it_interval.tv_sec = 0;
timer.it_interval.tv_nsec = 100 * 1000 * 1000;
timer.it_value = timer.it_interval;
timer_event.sigev_notify = SIGEV_SIGNAL;
timer_event.sigev_signo = SIGRTMIN;
timer_event.sigev_value.sival_ptr = (void *) &tt;
if (timer_create (CLOCK_REALTIME, &timer_event, &tt) < 0)
{
perror ("timer_create failed");
exit (-1);
}
if (timer_settime (tt, 0, &timer, NULL) < 0)
{
perror ("timer_settime failed");
exit (-1);
}
while (i++ < 10)
{
pause ();
}
return 0;
}
输出结果:
time:166081, 1934350847, [1934350847] 2163HZ
time:166081, 2120528291, [186177444] 1861HZ
time:166081, 2306679576, [186151285] 1861HZ
time:166081, 2494695630, [188016054] 1880HZ
time:166081, 2680865389, [186169759] 1861HZ
time:166081, 2867018473, [186153084] 1861HZ
time:166081, 3053152230, [186133757] 1861HZ
time:166081, 3239309935, [186157705] 1861HZ
time:166081, 3425467261, [186157326] 1861HZ
time:166081, 3611639266, [186172005] 1861HZ
- Linux 用户下定时器的实现
- linux下定时器的实现
- linux 下定时器的实现
- Linux下多定时器的实现
- Linux 下定时器的实现方式分析
- Linux 下定时器的实现方式分析
- Linux 下定时器的实现方式分析
- linux 下定时器的实现方式分析
- Linux 下定时器的实现方式分析
- linux下多定时器的实现
- Linux 下定时器的实现方式分析
- Linux下多定时器的实现
- Linux下定时器的实现方式分析
- Linux 下定时器的实现方式分析
- Linux 下定时器的实现方式分析
- Linux 下定时器的实现方式分析
- Linux 下定时器的实现方式分析
- Linux 下定时器的实现方式分析
- 汇编学习笔记:CPU内部组成,寄存器讲解
- Apache nutch1.5 & Apache solr3.6
- 关于vs2005、vs2008和vs2010项目互转的总结
- 解决flexpaper+swfobject在线预览滚动条不能滚动的问题
- 信息技术
- Linux 用户下定时器的实现
- 一个简单的url重写
- device_register和驱动driver_register先后注册的影响和关系
- 转一篇iBatis和Hibernate的对比
- 编程书籍 中文翻译版错误纠正
- rcp(插件开发)Part already exists in page layout 解决之道
- 一些有用的小代码
- C#发送邮件
- C++程序运行时内存布局之----------空类实例