views:

1714

answers:

9

Of late, I'm becoming more health oriented when constructing my program, I have observed that most of programs take 2 or 3 minutes to execute and when I check on the task scheduler, I see that they consume 100% of CPU usage, can I limit this usage programatically in code? This will certainly enable me to run multiple programs at a given time.

Thanks, Nidhi

+24  A: 

That's not your concern... It's the job of the operating system to distribute processor time between running processes. If you'd like to give other processes first crack at getting their stuff done, then simply reduce the priority of your own process by modifying the Process.PriorityClass value for it.

See also: Windows Equivalent of ‘nice’

Shog9
So True.
micahtan
And it would be wasteful to NOT use 100% if it's available and can be utilized. Of course, this may reflect poor design in the program, so make sure you don't have any Schlemiel algorithms. See http://en.wikipedia.org/wiki/Schlemiel_the_painter%27s_Algorithm
Daniel Straight
Nice, I'll use this on our server applications.
Carra
What about something like a game, where you might want the game loop to become idle when the game is paused or minimized? Certainly reducing utilization would be appropriate there.
Matt Olenik
@Matt: well, yeah, if you don't have anything to do, then by all means do nothing! But still, you're better off letting the OS handle this (via a blocking system call of some sort) - you just can't assume that your program ever has enough information about the system as a whole to throttle itself effectively, and may well end up being counter-productive (imagine a game that forced its loop down to 1% CPU while minimized... sounds great, right? But that's still enough to burn through your laptop battery...
Shog9
+1. Your system should be using all as much of it's CPU and memory as possible, otherwise it's wasted. The OS should decide how to allocate those resources, not any single program. In fact, Windows does that with memory, I believe. If you don't use it, it starts using it itself for disk buffers and such.
paxdiablo
+2  A: 

You can run your program in a thread with a lower threadpriority, the rest is up to your operating system. Having a process eat up 100% of your CPU is not bad. My SETI is usually taking up all my remaining CPU time without bothering my other programs. It only gets a problem when your thread gets priority over more important programs.

Carra
IIRC SETI only uses spare clock cycles so while the machine shows 100% cpu it is still responsive and as soon as something else requires processing power and starts to use the cpu the number of spare clock cycles is reduced so SETI gets limited not other processes.
OneSHOT
This is because SETI runs with the lowest allowed thread priority, and the OS handles the throttling.
Gregory
What is the definition of "spare"? SETI uses CPU cycles which are "spare" by setting it's priority very low (lower than the other programs which you want to remain responsive). If there are higher priority processes running then SETI won't be given any cycles.
David
+7  A: 

I honestly think rather than worry about trying to limit CPU utilization by your app, you should focus more of your energies on profiling the application to uncover and correct bottlenecks and inefficiencies that may exist.

itsmatt
+2  A: 

If you code is running at all, it is at 100%

I suppose slipping in some sleeps might have an effect.

I have to wonder about that 2-3 minute figure. I've seen it too, and I suppose it's loading and initializing lots of stuff I probably don't really need.

Mike Dunlavey
+3  A: 

If there is no other task running, is it wrong for your app to use all the cpu capacity that is available? It is available, as in it's there and it is free to use. So use it!

If you somehow limit the cpu usage of your task, it will take longer to complete. But it will still take the same number of cpu cycles, so you gain nothing. You just slow down your application.

Don't do it. Don't even try it. There's no reason why you should.

Treb
Yeah, why should "System Idle Task" have all the fun?
paxdiablo
+3  A: 

I think what you need to do is to understand the performance problem in your application instead of trying to put a cap on the CPU usage. You Can use Visual Studio Profiler to see why you application is taking 100% CPU for 2-3 minutes in the first place. This should reveal the hot spot in your app, and then you can be able to address this issue.

If you are asking in general regarding how to do resource throttling in windows, then you can look at the "Task" objects, Job objects allows you to set limits such as Working set, process priority...etc.

You can check out the Job objects documentation here http://msdn.microsoft.com/en-ca/library/ms684161(VS.85).aspx Hope this helps. Thanks

mfawzymkh
A: 

I honestly believe that there is still an unanswered question here. I think I understand what he is going through as I have a similar problem. My code, when it needs to REALLY think, will take up ALL available CPU, and because of that will cause my entire computer to LOCK while it's doing it's thing. The mouse moves, yet if I were to try to do anything it will simply put the [Program Not Responding(Busy)] addition to the title bar.... then I have to wait until it is done thinking, or processing a LARGE array of names, or addresses, that each one needs to have work done with it.

If there is a way to have my program not lock up while it's thinking so hard, how would I do that?

Maxor
In Windows, you ALWAYS have to put the UI on a separate thread.
Jacob
Ask a new question. But in the meantime, search background worker, and investigate how to use threading and still maintain interaction with the user.
Gregory
A: 

You could write a Governor class that throttles the CPU usage. This class would contain a utility method that should be called on a regular basis (e.g. calling this utility function within a while loop of your function) by your CPU bound function. The governor would check if the amount of time elapsed exceeded a particular threshold, and then sleep for a period of time so as to not consume all the CPU.

Here's a simple Java implementation off the top of my head (just so you get the idea) that will throttle the CPU usage to 50% if you have a single threaded CPU bound function.

public class Governor { long start_time;

public Governor() { this.start_time = System.currentTimeMillis(); }

public void throttle() { long time_elapsed = System.currentTimeMillis() - this.start_time;

if (time_elapsed > 100) //throttle whenever at least a 100 millis of work has been done
{
  try { Thread.sleep(time_elapsed); } catch (InterruptedExceptione ie) {} //sleep the same amount of time

  this.start_time = System.currentTimeMillis(); //reset after sleeping.
}

} }

Your CPU bound function would instantiate a Governor, and then just call throttle on a regular basis within the function.

mal
-1. This is a terrible idea. Your application should use as many system resources as it can (within reason, using all the handles in windows would be silly). Leave the OS to manage the allocation of such.
Gregory
Not necessarily - you can't rely on other external things managing how your application runs for you. I've seen this used many places before - even some versions of SQL Server have a resource governor. As an example, if your application is providing a service, but includes background tasks that maintain the application that could be CPU bound, the background tasks should not take up all the CPU, while denying service to users. You cannot leave such an implementation to the O.S. to manage. This is just one reason. There are many others.
mal