阅读内容

在Linux系统下实现定时器的几种常用方法

[日期:2007-01-05] 来源:  作者:Linux



最优雅的方法是使用RTC机制。利用select函数,你可以用单线程实现定时器,同时还可以处理其它任务。简单示例:
QUOTE:
#include

#include

#include

#include

#include

#include

#include

#include

int main(int argc, char* argv[])

{

unsigned long i = 0;

unsigned long data = 0;

int retval = 0;

int fd = open ("/dev/rtc", O_RDONLY);

if(fd < 0)

{

perror("open");

exit(errno);

}

/*Set the freq as 4Hz*/

if(ioctl(fd, RTC_IRQP_SET, 4) < 0)

{

perror("ioctl(RTC_IRQP_SET)");

close(fd);

exit(errno);

}

/*Set the freq as 4Hz*/

if(ioctl(fd, RTC_IRQP_SET, 4) < 0)

{

perror("ioctl(RTC_IRQP_SET)");

close(fd);

exit(errno);

}



/* Enable periodic interrupts */

if(ioctl(fd, RTC_PIE_ON, 0) < 0)

{

perror("ioctl(RTC_PIE_ON)");

close(fd);

exit(errno);

}


for(i = 0; i < 100; i++)

{

if(read(fd, &data, sizeof(unsigned long)) < 0)

{

perror("read");

close(fd);

exit(errno);

}

printf("timer\n");

}

/* Disable periodic interrupts */

ioctl(fd, RTC_PIE_OFF, 0);

close(fd);


return 0;

}

上一页12  GO
【内容导航】
第1页:在Linux系统下实现定时器的几种常用方法 第2页:在Linux系统下实现定时器的几种常用方法



相关资讯      
本文评论       全部评论
发表评论
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款


点评: 字数
姓名:

L
I
N
U
X


GOOGLE搜索
Google