CS 423 Operating System Design: Scheduling Periodic Tasks In - - PowerPoint PPT Presentation

cs 423 operating system design scheduling periodic tasks
SMART_READER_LITE
LIVE PREVIEW

CS 423 Operating System Design: Scheduling Periodic Tasks In - - PowerPoint PPT Presentation

CS 423 Operating System Design: Scheduling Periodic Tasks In Embedded Systems Professor Adam Bates Spring 2017 CS 423: Operating Systems Design Goals for Today Learning Objective: Conclude discussion inner workings of modern OS


slide-1
SLIDE 1

CS 423: Operating Systems Design

Professor Adam Bates Spring 2017

CS 423
 Operating System Design: Scheduling Periodic Tasks In Embedded Systems

slide-2
SLIDE 2

CS 423: Operating Systems Design

  • Learning Objective:
  • Conclude discussion inner workings of modern OS schedulers
  • Explore real-time scheduling for embedded systems
  • Announcements, etc:
  • MP1 is is out! Due Feb 20
  • Midterm Exam — Wednesday March 6th (in-class)
  • Updates to C4 reading lists; should be locked-in for the rest of

the semester now.

2

Goals for Today

Reminder: Please put away devices at the start of class

slide-3
SLIDE 3

CS 423: Operating Systems Design

Example

3

■ Three tasks A, B, C accumulate virtual time at

a rate of 1, 2, and 3, respectively.

■ What is the expected share of the CPU that

each gets?

Q01: A => {A:1, B:0, C:0} Q02: B => {A:1, B:2, C:0} Q03: C => {A:1, B:2, C:3} Q04: A => {A:2, B:2, C:3} Q05: B => {A:2, B:4, C:3} Q06: A => {A:3, B:4, C:3} Q07: A => {A:4, B:4, C:3} Q08: C => {A:4, B:4, C:6} Q09: A => {A:5, B:4, C:6} Q10: B => {A:5, B:6, C:6} Q11: A => {A:6, B:6, C:6} Strategy: How many quantums required for all clocks to be equal?

  • Least common multiple is 6
  • To reach VT=6…
  • A is scheduled 6 times
  • B is scheduled 3 times
  • C is scheduled 2 times.
  • 6+3+2 = 11
  • A => 6/11 of CPU time
  • B => 3/11 of CPU time
  • C => 2/11 of CPU time
slide-4
SLIDE 4

CS 423: Operating Systems Design

A Note on CPU Affinity

4

We’ve had lots of great (abstraction-violating) questions about how multiprocessor scheduling works in practice…

  • To answer, consider CPU Affinity — scheduling a process

to stay on the same CPU as long as possible

  • Benefits?
  • Soft Affinity — Natural occurs through efficient

scheduling

  • Present in O(1) onward, absent in O(N)
  • Hard Affinity — Explicit request to scheduler made

through system calls (Linux 2.5+)

slide-5
SLIDE 5

CS 423: Operating Systems Design

Multi-Processor Scheduling

5

  • CPU affinity would seem to necessitate a multi-queue

approach to scheduling… but how?

  • Asymmetric Multiprocessing (AMP): One processor (e.g.,

CPU 0) handles all scheduling decisions and I/O processing, other processes execute only user code.

  • Symmetric Multiprocessing (SMP): Each processor is self-
  • scheduling. Could work with a single queue, but also

works with private queues.

  • Potential problems?
slide-6
SLIDE 6

CS 423: Operating Systems Design

SMP Load Balancing

6

  • SMP systems require load balancing to keep the workload

evenly distributed across all processors.

  • Two general approaches:
  • Push Migration: Task routinely checks the load on each

processor and redistributes tasks between processors if imbalance is detected.

  • Pull Migration: Idle processor can actively pull waiting

tasks from a busy processor.

slide-7
SLIDE 7

CS 423: Operating Systems Design

Other scheduling policies

7

■ What if you want to maximize throughput?

slide-8
SLIDE 8

CS 423: Operating Systems Design

Other scheduling policies

8

■ What if you want to maximize throughput?

■ Shortest job first!

slide-9
SLIDE 9

CS 423: Operating Systems Design

Other scheduling policies

9

■ What if you want to maximize throughput?

■ Shortest job first!

■ What if you want to meet all deadlines?

slide-10
SLIDE 10

CS 423: Operating Systems Design

Other scheduling policies

10

■ What if you want to maximize throughput?

■ Shortest job first!

■ What if you want to meet all deadlines?

■ Earliest deadline first! ■ Problem?

slide-11
SLIDE 11

CS 423: Operating Systems Design

Other scheduling policies

11

■ What if you want to maximize throughput?

■ Shortest job first!

■ What if you want to meet all deadlines?

■ Earliest deadline first! ■ Problem? ■ Works only if you are not “overloaded”. If the total

amount of work is more than capacity, a domino effect occurs as you always choose the task with the nearest deadline (that you have the least chance of finishing by the deadline), so you may miss a lot of deadlines!

slide-12
SLIDE 12

CS 423: Operating Systems Design

EDF Domino Effect

12

■ Problem:

■ It is Monday. You have a homework due tomorrow

(Tuesday), a homework due Wednesday, and a homework due Thursday

■ It takes on average 1.5 days to finish a homework.

■ Question: What is your best (scheduling) policy?

slide-13
SLIDE 13

CS 423: Operating Systems Design

EDF Domino Effect

13

■ Problem:

■ It is Monday. You have a homework due tomorrow

(Tuesday), a homework due Wednesday, and a homework due Thursday

■ It takes on average 1.5 days to finish a homework.

■ Question: What is your best (scheduling) policy?

■ You could instead skip tomorrow’s homework and work on

the next two, finishing them by their deadlines

■ Note that EDF is bad: It always forces you to work on the

next deadline, but you have only one day between deadlines which is not enough to finish a 1.5 day homework – you might not complete any of the three homeworks!

slide-14
SLIDE 14

CS 423: Operating Systems Design

Drive-By-Wire Example

14

■ Consider a control system in a autonomous vehicle

■ Steering wheel sampled every 10 ms – wheel positions

adjusted accordingly (computing the adjustment takes 4.5 ms of CPU time)

■ Breaks sampled every 4 ms – break pads adjusted

accordingly (computing the adjustment takes 2ms of CPU time)

■ Velocity is sampled every 15 ms – acceleration is adjusted

accordingly (computing the adjustment takes 0.45 ms)

■ For safe operation, adjustments must always be computed

before the next sample is taken

■ How to assign priorities?

slide-15
SLIDE 15

CS 423: Operating Systems Design 15

Find a schedule that makes sure all task invocations meet their deadlines

Steering wheel task (4.5 ms every 10 ms) Breaks task (2 ms every 4 ms) Velocity control task (0.45 ms every 15 ms)

Drive-By-Wire Example

slide-16
SLIDE 16

CS 423: Operating Systems Design 16

Sanity check: Is the processor over-utilized?

  • E.G.: If you have 5 homeworks due this time tomorrow, each takes 6

hours, then you are over utilized (5x6 = 30 > 24).

Steering wheel task (4.5 ms every 10 ms) Breaks task (2 ms every 4 ms) Velocity control task (0.45 ms every 15 ms)

Drive-By-Wire Example

slide-17
SLIDE 17

CS 423: Operating Systems Design 17

Sanity check: Is the processor over-utilized?

  • E.G.: If you have 5 homeworks due this time tomorrow, each takes 6

hours, then you are over utilized (5x6 = 30 > 24).

Steering wheel task (4.5 ms every 10 ms) Breaks task (2 ms every 4 ms) Velocity control task (0.45 ms every 15 ms)

Drive-By-Wire Example

45%

+

50%

+

03% 98%

slide-18
SLIDE 18

CS 423: Operating Systems Design 18

Task Scheduling

How do we assign task priorities? (SCHED_FIFO)

Steering wheel task (4.5 ms every 10 ms) Breaks task (2 ms every 4 ms) Velocity control task (0.45 ms every 15 ms)

slide-19
SLIDE 19

CS 423: Operating Systems Design 19

Task Scheduling

How do we assign task priorities? (SCHED_FIFO)

■ Rate Monotonic (large rate = higher priority)

Steering wheel task (4.5 ms every 10 ms) Breaks task (2 ms every 4 ms) Velocity control task (0.45 ms every 15 ms) Intuition: Urgent tasks should be higher in priority

slide-20
SLIDE 20

CS 423: Operating Systems Design 20

Task Scheduling

How do we assign task priorities? (SCHED_FIFO)

■ Rate Monotonic (large rate = higher priority)

Steering wheel task (4.5 ms every 10 ms) Breaks task (2 ms every 4 ms) Velocity control task (0.45 ms every 15 ms) Intuition: Urgent tasks should be higher in priority

Is there a problem here??

slide-21
SLIDE 21

CS 423: Operating Systems Design

■ Deadlines are missed! ■ Average Utilization < 100%

Breaks task (2 ms every 4 ms) Steering wheel task (4.5 ms every 10 ms) Velocity control task (0.45 ms every 15 ms)

Task Scheduling

slide-22
SLIDE 22

CS 423: Operating Systems Design

■ Deadlines are missed! ■ Average Utilization < 100%

Breaks task (2 ms every 4 ms) Steering wheel task (4.5 ms every 10 ms) Velocity control task (0.45 ms every 15 ms)

Task Scheduling

Fix: Give this task invocation a lower priority (EDF)

slide-23
SLIDE 23

CS 423: Operating Systems Design

Breaks task (2 ms every 4 ms) Steering wheel task (4.5 ms every 10 ms) Velocity control task (0.45 ms every 15 ms)

■ Deadlines are missed! ■ Average Utilization < 100%

Task Scheduling

Fix: Give this task invocation a lower priority (EDF)

slide-24
SLIDE 24

CS 423: Operating Systems Design

Breaks task (2 ms every 4 ms) Steering wheel task (4.5 ms every 10 ms) Velocity control task (0.45 ms every 15 ms)

Task Scheduling

Static versus Dynamic priorities?

Static: Instances of the same task have the same priority

Dynamic: Instances of same task may have different priorities

Intuition: Dynamic priorities offer the designer more flexibility and hence are more capable to meet deadlines

slide-25
SLIDE 25

CS 423: Operating Systems Design

Task Scheduling

25

Re: Real Time Scheduling of Periodic Tasks…

■ Result #1: Earliest Deadline First (EDF) is the optimal

dynamic priority scheduling policy for independent periodic tasks (meets the most deadlines of all dynamic priority scheduling policies)

■ Result #2: Rate Monotonic Scheduling (RM) is the

  • ptimal static priority scheduling policy for

independent periodic tasks (meets the most deadlines

  • f all static priority scheduling policies)
slide-26
SLIDE 26

CS 423: Operating Systems Design

Task Scheduling

26

Re: Real Time Scheduling of Periodic Tasks…

■ Result #1: Earliest Deadline First (EDF) is the optimal

dynamic priority scheduling policy for independent periodic tasks (meets the most deadlines of all dynamic priority scheduling policies)

■ Result #2: Rate Monotonic Scheduling (RM) is the

  • ptimal static priority scheduling policy for

independent periodic tasks (meets the most deadlines

  • f all static priority scheduling policies)
slide-27
SLIDE 27

CS 423: Operating Systems Design

Locking vs. Priority

27

■ What if a higher-priority process needs a

resource locked by a lower-priority process?

■ How long will the higher priority process have to

wait for lower-priority execution?

slide-28
SLIDE 28

CS 423: Operating Systems Design

Locks and priorities may be at odds. Locking results in priority inversion

High-priority task Low-priority task Lock S Preempt.

Priority Inversion

slide-29
SLIDE 29

CS 423: Operating Systems Design

Locks and priorities may be at odds. Locking results in priority inversion

Priority Inversion

Attempt to lock S results in blocking High-priority task Low-priority task Lock S Preempt. Priority Inversion

slide-30
SLIDE 30

CS 423: Operating Systems Design

How should we account for priority inversion?

Priority Inversion

Attempt to lock S results in blocking

30

High-priority task Low-priority task Lock S Preempt. Unlock S Lock S Unlock S Priority Inversion

slide-31
SLIDE 31

CS 423: Operating Systems Design

Consider the case below: a series of intermediate priority tasks is delaying a higher-priority one

Unbounded Priority Inversion

Attempt to lock S results in blocking

31

High-priority task Low-priority task Lock S Preempt. Intermediate-priority tasks Preempt.

Unbounded Priority Inversion

slide-32
SLIDE 32

CS 423: Operating Systems Design

Consider the case below: a series of intermediate priority tasks is delaying a higher-priority one

Unbounded Priority Inversion

Attempt to lock S results in blocking

32

High-priority task Low-priority task Lock S Preempt. Intermediate-priority tasks Preempt.

Unbounded Priority Inversion

How can we prevent unbounded priority inversion?

slide-33
SLIDE 33

CS 423: Operating Systems Design

Solution: Let a task inherit the priority of any higher-priority task it is blocking

Priority Inheritance Protocol

Attempt to lock S results in blocking

33

High-priority task Low-priority task Lock S Preempt. Intermediate-priority tasks

Lock S Unlock S Unlock S

slide-34
SLIDE 34

CS 423: Operating Systems Design

Maximum Blocking Time

Priority Inheritance Protocol:

Need Red Need Blue Need Yellow

34

slide-35
SLIDE 35

CS 423: Operating Systems Design

■ What is the longest period of time a high priority

task will wait on a resource?

Semaphore Queue Resource 1 Semaphore Queue Resource 2 Semaphore Queue Resource M

Two priority inversion scenarios to consider:

(a) Lower priority task holds a resource I need (direct blocking) (b) Lower priority task inherits a higher priority than me because it holds a resource the higher- priority task needs (push-through blocking)

35

Max Priority Inversion Time

slide-36
SLIDE 36

CS 423: Operating Systems Design

Priority Ceiling Protocol

36

■ Definition: The priority ceiling of a semaphore is the

highest priority of any task that can lock it

■ A task that requests a lock Rk is denied if its priority

is not higher than the highest priority ceiling of all semaphores currently locked by other tasks (say it belongs to semaphore Rh)

■ The task is said to be blocked by the task holding lock Rh

■ A task inherits the priority of the top higher-priority

task it is blocking

slide-37
SLIDE 37

CS 423: Operating Systems Design

Need Yellow but Priority is lower Than Red ceiling Need Blue but Priority is lower Than Red ceiling Needs Red, waits for 1 critical section to complete. Done

37

Priority Ceiling Protocol:

Maximum Blocking Time

slide-38
SLIDE 38

CS 423: Operating Systems Design

Maximum Blocking Time

Compare to the Priority Inheritance Protocol!!

Need Red Need Blue Need Yellow

38