LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-07-2004, 10:29 AM   #1
unholy
Member
 
Registered: Sep 2003
Location: Eire
Distribution: Ubuntu 7.10
Posts: 344

Rep: Reputation: 30
while loop too cpu hungry


How do developers write applications that use next to no cpu? For example frozen bubble / kdevelop ... or any decent app.

My application waits for keyboard events in a while loop. But the cpu usage of the application jumps to 90% as a result!

This is an SDL application, but I also wrote a simple program and got the same problem..
Code:
unsigned int x = 0;
while(1)
{
  x++;
if ( x == 0 )
{
  cout << "x looped" << endl;
}
}
The cpu usage is also in the 90 percentile here. How do developers write applications that use next to no cpu?
 
Old 09-07-2004, 11:41 AM   #2
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,990

Rep: Reputation: 67
There are two basic ways that you can do this.
The first (and easiest) is to call usleep(us); from within the loop, with us set to a time in microseconds. This will sleep for us microseconds, during which time the CPU isn't used.

The problem with this is that you still have to poll much faster than you need to, which is inefficient. A better way is to set up a timer to interrupt your process every time it needs to do something, or an input event occurs, and have your process call the kernel-level function schedule(); instead (effectively sleep for a while unless interrupted). Unfortunately, I don't know how to do that outside of the kernel.

I suggest you could examine the source of Frozen Bubble to see how they do it.
 
Old 09-07-2004, 12:51 PM   #3
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
If you're waiting for input, from the user or the network, you typically use poll() or select(). The gtk event loop sits in poll() most of the time, waiting for events from X, which arrive over a socket. Qt probably does something similar.

If you're writing a gui app, of course you'll just use one of these toolkits and not deal with poll() or select() directly. Even if you aren't writing a gui, glib (used by gtk) can abstract the event loop for you.
 
Old 09-07-2004, 01:22 PM   #4
onnyloh
Member
 
Registered: Nov 2002
Posts: 57

Rep: Reputation: 15
i use java, thread programming could achieve the same goal.
i think c should work similiarly
!st create some thread using pthread lib, then ask them to wait in pool, once it is triggered(stdin), it will continue the loop.
 
Old 09-07-2004, 01:31 PM   #5
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
As far as I could tell, java doesn't give you a proper way to do networked gui apps as a single thread, which IMO is really icky. Particularly since threading isn't built into the C language like it is in java, I think things come out cleaner if you stay away from threads where select() does the same job. Programming in one thread means you don't need to worry about deadlocks and races, and what lock goes with what data structure and so on.

It's a lot easier to debug a program when it only does one thing at once!
 
Old 09-07-2004, 05:29 PM   #6
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Rep: Reputation: 32
I always just sleep for 5 or more miliseconds. My apps always use 1% or 0% cpu, and are REALLY fast. So what the heck!
 
Old 09-07-2004, 06:01 PM   #7
servnov
Member
 
Registered: Sep 2004
Distribution: Slackware 10.2
Posts: 276

Rep: Reputation: 30
while(1) !!!!

infinite loops aren't very efficient. O.o
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
kacpid eating cpu 99% cpu time! tjclancy Linux - Software 13 09-19-2011 03:08 PM
please help guys, wrong cpu speed shown with a regular non-mobile cpu !! : ( maku Linux - Hardware 1 01-02-2005 02:03 PM
CPU Utilization too Low With Cycle Hungry App sgovind Linux - Software 3 03-09-2004 06:29 PM
RH9 Memory Hungry? Can I SLim it down? tombomb300 Red Hat 2 11-23-2003 04:01 AM
x server is cpu hungry ? juby Linux - General 2 02-17-2003 10:40 PM


All times are GMT -5. The time now is 06:18 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration