Linux下的定时器,怎么用。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux下的定时器,怎么用。相关的知识,希望对你有一定的参考价值。
是用定时器好?还是新pthread_create一条线程,中间Sleep一个时间间隔好?
参考技术A 时事件,void(*handle)(union sigval v)参数就是处理事件的函数指针。int omsSetTimer(timer_t *tId,int value,int interval)就是设置定时器。
按你说的,如果要同时起多个定时器,需要定义一个数组timer_t tm[n];int it[n];tm就是定时器结构,it用来记录对应的定时器是否已经使用,使用中的就是1,没用的就是0;
主进程消息来了就从it找一个没用的来omsSetTimer,如果收到终止消息,那omsSetTimer 定时时间为0
int omsTimer(timer_t *tId,int iValue,int iSeconds ,void(*handle)(union sigval v),void * param)
struct sigevent se;
struct itimerspec ts;
memset (&se, 0, sizeof (se));
se.sigev_notify = SIGEV_THREAD;
se.sigev_notify_function = handle;
se.sigev_value.sival_ptr = param;
if (timer_create (CLOCK_REALTIME, &se, tId) < 0)
return -1;
ts.it_value.tv_sec = iValue;
// ts.it_value.tv_sec =3;
//ts.it_value.tv_nsec = (long)(iValue % 1000) * (1000000L);
ts.it_value.tv_nsec = 0;
ts.it_interval.tv_sec = iSeconds;
//ts.it_interval.tv_nsec = (long)(iSeconds % 1000) * (1000000L);
ts.it_interval.tv_nsec = 0;
if (timer_settime(*tId, TIMER_ABSTIME, &ts, NULL) < 0)
return -1;
return 0;
int omsSetTimer(timer_t *tId,int value,int interval)
struct itimerspec ts;
ts.it_value.tv_sec =value;
//ts.it_value.tv_nsec = (long)(value % 1000) * (1000000L);
ts.it_value.tv_nsec = 0;
ts.it_interval.tv_sec = interval;
//ts.it_interval.tv_nsec = (long)(interval % 1000) * (1000000L);
ts.it_interval.tv_nsec = 0;
if (timer_settime(*tId, TIMER_ABSTIME, &ts, NULL) < 0)
return -1;
return 0;
另外,团IDC网上有许多产品团购,便宜有口碑 参考技术B 数为秒数,在经过指定秒数后,alarm会发出一个SIGALRM信号
singal函数用来绑定信号处理器函数,这里绑定的是timer,被绑定的函数必须固定为返回值void、参数int。
只需要alarm(时间)就设置了,可能由于getchar需要进入中断导致信号被挂起所以没反应吧,可以试试把getchar换成别的东西来延时看看 参考技术C crontab多方便啊!具体命令格式,用法啊,百度上一大堆···
Linux下的hrtimer高精度定时器
本文转载自:http://blog.csdn.net/waverider2012/article/details/38305785
hrtimer高精度定时器的interval由ktime_set(const long secs, const unsigned long nsecs)决定,可做到ns级。此处的例子为5ms interval:
- #include <linux/kernel.h>
- #include <linux/module.h>
- #include <linux/hrtimer.h>
- #include <linux/ktime.h>
- MODULE_LICENSE("GPL");
- static struct hrtimer hr_timer;
- static struct work_struct wq_hrtimer;
- static ktime_t ktime;
- static unsigned int interval=5000; /* unit: us */
- struct timespec uptimeLast;
- static unsigned int count=0;
- #define COUNT_INTERVAL 4000
- unsigned long long diff_tv(struct timespec start, struct timespec end) {
- return (end.tv_sec-start.tv_sec)*1000000000+(end.tv_nsec-start.tv_nsec);
- }
- enum hrtimer_restart my_hrtimer_callback( struct hrtimer *timer )
- {
- schedule_work(&wq_hrtimer);
- return HRTIMER_NORESTART;
- }
- static void wq_func_hrtimer(struct work_struct *work)
- {
- struct timespec uptime;
- hr_timer.function = my_hrtimer_callback;
- ktime = ktime_set( interval/1000000, (interval%1000000)*1000 );
- hrtimer_start(&hr_timer, ktime, HRTIMER_MODE_REL );
- /* print time every COUNT_INTERVAL*interval second*/
- if(count%COUNT_INTERVAL==0)
- {
- do_posix_clock_monotonic_gettime(&uptime);
- printk(KERN_INFO"hrtimer:%9lu sec, %9lu ns, interval_delay=%lu ns\n",
- (unsigned long) uptime.tv_sec, uptime.tv_nsec,
- (unsigned long)(diff_tv(uptimeLast, uptime)-interval*1000*COUNT_INTERVAL) \
- /COUNT_INTERVAL);
- uptimeLast=uptime;
- }
- count++;
- }
- static int __init module_hrtimer_init( void )
- {
- struct timespec uptime;
- printk(KERN_INFO"HR Timer module installing\n");
- hrtimer_init( &hr_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL );
- ktime = ktime_set( interval/1000000, (interval%1000000)*1000 );
- hr_timer.function = my_hrtimer_callback;
- hrtimer_start( &hr_timer, ktime, HRTIMER_MODE_REL );
- do_posix_clock_monotonic_gettime(&uptime);
- uptimeLast = uptime;
- printk(KERN_INFO"hrtimer:%9lu sec, %9lu ns\n", (unsigned long) uptime.tv_sec,
- uptime.tv_nsec );
- INIT_WORK(&wq_hrtimer, wq_func_hrtimer);
- return 0;
- }
- static void __exit module_hrtimer_exit( void )
- {
- int ret;
- ret = hrtimer_cancel( &hr_timer );
- if (ret)
- printk("The timer was still in use...\n");
- printk("HR Timer module uninstalling\n");
- return;
- }
- module_init(module_hrtimer_init);
- module_exit(module_hrtimer_exit);
如果在my_hrtimer_callback()里面直接返回HRTIMER_RESTART会导致立即重新进入my_hrtimer_callback()。这时shell对输入没有任何响应。
所以为了解决这个问题,创建了一个work queue,由my_hrtimer_callback() enqueue这个工作队列。在work queue的处理函数里面重启hrtimer。
但是这样带来的负面影响是进入hrtimer_callback和wq_func被调用之间有Linux系统调度引入的延迟,导致interval出现误差。经过实测,在ZC706缺省配置下,这个延迟大约是17.5us (hrtimer interval为5ms,每20秒计算一次interval误差)。
- [email protected]:~/nfs/hrtimer# insmod hrtimer.ko
- HR Timer module installing
- hrtimer: 2900 sec, 993366078 ns
- hrtimer: 2900 sec, 998395278 ns, interval_delay=369966 ns
- hrtimer: 2921 sec, 69525447 ns, interval_delay=17782 ns
- hrtimer: 2941 sec, 139764655 ns, interval_delay=17559 ns
- hrtimer: 2961 sec, 210029519 ns, interval_delay=17566 ns
- hrtimer: 2981 sec, 280465631 ns, interval_delay=17609 ns
- hrtimer: 3001 sec, 350677038 ns, interval_delay=17552 ns
- hrtimer: 3021 sec, 420625114 ns, interval_delay=17487 ns
- hrtimer: 3041 sec, 490744847 ns, interval_delay=17529 ns
以上是关于Linux下的定时器,怎么用。的主要内容,如果未能解决你的问题,请参考以下文章