Reputation: 7466
I have a small program that needs to be run in a small Linux embedded system (ARM). It is written in C. It needs to poll some data (2x64-bit) from an API provided by the system manufacturer, and then do some calculations and send the data through the network. The data should be polled around 30 times every second (30Hz).
What would be the best way to do it in C? I've seen solutions using sleep()
, but it does not seem to be the best option for the job.
Upvotes: 4
Views: 811
Reputation: 1
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.
Upvotes: 3
Reputation: 122493
sleep()
suspends execution in seconds, if you are looking for a more accurate sleep()-like function, use usleep()
which suspends execution in microseconds, or nanosleep()
in nanoseconds.
Upvotes: 2