limit CPU usage in C#

  • Thread starter Thread starter tracernet_v2
  • Start date Start date
T

tracernet_v2

good day...

i noticed that when moving 750mb (multiple) files, it only used about
20% of the CPU usage...

i thought i could also do this 1 to my application, so that it wont eat
up much of my CPU usage and hang up eventually...

do you have codes for this 1??? hope someone here can give me this...
thanks....
 
Hi,

Moving files is an OS feature.
If OS is configured properly and drives are proper and OS is in good mood
then moving files around won't eat your CPU time.
Just use File.Copy.
 
tracernet_v2 said:
good day...

i noticed that when moving 750mb (multiple) files, it only used about
20% of the CPU usage...

i thought i could also do this 1 to my application, so that it wont eat
up much of my CPU usage and hang up eventually...

do you have codes for this 1??? hope someone here can give me this...
thanks....

Moving files is an IO bound operation, not a CPU intensive one. The fact
that only 20% of the time is spent executing instructions is because most of
the time the CPU is waiting for IO completion.
A CPU bound application, that is an application that continiously executes
instructions without some explicit waits (say sleep) or without performing
IO operations, will use the CPU for close to 100%. There is nothing in the
system that can set a CPU quota per process, all you can do is give up your
CPU slice by inserting wait's in your program, but again why would you do
this, CPU's are meant to execute instructions not to wait.

Willy.
 
There is nothing in the
system that can set a CPU quota per process, all you can do is give up your
CPU slice by inserting wait's in your program, but again why would you do
this, CPU's are meant to execute instructions not to wait.

Willy.

If you have a long-running CPU-intensive task, you might want to limit
the impact on the rest of the system by using just 80% (or so) of the
available cycles.

Hans Kesting
 
Hans said:
If you have a long-running CPU-intensive task, you might want to limit
the impact on the rest of the system by using just 80% (or so) of the
available cycles.

The normal way of doing this is by setting the priorities of the
various threads to be low. That means that when other things need
doing, they can take the CPU - but when there aren't, the CPU-intensive
code can run at full speed. There's no point in only using 80% of the
CPU time when nothing else needs to run.

Jon
 
Hans Kesting said:
If you have a long-running CPU-intensive task, you might want to limit the
impact on the rest of the system by using just 80% (or so) of the
available cycles.

Hans Kesting

If there are ready to run threads in the system, they will get their share
of the CPU even if you have a long running CPU intensive task running, this
is how the scheduler works.
If you need to give more CPU time to other tasks that the long running task,
you can lower the active thread's priority, but doing so will not restrict
the CPU time to a certain level, all depends on the level of activity of the
other threads, if they are also CPU bound your thread's share will drop far
below 80%, if there are no realy active threads in the system, your thread
will still occupy most of the CPU time.
In short, you can't say I want this thread to be at most 80% on the CPU.
Note that it's not impossible to implement a kind of watch dog that changes
the thread(s) priority or reduces the thread's quantum of a process
depending on it's current activity and as such limit it's share of the
CPU's, SQL server has this so called throttle/governor built-in to prevent
tasks from consuming more than x resources (CPU and others).

Willy.
 
Back
Top