寻找周期c语言,C语言通过timeval结构设置周期

#include

#include

#include

int main()

{

int time_interval=3; // Set period to be 3s

printf("Testing start:\n");

while(1){

setPeriod(time_interval);

//You can add a method here to realize what you want to do in this period.

}

return 0;

}

void setPeriod(int time_interval){

static struct timeval tv1;

struct timeval tv2;

int time_in_us;

static int flag = 1;

gettimeofday(&tv2,NULL);

if(flag){

tv1.tv_sec = tv2.tv_sec;

tv1.tv_usec = tv2.tv_usec;

flag = 0;

return;

}

time_in_us = (tv2.tv_sec - tv1.tv_sec) * 1000000 + tv2.tv_usec - tv1.tv_usec;

if(time_in_us >= time_interval * 1000000) {

tv1.tv_sec = tv2.tv_sec;

tv1.tv_usec = tv2.tv_usec;

// You can add a method here to make statistic about the data you get in this peorid.

printf("Hello, world\n");

}

}

---------------------------------------------------------------------------------------------------------------------------------------------

以上代码实现一个周期为3s的设置,执行结果为: 每隔三秒print一个“Hello world”.

注意,要实现每隔三秒print一个“Hello world”, sleep(3000) 会是一个更简便的方法。但是,用sleep方法,那么在该周期三秒内,只能sleep, 不能实现其他动作。

欢迎大家指正!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 C 语言中,可以使用 `select()` 函数来实现读取文件时的超时操作。`select()` 函数可以同时监视多个文件描述符,如果其中的某个文件描述符变为“可读”,则 `select()` 函数就会返回。我们可以利用这一特性,在 `select()` 函数的超时时间内等待数据的到来,如果超时时间到了但数据还未到来,则认为读取超时。 以下是一个示例代码,演示了如何使用 `select()` 函数实现读取文件时的超时操作: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <errno.h> #include <fcntl.h> #include <sys/select.h> #define TIMEOUT_SEC 5 // 超时时间(秒) #define BUF_SIZE 1024 // 缓冲区大小 int main(int argc, char *argv[]) { int fd; struct timeval timeout; fd_set rset; char buf[BUF_SIZE]; int ret, n; // 打开文件 fd = open("file.txt", O_RDONLY); if (fd < 0) { perror("open failed"); exit(EXIT_FAILURE); } // 设置非阻塞模式 int flags = fcntl(fd, F_GETFL, 0); fcntl(fd, F_SETFL, flags | O_NONBLOCK); // 等待数据到来 FD_ZERO(&rset); FD_SET(fd, &rset); timeout.tv_sec = TIMEOUT_SEC; timeout.tv_usec = 0; ret = select(fd + 1, &rset, NULL, NULL, &timeout); if (ret < 0) { perror("select failed"); exit(EXIT_FAILURE); } else if (ret == 0) { printf("read timeout\n"); exit(EXIT_SUCCESS); } else { if (FD_ISSET(fd, &rset)) { n = read(fd, buf, BUF_SIZE); if (n < 0) { if (errno == EWOULDBLOCK || errno == EAGAIN) { printf("read would block\n"); } else { perror("read failed"); } } else if (n == 0) { printf("end of file\n"); } else { printf("read %d bytes: %.*s\n", n, n, buf); } } } close(fd); exit(EXIT_SUCCESS); } ``` 在上述代码中,我们使用 `select()` 函数等待 fd 的可读事件,等待时间为 `TIMEOUT_SEC` 秒。如果在等待时间内 fd 变为“可读”,则调用 `read()` 函数读取数据。如果 `read()` 函数返回 0,表示已经读到文件末尾;如果 `read()` 函数返回负值,可以通过判断 `errno` 是否为 `EWOULDBLOCK` 或 `EAGAIN` 来判断是否读取超时。如果 `read()` 函数返回正值,则表示读取到了数据。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值