【linux】select实现定时器

/*秒级定时器*/
void seconds_sleep(unsigned     long  seconds)
{
    if(seconds == 0) return;
    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)
{
    if(mSec == 0) return;
    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)
{
    if(uSec == 0) return;
    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);
}
 
 
int main()
{
    int i;
    for(i=0;i<5;++i){
    printf("%d\n",i);
    //seconds_sleep(2);
    //milliseconds_sleep(2000);
    microseconds_sleep(2000000);
    }
}

http://gityuan.com/2019/01/05/linux-poll-select/
https://blog.csdn.net/sjp11/article/details/126312199
https://www.cnblogs.com/sctb/p/17400454.html
https://blog.csdn.net/weixin_37926485/article/details/122810971

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Linux中,有几种方法可以实现定时器功能: 1. 使用系统调用 alarm():该调用会在指定的时间(以秒为单位)后触发SIGALRM信号。可以通过注册信号处理函数来处理该信号,实现定时器功能。例如: ``` #include <unistd.h> #include <signal.h> void handler(int signum) { // 定时器触发后的处理逻辑 } int main() { signal(SIGALRM, handler); alarm(5); // 5秒后触发SIGALRM信号 while (1) {} // 持续运行程序,直到收到SIGALRM信号 return 0; } ``` 2. 使用timer_create()函数:该函数可以创建一个定时器,精度可以达到毫秒级别。例如: ``` #include <time.h> #include <signal.h> void handler(int signum) { // 定时器触发后的处理逻辑 } int main() { struct sigevent sev; timer_t timerid; struct itimerspec its; // 创建定时器 sev.sigev_notify = SIGEV_SIGNAL; sev.sigev_signo = SIGUSR1; sev.sigev_value.sival_ptr = &timerid; timer_create(CLOCK_REALTIME, &sev, &timerid); // 设置定时器 its.it_value.tv_sec = 1; its.it_value.tv_nsec = 0; its.it_interval.tv_sec = 1; its.it_interval.tv_nsec = 0; timer_settime(timerid, 0, &its, NULL); // 注册信号处理函数 signal(SIGUSR1, handler); while (1) {} // 持续运行程序,直到收到SIGUSR1信号 return 0; } ``` 3. 使用select()函数:该函数可以等待多个文件描述符中的任意一个就绪,可以通过设置超时时间实现定时器功能。例如: ``` #include <stdio.h> #include <sys/select.h> int main() { fd_set rfds; struct timeval tv; while (1) { // 清空文件描述符集合 FD_ZERO(&rfds); // 设置需要监视的文件描述符 FD_SET(0, &rfds); // 监视标准输入 // 设置超时时间 tv.tv_sec = 5; tv.tv_usec = 0; // 等待文件描述符就绪或超时 int ret = select(1, &rfds, NULL, NULL, &tv); if (ret == -1) { perror("select"); } else if (ret == 0) { printf("Timeout\n"); // 超时处理逻辑 } else { if (FD_ISSET(0, &rfds)) { printf("Input available\n"); // 文件描述符就绪处理逻辑 } } } return 0; } ``` 这些方法都可以实现定时器功能,选择哪种方法取决于具体的场景和需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yengi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值