Re: Changing process priority



In article <a7039$45d67d2d$d4bb52d9$14076@xxxxxxxxxxxxxx>,
John den Haan <nospam@xxxxxxxxxx> wrote:
This question may be basic for you guys, but I haven't found anything
related to it in the FAQ. How do I make sure a simple while(1){} loop
(for example) doesn't consume half my CPU?

If you restrict your choices to those available within the C
standard, then the answer is "Don't write such a loop,
and don't let anyone else write such a loop either."

If you are willing to go outside the facilities provided by standard C,
then you are dealing with OS-specific resource
allocation policies that C doesn't know anything about. In order
to find out about those policies and how to manipulate them,
you would need to ask in a forum that deals with your specific OS.


I think you will find that it is difficult to do what you want,
even with common OS extensions. OS extensions that control
process priority -usually- are set up to change -relative-
priorities, and if there is only one task available to run,
-usually- even if you lower your process priority as far as possible,
the process will consume as much CPU as is not being used for
anything else. Though sometimes there is a "idle process" that runs
an infinite loop consuming all leftover CPU time, and if you manage to
lower your process priority to match that of the idle process,
then your process and the idle process might each average half of
the available CPU time -- but keep in mind that in such a scheme,
the whole CPU time is still being used by -something-.

In order to get a hard guarantee that your process is not going to
use half (or more) of the CPU even when nothing else is waiting to run,
you would probably need to use an OS that provided "real time"
processes and processor quanta. Common operating systems don't
provide those kind of facilities (though I don't know if Linux has
them.)
--
"law -- it's a commodity"
-- Andrew Ryan (The Globe and Mail, 2005/11/26)
.



Relevant Pages

  • Re: MSDN volatile sample
    ... A thread running in a tight loop with Sleepwill consume all remaining ... CPU time, AND won't let lower priority background threads to run. ... You mean Sleepwill consume 100% CPU time? ...
    (microsoft.public.vc.language)
  • Re: xpe ntvdm.exe 100% processor use
    ... loop and polling the keyboard. ... In fact it may not be using or needing the CPU time but because it preempts the 'idle process' it is ...
    (microsoft.public.windowsxp.embedded)
  • Re: Serial Port CPU Usage Problem
    ... Before answering you I would like you to compile and execute test ... consumes a lot of CPU time on your machine? ... transaction does not solve the problem and even one call to (Serial Port) ... ReadFile/WriteFile in a cycle will consume the same CPU time. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Continuous loop handling
    ... > monitor the hardware input in a while loop. ... Tough my gui is active by this ... Is there a way to free up the cpu time ... Sleep somewhere inside your loop. ...
    (microsoft.public.vc.mfc)
  • Re: How Do I Use An SSLServerSocket With ServerSocketChannel?
    ... it's Java 5.0 or give up. ... endless loops of reading a socket and writing to the other one while ... loop and when it does a ServerSocket.accept, it goes into the inner loop ... isn't that going to steal a lot of CPU time ...
    (comp.lang.java.help)