Re: how to let all others run

From: Richard B. Johnson (root@chaos.analogic.com)
Date: Thu Apr 05 2001 - 11:52:28 EST


On 5 Apr 2001, John Fremlin wrote:

> "Richard B. Johnson" <root@chaos.analogic.com> writes:
>
> > On 4 Apr 2001, John Fremlin wrote:
> > >
> > > Hi Oliver!
> > >
> > > Oliver Neukum <Oliver.Neukum@lrz.uni-muenchen.de> writes:
> > >
> > > > is there a way to let all other runable tasks run until they block
> > > > or return to user space, before the task wishing to do so is run
> > > > again ?
> > >
> > > Are you trying to do this in kernel or something? From userspace you
> > > can use nice(2) then sched_yield(2), though I don't know if the linux
> > > implementations will guarrantee anything.
> > >
> >
> > I recommend using usleep(0) instead of sched_yield(). Last time I
> > checked, sched_yield() seemed to spin and eat CPU cycles, usleep(0)
> > always gives up the CPU.
>
> What is wrong with this? sched_yield only yields to processes with
> lower priority (hence suggestion to use nice(2)). Does sched_yield()
> fail to yield in cases when a higher priority process wants to run?
> usleep() wastes time if no other such process is waiting, surely?
>
> [...]

Only an observation:

main()
{
    nice(19);
    for(;;)
        sched_yield();
}

does...

 12:45pm up 19:10, 6 users, load average: 0.66, 0.19, 0.06
31 processes: 29 sleeping, 2 running, 0 zombie, 0 stopped
CPU states: 44.1% user, 56.9% system, 99.1% nice, 0.0% idle
Mem: 320368K av, 309608K used, 10760K free, 0K shrd, 12688K buff
Swap: 0K av, 0K used, 0K free 188272K cached

 PID USER PRI NI SIZE RSS SHARE STAT LIB %CPU %MEM TIME COMMAND
15902 root 20 19 212 212 176 R N 0 99.1 0.0 1:05 xxx
15921 root 13 0 568 568 432 R 0 1.9 0.1 0:00 top
    1 root 8 0 148 148 116 S 0 0.0 0.0 0:00 init

It consumes 99.1 percent CPU, just spinning.

However,

        for(;;)
            usleep(0);

Doesn't even show on `top`. Further, it gets the CPU about 100 times
a second (HZ). This is normally what you want for something that
polls, buts needs to give up the CPU so that whatever it's waiting
for can get done as soon as possible.

Cheers,
Dick Johnson

Penguin : Linux version 2.4.1 on an i686 machine (799.53 BogoMips).

"Memory is like gasoline. You use it up when you are running. Of
course you get it all back when you reboot..."; Actual explanation
obtained from the Micro$oft help desk.

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/



This archive was generated by hypermail 2b29 : Sat Apr 07 2001 - 21:00:17 EST