Correct way in in Linux GCC to create an idle loop

Hello, I'm experienced in C but relatively new to linux programming.  But it's nice and quick to use GCC.  I'm writing a couple of utilities and they use the pattern below to perform some periodic behavior.

It works fine but according to the system monitor the CPU % is pretty high.  So I have to ask Linux experts -- is this bad practice?  Is there some Linux OS call I should make to cede control back to the operating system for a specific amount of time?

What I'm interested in is the capital-R "Right" way to do this, from the true Linux gurus.

OR ... to get affirmation that this practice is okay... less work for me.  :)

Thanks for any help.

void main()

   int counter = 0;

   // initialize code

      if (counter>10000)
         // do something periodic

Open in new window

Who is Participating?
sarabandeConnect With a Mentor Commented:
you should use sleep or usleep function to wait a little while.

if you want your program to be responsive, you need to pass the periodic stuff into a thread or "fork" a second process and wait for user input in the main thread.

phoffricConnect With a Mentor Commented:
The right way to do this depends on (1) what the "something" is in "// do something periodic" and (2) how much time error in your periodicity your application allows. When you sleep, you are guaranteed to sleep at least as long as you specify, but you are not guaranteed to wake up exactly when the specified sleep time expires, so left unchecked, you may see a large drift over a long period of time.
RonMexicoAuthor Commented:
Sara, usleep seems to work nicely and does reduce my CPU levels, so it seems like better practice... ?  Or maybe I shouldn't care about CPU load, the linux scheduler will take control away as necessary?

Phoffric, good point.  This (and other tasks I would use this for) is not a precise or time-critical function, I would be looking for a different architecture.  Thanks.
>> maybe I shouldn't care about CPU load, the linux scheduler will take control away as necessary?
If you don't care that you will be doing nothing during a time slice while other tasks are waiting to run, then maybe you don't need to care (other than the fact that your CPU loading is high, and generating heat, and wasting electricity, and needing extra air conditioning to cool off the room if there are many servers doing nothing in tight do-nothing loops).

You can divide the sleep time by N (your choice, could be 2, 3, 4, depending upon the resolution you need), and then check the time to see if you are within the desired scheduling time.

This may be useful depending upon your overall system requirements. At least other runnable tasks will be given a quicker chance to do their job.
the idle loop you try to use will be prominently eliminated to noop by any even minimally optimizing compiler. Depending what you are waiting foor - usleep, sched_yeld, sleep, select. select, poll etc...
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.