cpu load 2 - Slackware

This is a discussion on cpu load 2 - Slackware ; 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). ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: cpu load 2

  1. 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

  2. Re: cpu load 2

    On Feb 11, 10:58 am, TheMaxer 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

    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 .

    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)

  3. Re: cpu load 2

    On 11 feb, 19:33, Lew Pitcher wrote:
    > On Feb 11, 10:58 am, TheMaxer 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

  4. Re: cpu load 2

    On 12 feb, 09:13, TheMaxer wrote:
    > On 11 feb, 19:33, Lew Pitcher wrote:
    >
    > > On Feb 11, 10:58 am, TheMaxer 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

+ Reply to Thread