By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,757 Members | 1,684 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,757 IT Pros & Developers. It's quick & easy.

is it possible?

P: n/a
Is it possible to force a limit on the amount of CPU a task is taking?

I am thinking of writing a simple app that will let me force other
applications to "be nice" by only using so much CPU.

Is this possible ... does anything like this exist?
Mar 31 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a


gregory_may wrote:
Is it possible to force a limit on the amount of CPU a task is taking?

I am thinking of writing a simple app that will let me force other
applications to "be nice" by only using so much CPU.

Is this possible ... does anything like this exist?


You can set the priority of the process lower, but that's not what you
are after, is it?

In unix you could send SIGSTOP and SIGCONT to the process, I don't think
there is anything similar in the win32 api.

Perhaps this can be of help to you:
http://www.codeproject.com/threads/pausep.asp

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Mar 31 '06 #2

P: n/a
<"gregory_may" <None>> wrote:
Is it possible to force a limit on the amount of CPU a task is taking?

I am thinking of writing a simple app that will let me force other
applications to "be nice" by only using so much CPU.

Is this possible ... does anything like this exist?


Well, you can set the thread's priority to a low value - it will still
take all the CPU when there's nothing else running, but other things
will take priority over it.

Thread priorities are somewhat heuristic - it's more of an indication
than a guarantee. It may well help you though.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 31 '06 #3

P: n/a
So, If I spin up a "fake" idle process to steal CPU from the system, could I
allocate cycles back to processes I wanted to have some extra time (like the
UI).

"gregory_may" <None> wrote in message
news:O0**************@TK2MSFTNGP11.phx.gbl...
Is it possible to force a limit on the amount of CPU a task is taking?

I am thinking of writing a simple app that will let me force other
applications to "be nice" by only using so much CPU.

Is this possible ... does anything like this exist?

Apr 3 '06 #4

P: n/a
I really don't understand why you ever need this. The OS will assign thread
quamtums to the process depending on their activity and priority, the OS
will boost priority levels of UI threads and threads that return from a
pending IO and the OS will give all threads a fair share of the CPU
resources when there is contention. At the application level there is simply
no way to achieve what you are after, you can't and you shouldn't even try
to take over the role of the OS scheduler.

Willy.

"gregory_may" <None> wrote in message
news:ur**************@TK2MSFTNGP11.phx.gbl...
| So, If I spin up a "fake" idle process to steal CPU from the system, could
I
| allocate cycles back to processes I wanted to have some extra time (like
the
| UI).
|
| "gregory_may" <None> wrote in message
| news:O0**************@TK2MSFTNGP11.phx.gbl...
| > Is it possible to force a limit on the amount of CPU a task is taking?
| >
| > I am thinking of writing a simple app that will let me force other
| > applications to "be nice" by only using so much CPU.
| >
| > Is this possible ... does anything like this exist?
| >
|
|
Apr 4 '06 #5

P: n/a
I guess the short answer is because I as the user would like to work around
a resource issues.

Many applications thrash the disk or another resource killing the
performance of my machine. Acrobat, Flash, Outlook, Defrag Jobs, compiles,
SQL Queries, etc. I would love to halt some of the background stuff for a
few seconds, do what I want, then let Windows take control again. Or if it
could give significant priority to the "Active" window, that would solve
most of my issues.
"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
news:eT**************@TK2MSFTNGP14.phx.gbl...
I really don't understand why you ever need this. The OS will assign thread
quamtums to the process depending on their activity and priority, the OS
will boost priority levels of UI threads and threads that return from a
pending IO and the OS will give all threads a fair share of the CPU
resources when there is contention. At the application level there is
simply
no way to achieve what you are after, you can't and you shouldn't even try
to take over the role of the OS scheduler.

Willy.

"gregory_may" <None> wrote in message
news:ur**************@TK2MSFTNGP11.phx.gbl...
| So, If I spin up a "fake" idle process to steal CPU from the system,
could
I
| allocate cycles back to processes I wanted to have some extra time (like
the
| UI).
|
| "gregory_may" <None> wrote in message
| news:O0**************@TK2MSFTNGP11.phx.gbl...
| > Is it possible to force a limit on the amount of CPU a task is taking?
| >
| > I am thinking of writing a simple app that will let me force other
| > applications to "be nice" by only using so much CPU.
| >
| > Is this possible ... does anything like this exist?
| >
|
|

Apr 7 '06 #6

P: n/a
Using the technique Helge mentioned on a hot key to pause all applications
except the active window/application ... now that could be interesting:

http://www.codeproject.com/threads/pausep.asp

"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
news:eT**************@TK2MSFTNGP14.phx.gbl...
I really don't understand why you ever need this. The OS will assign thread
quamtums to the process depending on their activity and priority, the OS
will boost priority levels of UI threads and threads that return from a
pending IO and the OS will give all threads a fair share of the CPU
resources when there is contention. At the application level there is
simply
no way to achieve what you are after, you can't and you shouldn't even try
to take over the role of the OS scheduler.

Willy.

"gregory_may" <None> wrote in message
news:ur**************@TK2MSFTNGP11.phx.gbl...
| So, If I spin up a "fake" idle process to steal CPU from the system,
could
I
| allocate cycles back to processes I wanted to have some extra time (like
the
| UI).
|
| "gregory_may" <None> wrote in message
| news:O0**************@TK2MSFTNGP11.phx.gbl...
| > Is it possible to force a limit on the amount of CPU a task is taking?
| >
| > I am thinking of writing a simple app that will let me force other
| > applications to "be nice" by only using so much CPU.
| >
| > Is this possible ... does anything like this exist?
| >
|
|

Apr 7 '06 #7

P: n/a
This is a piece of crap, especially this:

<
The risks with this aproach.
....Not all programs are well written. Not all programs are made to be
suspended, specially the multithreaded ones...

is really hilarious, his utility is calling "SuspendThread" to suspend
arbitrary threads in other processes, a function that was designed to be
used by debuggers, or at best, to suspend the current process thread(s),
provided you know exactly what they are doing, something you never know for
sure. Talking about well "written programs", this utility isn't one of them
at all.

Willy.

"gregory_may" <None> wrote in message
news:Os**************@TK2MSFTNGP03.phx.gbl...

| Using the technique Helge mentioned on a hot key to pause all applications
| except the active window/application ... now that could be interesting:
|
| http://www.codeproject.com/threads/pausep.asp
|
|
|
| "Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
| news:eT**************@TK2MSFTNGP14.phx.gbl...
| >I really don't understand why you ever need this. The OS will assign
thread
| > quamtums to the process depending on their activity and priority, the OS
| > will boost priority levels of UI threads and threads that return from a
| > pending IO and the OS will give all threads a fair share of the CPU
| > resources when there is contention. At the application level there is
| > simply
| > no way to achieve what you are after, you can't and you shouldn't even
try
| > to take over the role of the OS scheduler.
| >
| > Willy.
| >
| > "gregory_may" <None> wrote in message
| > news:ur**************@TK2MSFTNGP11.phx.gbl...
| > | So, If I spin up a "fake" idle process to steal CPU from the system,
| > could
| > I
| > | allocate cycles back to processes I wanted to have some extra time
(like
| > the
| > | UI).
| > |
| > | "gregory_may" <None> wrote in message
| > | news:O0**************@TK2MSFTNGP11.phx.gbl...
| > | > Is it possible to force a limit on the amount of CPU a task is
taking?
| > | >
| > | > I am thinking of writing a simple app that will let me force other
| > | > applications to "be nice" by only using so much CPU.
| > | >
| > | > Is this possible ... does anything like this exist?
| > | >
| > |
| > |
| >
| >
|
|
Apr 7 '06 #8

P: n/a
I think your holding back something ... feel free to just say what's on your
mind.

I think I got my question answered. What I am after is really not
possible(unfortunately). There are a few hacks that might interesting to
try, but nothing that looks production quality.

Thanks for your feedback.

"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
This is a piece of crap, especially this:

<
The risks with this aproach.
...Not all programs are well written. Not all programs are made to be
suspended, specially the multithreaded ones...

is really hilarious, his utility is calling "SuspendThread" to suspend
arbitrary threads in other processes, a function that was designed to be
used by debuggers, or at best, to suspend the current process thread(s),
provided you know exactly what they are doing, something you never know
for
sure. Talking about well "written programs", this utility isn't one of
them
at all.

Willy.

"gregory_may" <None> wrote in message
news:Os**************@TK2MSFTNGP03.phx.gbl...

| Using the technique Helge mentioned on a hot key to pause all
applications
| except the active window/application ... now that could be interesting:
|
| http://www.codeproject.com/threads/pausep.asp
|
|
|
| "Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
| news:eT**************@TK2MSFTNGP14.phx.gbl...
| >I really don't understand why you ever need this. The OS will assign
thread
| > quamtums to the process depending on their activity and priority, the
OS
| > will boost priority levels of UI threads and threads that return from
a
| > pending IO and the OS will give all threads a fair share of the CPU
| > resources when there is contention. At the application level there is
| > simply
| > no way to achieve what you are after, you can't and you shouldn't even
try
| > to take over the role of the OS scheduler.
| >
| > Willy.
| >
| > "gregory_may" <None> wrote in message
| > news:ur**************@TK2MSFTNGP11.phx.gbl...
| > | So, If I spin up a "fake" idle process to steal CPU from the system,
| > could
| > I
| > | allocate cycles back to processes I wanted to have some extra time
(like
| > the
| > | UI).
| > |
| > | "gregory_may" <None> wrote in message
| > | news:O0**************@TK2MSFTNGP11.phx.gbl...
| > | > Is it possible to force a limit on the amount of CPU a task is
taking?
| > | >
| > | > I am thinking of writing a simple app that will let me force other
| > | > applications to "be nice" by only using so much CPU.
| > | >
| > | > Is this possible ... does anything like this exist?
| > | >
| > |
| > |
| >
| >
|
|

Apr 7 '06 #9

P: n/a
"gregory_may" <None> wrote:
I think I got my question answered. What I am after is really not
possible(unfortunately). There are a few hacks that might interesting to
try, but nothing that looks production quality.


I wouldn't draw that conclusion.

Read the MSDN topic "Scheduling Priorities". It explains that Windows
uses a "waterfall" scheduler, where if there exists ANY
higher-priority thread with stuff to do, then the lower-priority
threads will be starved.

Therefore if your program runs at a higher priority than
Outlook/Adobe/whatever, and is cpu-bound, then those other processes
won't even get a look-in. You can experiment with this using the Task
Manager as follows. Set the resource-hog to "BelowNormal" priority
class, and run another cpu-bound process with is at Normal priority.
I'm pretty sure that for the apps you mentioned
(Outlook/Photoshop/whatever) you'll find that they quieten down
enough.

Notionally, setting their priority to below IDLE should halt them
completely. I haven't tested this.
Unfortunately, the fundamental problem is that bandwidth to the disk
isn't enough, and windows does NOT throttle disk-use-priority in the
same way it throttles CPU-use-priority. That's why you have to
approximate with cpu-priority.

--
Lucian
Apr 7 '06 #10

P: n/a

"gregory_may" <None> wrote in message
news:u3**************@TK2MSFTNGP03.phx.gbl...
|I think your holding back something ... feel free to just say what's on
your
| mind.
|

I'm not holding back anything, what makes you think that?

| I think I got my question answered. What I am after is really not
| possible(unfortunately). There are a few hacks that might interesting to
| try, but nothing that looks production quality.
|

What you are looking for (or something similar) exists for W2K3 Datacenter
and Enterprise Edition, it's called Windows System Resource Manager (WSRM),
but these systems need to have a lot of resources available to start with.
The WSRM core runs at the kernel level, here it interacts with the System
Scheduler, the Memory manager and the IO manager. The reason it's
implemented at this level is because it's impossible to control the
scheduling and memory consumpton of arbitrary process at the user level. All
you can do at the user level is boost or reduce the priority level of a
specific thread in YOUR OWN process, but be sure you know the impact this
might have on other threads (user and system) running in the system. Say an
interactive program, needs to run a task as fast as possible, one way to
achieve this is to spin a thread with elevated priority (say,
ThreadPriority.Highest). This thread has a fair chance to get scheduled
before most other threads in the system, but, your UI will freeze as long as
this thread runs, other threads like SQL server worker threads will not get
the CPU either (well, the system will prevent starvation, so the will get
scheduled once in a while).

Willy.

Apr 7 '06 #11

P: n/a
"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote:
All you can do at the user level is boost or reduce the priority level of a
specific thread in YOUR OWN process


Are you sure? I just tried a test program which enumerated all the
other processes, then tried to open them with PROCESS_SET_INFORMATION.
I was succesfully able to open most of them. And I was able to
SetPriorityClass on them without error.

I presume the Task Manager uses the same APIs to alter priority class
of other applications as well. Note: my app wasn't a debugger, and
wasn't running within the VS debugger or anything like that.

--
Lucian
Apr 7 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.