4

我有一个需要在小型 Linux 嵌入式系统 (ARM) 中运行的小程序。它是用C编写的。它需要从系统制造商提供的API中轮询一些数据(2x64位),然后进行一些计算并通过网络发送数据。数据应每秒轮询约 30 次 (30Hz)。

在 C 中最好的方法是什么?我见过使用 的解决方案sleep(),但它似乎不是这项工作的最佳选择。

4

2 回答 2

3

I suggest consider using the poll(2) multiplexing syscall to do the polling.

notice that when poll is waiting and polling for input, it does not consume any CPU

If the processing of each event takes some significant time (e.g. a millisecond or more) you may want to recompute the delay.

You could use timerfd_create(2) (and give both your device file descriptor and your timer fd to poll). See also timer_create(2)...

Perhaps clock_gettime(2) could be useful.

And reading time(7) is definitely useful. Perhaps also the Advanced Linux Programming book.

于 2013-10-07T17:05:54.863 回答
2

sleep()在几秒钟内暂停执行,如果您正在寻找更准确的类似 sleep() 的函数,请使用usleep()它以微秒或nanosleep()纳秒为单位暂停执行。

于 2013-10-07T14:40:32.883 回答