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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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...
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.