Connecting Tech Pros Worldwide Help | Site Map

limit CPU usage in C#

tracernet_v2
Guest
 
Posts: n/a
#1: Nov 17 '05
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....

Miha Markic [MVP C#]
Guest
 
Posts: n/a
#2: Nov 17 '05

re: limit CPU usage in C#


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.

--
Miha Markic [MVP C#]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

"tracernet_v2" <hykurama@gmail.com> wrote in message
news:1132215532.049907.123390@g14g2000cwa.googlegr oups.com...[color=blue]
> 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....
>[/color]


Willy Denoyette [MVP]
Guest
 
Posts: n/a
#3: Nov 17 '05

re: limit CPU usage in C#



"tracernet_v2" <hykurama@gmail.com> wrote in message
news:1132215532.049907.123390@g14g2000cwa.googlegr oups.com...[color=blue]
> 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....
>[/color]

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.


Hans Kesting
Guest
 
Posts: n/a
#4: Nov 17 '05

re: limit CPU usage in C#


> There is nothing in the[color=blue]
> 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.[/color]

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


Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#5: Nov 17 '05

re: limit CPU usage in C#


Hans Kesting wrote:[color=blue][color=green]
> > 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.[/color]
>
> 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.[/color]

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

Willy Denoyette [MVP]
Guest
 
Posts: n/a
#6: Nov 17 '05

re: limit CPU usage in C#



"Hans Kesting" <news.1.hansdk@spamgourmet.com> wrote in message
news:mn.8aee7d5bc66cac5d.41829@spamgourmet.com...[color=blue][color=green]
>> 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.[/color]
>
> 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
>
>[/color]

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.


Closed Thread