Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.arch.embedded > #29047

Re: From cooperative to preemptive scheduler: a real example

From Don Y <blockedofcourse@foo.invalid>
Newsgroups comp.arch.embedded
Subject Re: From cooperative to preemptive scheduler: a real example
Date 2020-01-06 19:37 -0700
Organization A noiseless patient Spider
Message-ID <qv0qte$md6$1@dont-email.me> (permalink)
References <qv0lmu$up1$1@dont-email.me>

Show all headers | View raw


On 1/6/2020 6:08 PM, pozz wrote:

[ 8< ]

> My impression is that a very simple code is cluttered with synchronization 
> things that decrease readability and maintainability and increase complexity. 
> Why? Just to use preemption?

The "clutter" is introduced because your "problem" inherently involves
conflict; you're allowing two competing uses for a single resource.

The use of the synchronization primitive OVERTLY acknowledges this
issue/possibility -- lest (subsequent) another developer fail to
recognize that the possibility exists (i.e., "latent bug").

> Again my impression is that preemption is NOT GOOD and must be avoided if it 
> isn't required.

"Multiplication is NOT GOOD and must be avoided if it isn't required (i.e.,
if you can use repeated ADDITIONs, instead)"

> So the question is: when a preemption scheduler is needed? Could you give a 
> real example?

The "scheduler" is present in any multitasking system -- cooperative or
preemptive.  SOMETHING has to decide who to give the processor to when the 
currently executing task gives up control (or, has it removed from it)

In your cooperative examples, the "while()" is used to implement the scheduler:
when one task() "returns", the one listed on the next line (of the while loop)
is given control... "scheduled" to run.

Don't conflate "big loop" with "cooperative".

>  From what I have understood, preemption could solve real-time requirement.

Preemption, like any capability, brings with it assets and liabilities.
Imagine you were tasked with building a box that blinked lights (XMAS
lights!) at different/varying rates.  The box has a dozen solid state
switches that control the individual lights (or, "light strands").

It would be really easy -- and intuitive -- to write:
     void lights1() {
        while(FOREVER) {
          light(1,ON);  sleep(500ms);
          light(1,OFF); sleep(279ms);
        }
     }

     void lights2() {
        while(FOREVER) {
          light(2,ON);  sleep(100ms);
          light(2,OFF); sleep(50ms);
        }
     }

     void lights3() {
        while(FOREVER) {
          ontime = (10.0 * rand() ) / RAND_MAX;
          light(3,ON);  sleep(ontime);
          light(3,OFF); sleep(10.0 - ontime);
        }
     }

etc.  No silly "yields" to get in the way.  No need for synchronization
primitives, either, because nothing is SHARED!

[Contrived example but you'll find that there are many cases of
tasks co-executing that are NOT sharing anything (other than the
processor)]

There are other classes of problems where the problem lends itself, naturally,
to "peaceful" sharing -- where you're not in conflict with another.  And,
other techniques to hide the sharing mitigation in other mechanisms.

Preemption lets you code AS IF you were the sole owner of the processor...
EXCEPT when you need to share something (which would imply that you are
NOT the sole owner -- at least at THAT time!  :> )

The downside to cooperative multitasking is that *it* clutters your
code -- with all those yield()s -- and requires you to keep track of
how "long" you've hogged the CPU in the time since your last yield
(because that time gets reflected to all subsequent task runnings).

When I write code in a cooperative environment, I *litter* the code
with yield()s to keep *reaction* times (of other tasks) short.  This
then means yield() has to run like greased lightning lest it impact
overall performance (because it is pure overhead!)

Back to comp.arch.embedded | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

From cooperative to preemptive scheduler: a real example pozz <pozzugno@gmail.com> - 2020-01-07 02:08 +0100
  Re: From cooperative to preemptive scheduler: a real example Don Y <blockedofcourse@foo.invalid> - 2020-01-06 19:37 -0700
    Re: From cooperative to preemptive scheduler: a real example pozz <pozzugno@gmail.com> - 2020-01-07 10:11 +0100
      Re: From cooperative to preemptive scheduler: a real example Don Y <blockedofcourse@foo.invalid> - 2020-01-07 07:51 -0700
        Re: From cooperative to preemptive scheduler: a real example pozz <pozzugno@gmail.com> - 2020-01-07 23:51 +0100
          Re: From cooperative to preemptive scheduler: a real example Niklas Holsti <niklas.holsti@tidorum.invalid> - 2020-01-09 01:39 +0200
            Re: From cooperative to preemptive scheduler: a real example pozz <pozzugno@gmail.com> - 2020-01-10 00:42 +0100
              Re: From cooperative to preemptive scheduler: a real example Niklas Holsti <niklas.holsti@tidorum.invalid> - 2020-01-15 18:39 +0200
          Re: From cooperative to preemptive scheduler: a real example Don Y <blockedofcourse@foo.invalid> - 2020-01-08 23:12 -0700
            Re: From cooperative to preemptive scheduler: a real example pozz <pozzugno@gmail.com> - 2020-01-10 00:49 +0100
          Re: From cooperative to preemptive scheduler: a real example Don Y <blockedofcourse@foo.invalid> - 2020-01-08 23:12 -0700
          Re: From cooperative to preemptive scheduler: a real example Don Y <blockedofcourse@foo.invalid> - 2020-01-09 01:33 -0700
  Re: From cooperative to preemptive scheduler: a real example Niklas Holsti <niklas.holsti@tidorum.invalid> - 2020-01-07 09:38 +0200
    Re: From cooperative to preemptive scheduler: a real example pozz <pozzugno@gmail.com> - 2020-01-08 00:02 +0100
      Re: From cooperative to preemptive scheduler: a real example Niklas Holsti <niklas.holsti@tidorum.invalid> - 2020-01-09 00:54 +0200
        Re: From cooperative to preemptive scheduler: a real example pozz <pozzugno@gmail.com> - 2020-01-09 12:19 +0100
          Re: From cooperative to preemptive scheduler: a real example Niklas Holsti <niklas.holsti@tidorum.invalid> - 2020-01-09 14:03 +0200
            Re: From cooperative to preemptive scheduler: a real example pozz <pozzugno@gmail.com> - 2020-01-10 00:32 +0100
              Re: From cooperative to preemptive scheduler: a real example Niklas Holsti <niklas.holsti@tidorum.invalid> - 2020-01-15 18:53 +0200
                Re: From cooperative to preemptive scheduler: a real example pozz <pozzugno@gmail.com> - 2020-01-16 14:48 +0100
                Re: From cooperative to preemptive scheduler: a real example upsidedown@downunder.com - 2020-01-16 21:04 +0200
                Re: From cooperative to preemptive scheduler: a real example Niklas Holsti <niklas.holsti@tidorum.invalid> - 2020-01-16 22:24 +0200
                Re: From cooperative to preemptive scheduler: a real example pozz <pozzugno@gmail.com> - 2020-01-17 02:05 +0100
                Re: From cooperative to preemptive scheduler: a real example Niklas Holsti <niklas.holsti@tidorum.invalid> - 2020-01-18 23:03 +0200
  Re: From cooperative to preemptive scheduler [Stack] pozz <pozzugno@gmail.com> - 2020-01-14 13:27 +0100
    Re: From cooperative to preemptive scheduler [Stack] Hans-Bernhard Bröker <HBBroeker@t-online.de> - 2020-01-14 20:10 +0100
      Re: From cooperative to preemptive scheduler [Stack] pozz <pozzugno@gmail.com> - 2020-01-15 09:36 +0100
        Re: From cooperative to preemptive scheduler [Stack] Stef <stef33d@yahooI-N-V-A-L-I-D.com.invalid> - 2020-01-15 11:30 +0100
          Re: From cooperative to preemptive scheduler [Stack] pozz <pozzugno@gmail.com> - 2020-01-15 11:51 +0100
        Re: From cooperative to preemptive scheduler [Stack] Hans-Bernhard Bröker <HBBroeker@t-online.de> - 2020-01-15 21:51 +0100
    Re: From cooperative to preemptive scheduler [Stack] Niklas Holsti <niklas.holsti@tidorum.invalid> - 2020-01-15 21:26 +0200
      Re: From cooperative to preemptive scheduler [Stack] Hans-Bernhard Bröker <HBBroeker@t-online.de> - 2020-01-15 22:12 +0100
        Re: From cooperative to preemptive scheduler [Stack] Niklas Holsti <niklas.holsti@tidorum.invalid> - 2020-01-15 23:30 +0200

csiph-web