On Mon, Dec 12, 2005 at 06:17:35PM -0500, Jeff Moyer wrote:
==> Regarding [Wlug] getting that cpu to sleep ...; brad <maitre@ccs.neu.edu> adds:
maitre> greetings,
maitre> I have a question for those linux programmers out there. i'm maitre> writing a program on linux (kubuntu to be specific) that requires maitre> high-precision timing, somewhere on the order of 100 to 1000 micro maitre> seconds. I get very good timing out of a loop written such that a maitre> while() loop checks the system clock after every iteration and maitre> comparing that to the desired wait time, like so (pseudo_code): maitre> while(time_passed < wait_time) { time_passed = get_time() } ...
maitre> The problem with this approach is that the process uses 99% of the maitre> CPU since it just spins the CPU until enough time has expired. My
The other problem with this is that you can be scheduled if your timeslice runs out.
umm, i didn't think about that one.
maitre> question: is there a way on linux (in C) to have the CPU sleep or maitre> send it a no-op such that it's not just eating up cycles until time maitre> expires? I have looked at nanosleep and usleep, however they appear maitre> to only have precision to 10ms which too latent for my needs.
As Mike pointed out, in more recent kernels you can tune the value of HZ to get better granularity from select/poll.
Actually Mike's response reminded me of an article i bumped into about nanosleep on dragonflyBSD http://www.dragonflybsd.org/docs/nanosleep/
What, precisely, are you trying to do and on what kernel? Perhaps with more information we could provide better guidance.
The immediate task at hand is that i want to get readings from an instrument (a light sensor) at a uniform interval, say every 1000 micro seconds. I have been able to achieve this task by setting up the loop i described above. However the loop above takes 99% of the CPU. I'd like to be able to achieve this without pegging the CPU at 99%. Does this make sense? if not i can try explaining it again, perhaps with some code. The kernel i'm running is 2.6.12. (well uname -r reports '2.6.12-9-686-smp.' its ubuntu's stock smp kernel.) -brad