导航:首页 > 操作系统 > linux定时器select

linux定时器select

发布时间:2022-06-01 03:30:56

1. 如何在linux下实现定时器

定时器Timer应用场景非常广泛,在Linux下,有以下几种方法:
1,使用sleep()和usleep()
其中sleep精度是1秒,usleep精度是1微妙,具体代码就不写了。使用这种方法缺点比较明显,在Linux系统中,sleep类函数不能保证精度,尤其在系统负载比较大时,sleep一般都会有超时现象。
2,使用信号量SIGALRM + alarm()
这种方式的精度能达到1秒,其中利用了*nix系统的信号量机制,首先注册信号量SIGALRM处理函数,调用alarm(),设置定时长度,代码如下:

[cpp] view plain
#include <stdio.h>
#include <signal.h>

void timer(int sig)
{
if(SIGALRM == sig)
{
printf("timer\n");
alarm(1); //we contimue set the timer
}

return ;
}

int main()
{
signal(SIGALRM, timer); //relate the signal and function

alarm(1); //trigger the timer

getchar();

return 0;
}
alarm方式虽然很好,但是无法首先低于1秒的精度。

3,使用RTC机制
RTC机制利用系统硬件提供的Real Time Clock机制,通过读取RTC硬件/dev/rtc,通过ioctl()设置RTC频率,代码如下:

[cpp] view plain
#include <stdio.h>
#include <linux/rtc.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>

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, 1) < 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;
}
这种方式比较方便,利用了系统硬件提供的RTC,精度可调,而且非常高。
4,使用select()

这种方法在看APUE神书时候看到的,方法比较冷门,通过使用select(),来设置定时器;原理利用select()方法的第5个参数,第一个参数设置为0,三个文件描述符集都设置为NULL,第5个参数为时间结构体,代码如下:

[cpp] view plain
#include <sys/time.h>
#include <sys/select.h>
#include <time.h>
#include <stdio.h>

/*seconds: the seconds; mseconds: the micro seconds*/
void setTimer(int seconds, int mseconds)
{
struct timeval temp;

temp.tv_sec = seconds;
temp.tv_usec = mseconds;

select(0, NULL, NULL, NULL, &temp);
printf("timer\n");

return ;
}

int main()
{
int i;

for(i = 0 ; i < 100; i++)
setTimer(1, 0);

return 0;
}
这种方法精度能够达到微妙级别,网上有很多基于select()的多线程定时器,说明select()稳定性还是非常好。

总结:如果对系统要求比较低,可以考虑使用简单的sleep(),毕竟一行代码就能解决;如果系统对精度要求比较高,则可以考虑RTC机制和select()机制。

2. 嵌入式Linux下定时器问题,定时10us。

可以使用select函数实现定时,
timeval
tv;
tv.tv_usec=25;
tv.tv_sec=0;
select(1,NULL,NULL,NULL
&tv);

3. linux下的select函数是干嘛的

select是用来设置超时时间的,其第一个参数本来是一个文件号,假如读取该文件长时间没有返回则超时跳出,而这部分代码将文件号设置为0,说明只是为了控制延时
不过看你这部分代码,明显只是实现一个比较精确定时的sleep
这段代码之所以这么做,是因为linux本身的sleep函数非常不准(windows也是一样),在线程较多,cpu任务较重的时候,sleep函数的精确度根本无法达到要求
于是你这段代码使用select来代替sleep更为精准,其精准程度和内核相关,如果内核的滴答频率决定的,一般是100HZ也有1000hz的(因内核版本不同而不同),也就是说select做多可以精确到10ms,或者1ms,而sleep就做不到
于是
这段函数最重要的作用就是用高精确的select函数来代替低精确度的sleep函数,实现时间较为精准的延时

4. linux c下select函数的疑问

多个描述符返回准备好的那个,如果都为空,可以用作定时器

5. 怎样在Linux下实现精确定时器

linux下使用select实现精确定时器
在编写程序时,我们经常回用到定时器。本文讲述如何使用select实现超级时钟。使用select函数,我们能实现微妙级别精度的定时器。同时,select函数也是我们在编写非阻塞程序时经常用到的一个函数。
首先看看select函数原型如下:
int select(int nfds, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds, struct timeval *timeout);

参数说明:
slect的第一个参数nfds为fdset集合中最大描述符值加1,fdset是一个位数组,其大小限制为__FD_SETSIZE(1024),位数组的每一位代表其对应的描述符是否需要被检查。
select的第二三四个参数表示需要关注读、写、错误事件的文件描述符位数组,这些参数既是输入参数也是输出参数,可能会被内核修改用于标示哪些描述符上发生了关注的事件。所以每次调用select前都需重新初始化fdset。
timeout参数为超时时间,该结构会被内核修改,其值为超时剩余的时间。
利用select实现定时器,需要利用其timeout参数,注意到:
1)select函数使用了一个结构体timeval作为其参数。
2)select函数会更新timeval的值,timeval保持的值为剩余时间。
如果我们指定了参数timeval的值,而将其他参数都置为0或者NULL,那么在时间耗尽后,select函数便返回,基于这一点,我们可以利用select实现精确定时。
timeval的结构如下:
struct timeval{
long tv_sec;/*secons*
long tv_usec;/*microseconds*/
}

我们可以看出其精确到microseconds也即微妙。
一、秒级定时器

void seconds_sleep(unsigned seconds){
struct timeval tv;
tv.tv_sec=seconds;
tv.tv_usec=0;
int err;
do{
err=select(0,NULL,NULL,NULL,&tv);
}while(err<0 && errno==EINTR);
}

二、毫秒级别定时器

void milliseconds_sleep(unsigned long mSec){
struct timeval tv;
tv.tv_sec=mSec/1000;
tv.tv_usec=(mSec%1000)*1000;
int err;
do{
err=select(0,NULL,NULL,NULL,&tv);
}while(err<0 && errno==EINTR);
}

三、微妙级别定时器

void microseconds_sleep(unsigned long uSec){
struct timeval tv;
tv.tv_sec=uSec/1000000;
tv.tv_usec=uSec%1000000;
int err;
do{
err=select(0,NULL,NULL,NULL,&tv);
}while(err<0 && errno==EINTR);
}

现在我们来编写几行代码看看定时效果吧。

#include <stdio.h>
#include <sys/time.h>
#include <errno.h>
int main()
{
int i;
for(i=0;i<5;++i){
printf("%d\n",i);
//seconds_sleep(1);
//milliseconds_sleep(1500);
microseconds_sleep(1900000);
}
}

注:timeval结构体中虽然指定了一个微妙级别的分辨率,但内核支持的分别率往往没有这么高,很多unix内核将超时值向上舍入成10ms的倍数。此外,加上内核调度延时现象,即定时器时间到后,内核还需要花一定时间调度相应进程的运行。因此,定时器的精度,最终还是由内核支持的分别率决定。

6. linux制作毫秒级定时器

1
nanosleep函数可以提供最高分辨率,一般是纳秒级
2
select、poll函数的定时是毫秒级,pselect是纳秒级
以上三个函数都可以实现你的要求

7. select 循环定时器 linux

select函数
不是定时器,是I/O的复用,变成
异步传输
。linux的定时器要用信号如alarm来完成秒级定时,用内核定时完成毫秒级定时器。

8. 关于在linux 下如何实现定时器的请教

我知道linux下的select函数可以用来实现高精度的sleep,定时器暂不清楚,找找posix的API吧!

9. 关于select定时器的问题

刚才试了一下 没出现lz说的情况 虽说有误差 但是没那么大
测试程序
#include <stdio.h>
#include <sys/socket.h>

useconds_t timepased(struct timeval t1, struct timeval t2)
{
if((t2.tv_sec - t1.tv_sec) == 0)
return (t2.tv_usec - t1.tv_usec);
t2.tv_usec += ((t2.tv_sec - t1.tv_sec) * 1000000);
return (t2.tv_usec - t1.tv_usec);
}

int main(int argc, char **argv)
{
struct timeval tv, be, af;
tv.tv_sec = 0;
tv.tv_usec = atoi(argv[1]);
gettimeofday(&be, NULL);
select(1, NULL, NULL, NULL, &tv);
gettimeofday(&af, NULL);
printf("%ld\n", timepased(be, af));
return 0;
}

运行情况
[wangy@r2p ~/study] $ ./select 100000
103964
[wangy@r2p ~/study] $ ./select 100000
103296
[wangy@r2p ~/study] $ ./select 100000
105189
[wangy@r2p ~/study] $ ./select 10000
15627
[wangy@r2p ~/study] $ ./select 10000
15343
[wangy@r2p ~/study] $ ./select 10000
19578
[wangy@r2p ~/study] $ ./select 10000
17587

lz也用这个程序测一下看看吧

10. 求linux毫秒级定时器的实现

1 nanosleep函数可以提供最高分辨率,一般是纳秒级
2 select、poll函数的定时是毫秒级,pselect是纳秒级
以上三个函数都可以实现你的要求

阅读全文

与linux定时器select相关的资料

热点内容
pdf劈开合并 浏览:15
不能修改的pdf 浏览:742
同城公众源码 浏览:477
一个服务器2个端口怎么映射 浏览:283
java字符串ascii码 浏览:62
台湾云服务器怎么租服务器 浏览:464
旅游手机网站源码 浏览:317
android关联表 浏览:932
安卓导航无声音怎么维修 浏览:324
app怎么装视频 浏览:426
安卓系统下的软件怎么移到桌面 浏览:81
windows拷贝到linux 浏览:757
mdr软件解压和别人不一样 浏览:891
单片机串行通信有什么好处 浏览:328
游戏开发程序员书籍 浏览:851
pdf中图片修改 浏览:275
汇编编译后 浏览:482
php和java整合 浏览:836
js中执行php代码 浏览:449
国产单片机厂商 浏览:63