Unix Technical Forum

cpu load 2

This is a discussion on cpu load 2 within the Slackware Linux Support forums, part of the Unix Operating Systems category; --> Dear all, My first question has been answered, thnx a lot!!! Now, to solve the load problem i've noticed ...


Go Back   Unix Technical Forum > Unix Operating Systems > Slackware Linux Support

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-21-2008, 04:47 AM
TheMaxer
 
Posts: n/a
Default cpu load 2

Dear all,

My first question has been answered, thnx a lot!!!

Now, to solve the load problem i've noticed that the program uses a
tight for(; loop, effectively spinning as hard as possible, whilst
not needed (explaining the cpu hogging). Now, I could use e.g.
sleep(), nanosleep() etc, but these seem to have side effects, either
not actually sleeping or very course sleep intervals. Is there some
kind of yield() that allows me to do just one for(; loop and then
relinquish the remainder of the time slice allocated to the app?

e.g.:

for (;
{
dosomething();
yield_rest_of_timeslice();
}

Thnx

Max
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-21-2008, 04:47 AM
Lew Pitcher
 
Posts: n/a
Default Re: cpu load 2

On Feb 11, 10:58 am, TheMaxer <m...@max-irene.demon.nl> wrote:
> Dear all,
>
> My first question has been answered, thnx a lot!!!
>
> Now, to solve the load problem i've noticed that the program uses a
> tight for(; loop, effectively spinning as hard as possible, whilst
> not needed (explaining the cpu hogging).

[snip]
> Is there some
> kind of yield() that allows me to do just one for(; loop and then
> relinquish the remainder of the time slice allocated to the app?

[snip]

Yes, sched_yield(2) (see "man 2 sched_yield")
.. . .

SCHED_YIELD(2) Linux Programmer's Manual
SCHED_YIELD(2)

NAME
sched_yield - yield the processor

SYNOPSIS
#include <sched.h>

int sched_yield(void);

DESCRIPTION
A process can relinquish the processor voluntarily without
blocking by
calling sched_yield(). The process will then be moved to the
end of
the queue for its static priority and a new process gets to
run.

Note: If the current process is the only process in the
highest prior-
ity list at that time, this process will continue to run after
a call
to sched_yield().

POSIX systems on which sched_yield() is available define
_POSIX_PRIOR-
ITY_SCHEDULING in <unistd.h>.

RETURN VALUE
On success, sched_yield() returns 0. On error, -1 is
returned, and
errno is set appropriately.

CONFORMING TO
POSIX.1-2001.

SEE ALSO
sched_setscheduler(2) for a description of Linux scheduling.

Programming for the real world - POSIX.4 by Bill O.
Gallmeister,
O'Reilly & Associates, Inc., ISBN 1-56592-074-0

Linux 1.3.81 1996-04-10
SCHED_YIELD(2)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-21-2008, 04:47 AM
TheMaxer
 
Posts: n/a
Default Re: cpu load 2

On 11 feb, 19:33, Lew Pitcher <lpitc...@teksavvy.com> wrote:
> On Feb 11, 10:58 am, TheMaxer <m...@max-irene.demon.nl> wrote:> Dear all,
>
> Yes, sched_yield(2) (see "man 2 sched_yield")



Thnx Lew!

However, the process is the only one running, therefore sched_yield()
doesn't do something. A sleep() gives me the desired cpu load, but is
too coarse. I want something like, wake-up every 20msec or so and then
quickly dosomething() and sleep again....

Max
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-21-2008, 04:47 AM
TheMaxer
 
Posts: n/a
Default Re: cpu load 2

On 12 feb, 09:13, TheMaxer <m...@max-irene.demon.nl> wrote:
> On 11 feb, 19:33, Lew Pitcher <lpitc...@teksavvy.com> wrote:
>
> > On Feb 11, 10:58 am, TheMaxer <m...@max-irene.demon.nl> wrote:> Dear all,

>
> > Yes, sched_yield(2) (see "man 2 sched_yield")

>
> Thnx Lew!
>
> However, the process is the only one running, therefore sched_yield()
> doesn't do something. A sleep() gives me the desired cpu load, but is
> too coarse. I want something like, wake-up every 20msec or so and then
> quickly dosomething() and sleep again....
>
> Max


dear all,

It appears that nanosleep() is the way to go, after all. The issue
being that nanosleep() degrades to a busy waiting loop below certain
sleep values. This is ok from a nanosleep implementation point of
view.

I managed to change the app, so a bigger value for the sleep was
acceptable, making the app actually sleep between loop iterations.
Problem solved ;-)

Thanks.

Max
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 09:10 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com