天下网吧 >> 网吧天地 >> 网吧技术 >> 网吧系统 >> 正文

Linux操作系统下实时定时器posix_timer

2008-4-8塞迪网佚名

其实Linux下还有一种高精度的定时器,那就是posix_timer。原理上来说,可以在变频的时候也使用rdtsc指令,因为CPU的频率我们也是知道的,变频的时候内核也是知道的。

下面是我的timer_create的例子,编译的时候要加上rt库,这是linux的realtime库:

gcc -o test test.c

#include 
#include 
#include 
#include 
#include 

#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

欢迎访问最专业的网吧论坛,无盘论坛,网吧经营,网咖管理,网吧专业论坛https://bbs.txwb.com

关注天下网吧微信,了解网吧网咖经营管理,安装维护:


本文来源:塞迪网 作者:佚名

声明
本文来源地址:0
声明:本站所发表的文章、评论及图片仅代表作者本人观点,与本站立场无关。若文章侵犯了您的相关权益,请及时与我们联系,我们会及时处理,感谢您对本站的支持!联系Email:support@txwb.com.,本站所有有注明来源为天下网吧或天下网吧论坛的原创作品,各位转载时请注明来源链接!
天下网吧·网吧天下
  • 本周热门
  • 本月热门
  • 阅读排行