CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11...

49
CIS 4930/6930: Principles of Cyber-Physical Systems Chapter 11 Scheduling Hao Zheng Department of Computer Science and Engineering University of South Florida H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 1 / 44

Transcript of CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11...

Page 1: CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11 Schedulinghaozheng/teach/PrinciplesCPS/slides/cha… · CIS 4930/6930: Principles of Cyber-Physical Systems Chapter

CIS 4930/6930: Principles ofCyber-Physical Systems

Chapter 11 Scheduling

Hao Zheng

Department of Computer Science and EngineeringUniversity of South Florida

H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 1 / 44

Page 2: CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11 Schedulinghaozheng/teach/PrinciplesCPS/slides/cha… · CIS 4930/6930: Principles of Cyber-Physical Systems Chapter

Functions of an Operating System (orMicrokernel)

• Memory management

• File system

• Networking

• Security

• Input and output (interrupt handling)

• Synchronization (semaphores and locks)

• Scheduling of threads or processes

H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 2 / 44

Page 3: CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11 Schedulinghaozheng/teach/PrinciplesCPS/slides/cha… · CIS 4930/6930: Principles of Cyber-Physical Systems Chapter

Functions of an Operating System (orMicrokernel)

• Memory management

• File system

• Networking

• Security

• Input and output (interrupt handling)

• Synchronization (semaphores and locks)

• Scheduling of threads or processes• Creation and termination of threads• Timing of thread activations

H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 2 / 44

Page 4: CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11 Schedulinghaozheng/teach/PrinciplesCPS/slides/cha… · CIS 4930/6930: Principles of Cyber-Physical Systems Chapter

Real-time Systems

• A real-time system includes timing constraints such as:• Physical-time deadlines by which a task must be completed.• Requirements that a task must occur no earlier than a particular

time.• A task must occur a set time after another task.• A task must occur periodically at some specified period.

• Tasks may be dependent on one another or simply share aprocessor.

• All of these cases require a careful scheduling strategy. k6

H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 3 / 44

Page 5: CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11 Schedulinghaozheng/teach/PrinciplesCPS/slides/cha… · CIS 4930/6930: Principles of Cyber-Physical Systems Chapter

11.1 Basics of Scheduling:11.1.1 Scheduling Decisions

• A scheduling decision has three parts:• Assignment: which processor should execute the task.• Ordering: in what order each processor should execute its tasks.• Timing: the time at which each task executes.

• Decisions may be at design time or run time.• A fully-static scheduler makes all decisions at design time (no

locks).• A static order scheduler defers timing to run time (may block

on lock).• A static assignment scheduler defers ordering and timing to

run time.• A fully-dynamic scheduler makes all decisions at run time.

H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 4 / 44

Page 6: CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11 Schedulinghaozheng/teach/PrinciplesCPS/slides/cha… · CIS 4930/6930: Principles of Cyber-Physical Systems Chapter

11.1 Basics of Scheduling:11.1.1 Scheduling Decisions

• A non-preemptive scheduler dispatches when current threadcompletes.

• A preemptive scheduler may make a scheduling decisionduring execution of a task• Upon a timer interrupt at a jiffy interval.• Upon an I/O interrupt.• When it attempts to acquire an unavailable lock, and resumed

when another task releases the lock.• When it releases a lock, if a higher priority thread requires the

lock.• When the current thread makes any OS call.

• File system access• Network access• ...

H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 5 / 44

Page 7: CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11 Schedulinghaozheng/teach/PrinciplesCPS/slides/cha… · CIS 4930/6930: Principles of Cyber-Physical Systems Chapter

11.1.2 Task Models

• The set of assumptions is called the task model of thescheduler.

• Assume a finite number of tasks that may or may not terminate.• Real-time systems often assume that tasks terminate.

• Some schedulers can assume that• All tasks are known before scheduling begins, or• New tasks can arrive dynamically.

• Some schedulers support scenarios where each task τ ∈ Texecutes repeatedly, possibly forever, and possibly periodically.

H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 6 / 44

Page 8: CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11 Schedulinghaozheng/teach/PrinciplesCPS/slides/cha… · CIS 4930/6930: Principles of Cyber-Physical Systems Chapter

11.1.2 Task Models

• Distinction between a task and its executions:• When task τ ∈ T executes repeatedly, the task executions areτ1, τ2, . . ..

• A sporadic task repeats with irregular timing, but has a lowerbound on the time between executions.

• If execution i must precede j , we write i < j (precedenceconstraint).

• A task may require preconditions to be satisfied before it isenabled.• Availability of a lock may be a precondition for resumption of a

task.

H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 7 / 44

Page 9: CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11 Schedulinghaozheng/teach/PrinciplesCPS/slides/cha… · CIS 4930/6930: Principles of Cyber-Physical Systems Chapter

Task Execution Times

oi

ei

ri si fi di

i

H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 8 / 44

Page 10: CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11 Schedulinghaozheng/teach/PrinciplesCPS/slides/cha… · CIS 4930/6930: Principles of Cyber-Physical Systems Chapter

Deadlines and Priority

• Hard real-time scheduling has hard deadlines which are realphysical constraints that are an error when missed.

• Soft real-time scheduling has desired deadlines which are noterrors when missed.

• A priority-based scheduler assumes each task is assigned anumber (priority) and chooses the enabled task with the highestpriority.

• A fixed priority remains constant while a dynamic priority canchange.

• A non-preemptive priority-based scheduler only uses thepriorities to choose the next task, but never interrupts a taskthat is executing.

• A preemptive priority-based scheduler can change to ahigher priority task at any time.

H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 9 / 44

Page 11: CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11 Schedulinghaozheng/teach/PrinciplesCPS/slides/cha… · CIS 4930/6930: Principles of Cyber-Physical Systems Chapter

11.1.3 Comparing Schedulers

• A schedule is feasible if it meets all deadlines (fi ≤ di).• A scheduler that produces feasible schedules whenever possible

is optimal with respect to feasibility.

• Schedulers also judged based on utilization (the percentage oftime the processor is executing tasks).• Optimal w.r.t feasibility schedulers deliver feasible schedules

whenever the utilization is ≤ 100%.

• Maximum lateness is another criterion:

Lmax = maxi∈T

(fi − di)

• Total completion time is also important:

M = maxi∈T

fi −mini∈T

ri

H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 10 / 44

Page 12: CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11 Schedulinghaozheng/teach/PrinciplesCPS/slides/cha… · CIS 4930/6930: Principles of Cyber-Physical Systems Chapter

11.1.4 Implementation of a Scheduler

• Thread data structure:• Copy of all state (machine registers).• Address at which to resume executing the thread.• Status of the thread (e.g. blocked on mutex).• Priority, worst case execution time, and other info to assist the

scheduler.

• Operating System:• Set up periodic timer interrupts.• Create default thread data structures.• Dispatch a thread.

• Timer interrupt service routine:• Setup next timer interrupt.• Dispatch a thread.

H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 11 / 44

Page 13: CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11 Schedulinghaozheng/teach/PrinciplesCPS/slides/cha… · CIS 4930/6930: Principles of Cyber-Physical Systems Chapter

Dispatching a Thread

1 Disable interrupts.

2 Save state (registers) including the return address on the stack.

3 Save the stack pointer into the current thread data structure.

4 Determine which thread should execute (scheduling).

5 If the same one, enable interrupts and return.

6 Restore the stack pointer for the new thread.

7 Copy thread state into machine registers.

8 Replace program counter on the stack for the new thread.

9 Enable interrupts.

10 Return.

H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 12 / 44

Page 14: CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11 Schedulinghaozheng/teach/PrinciplesCPS/slides/cha… · CIS 4930/6930: Principles of Cyber-Physical Systems Chapter

11.2 Rate Monotonic (RM) Scheduling

• Assume n tasks (i.e., T = {τ1, τ2, . . . τn}) invoked periodicallywhere each task must complete in each period, pi .

• Rate monotonic schedule gives higher priority to a task withsmaller period, and it is optimal with respect to feasibility.

• Note: important assumption is that context switch time isnegligible.

Liu and Leland, “Scheduling algorithms for multiprogramming ina hard-real-time environment,” J. ACM, 20(1), 1973.

H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 13 / 44

Page 15: CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11 Schedulinghaozheng/teach/PrinciplesCPS/slides/cha… · CIS 4930/6930: Principles of Cyber-Physical Systems Chapter

Example: Two Periodic Tasks

e2

p2

e1

p1

τ1,1 τ1,2

τ2,2τ2,1

τ1,7τ1,6τ1,5τ1,4τ1,3τ1

τ2

H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 14 / 44

Page 16: CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11 Schedulinghaozheng/teach/PrinciplesCPS/slides/cha… · CIS 4930/6930: Principles of Cyber-Physical Systems Chapter

Example: Two Periodic Tasks

e2

p2

e1

p1

τ1,1 τ1,2

τ2,2τ2,1

τ1,7τ1,6τ1,5τ1,4τ1,3τ1

τ2

Is a non-preemptive schedule feasible?

H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 14 / 44

Page 17: CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11 Schedulinghaozheng/teach/PrinciplesCPS/slides/cha… · CIS 4930/6930: Principles of Cyber-Physical Systems Chapter

Example: Two Periodic Tasks

e2

p2

e1

p1

τ1,1 τ1,2

τ2,2τ2,1

τ1,7τ1,6τ1,5τ1,4τ1,3τ1

τ2

Is a non-preemptive schedule feasible? No!

H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 14 / 44

Page 18: CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11 Schedulinghaozheng/teach/PrinciplesCPS/slides/cha… · CIS 4930/6930: Principles of Cyber-Physical Systems Chapter

Example: Two Periodic Tasks

e2

p2

e1

p1

τ1,1 τ1,2

τ2,2τ2,1

τ1,7τ1,6τ1,5τ1,4τ1,3τ1

τ2

How about a preemptive schedule with higher priority for red task?

H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 14 / 44

Page 19: CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11 Schedulinghaozheng/teach/PrinciplesCPS/slides/cha… · CIS 4930/6930: Principles of Cyber-Physical Systems Chapter

Example: Two Periodic Tasks

e2

p2

p1

+

τ1

τ2

How about a preemptive schedule with higher priority for red task?Yes!

H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 14 / 44

Page 20: CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11 Schedulinghaozheng/teach/PrinciplesCPS/slides/cha… · CIS 4930/6930: Principles of Cyber-Physical Systems Chapter

Worst Case Response Time

τ1

τ2

τ1

τ2

τ1

τ2

τ1

τ2

o2

H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 15 / 44

Page 21: CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11 Schedulinghaozheng/teach/PrinciplesCPS/slides/cha… · CIS 4930/6930: Principles of Cyber-Physical Systems Chapter

Non-RM Schedule Feasible

e2

p2

e1

p1

τ1

τ2

Condition for feasibility: e1 + e2 ≤ p1

H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 16 / 44

Page 22: CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11 Schedulinghaozheng/teach/PrinciplesCPS/slides/cha… · CIS 4930/6930: Principles of Cyber-Physical Systems Chapter

RM Schedule Also Feasible

e2

p2

e1

p1

τ1

τ2

e1 + e2 ≤ p1 −→ feasible schedule

H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 17 / 44

Page 23: CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11 Schedulinghaozheng/teach/PrinciplesCPS/slides/cha… · CIS 4930/6930: Principles of Cyber-Physical Systems Chapter

Comments

• Proof can be extended to an arbitrary number of tasks.

• Proof only gives optimality w.r.t. feasibility, not other optimalitycriteria.

• Practical implementation:• Timer interrupt at greatest common divisor of the periods.• Multiple timers.

• Note RM schedulers do not always achieve 100 percentutilization.

H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 18 / 44

Page 24: CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11 Schedulinghaozheng/teach/PrinciplesCPS/slides/cha… · CIS 4930/6930: Principles of Cyber-Physical Systems Chapter

11.3 Earliest Deadline First

Jackson’s earliest due date (EDD) Algorithm (1955)

• Given n independent one-time tasks with deadlines d1, . . . , dn,schedule them to minimize the maximum lateness, defined as

Lmax = max1≤i≤n

{fi − di}

where fi is the finishing time of task i . Note that this is negativeiff all deadlines are met.

• Earliest Due Date (EDD) algorithm: Execute them in order ofnon-decreasing deadlines.• Sort tasks such that d1 ≤ d2 ≤ . . . ≤ dn.• Then, executes τ1, τ2, . . . , τn.

H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 19 / 44

Page 25: CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11 Schedulinghaozheng/teach/PrinciplesCPS/slides/cha… · CIS 4930/6930: Principles of Cyber-Physical Systems Chapter

11.3 Earliest Deadline First: EDD

• Note that this does not require preemption.

• Minimizes the maximum lateness as compared to all otherpossible orderings.

• Does not support arrival of tasks, thus no periodic or repeatingtasks.

H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 20 / 44

Page 26: CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11 Schedulinghaozheng/teach/PrinciplesCPS/slides/cha… · CIS 4930/6930: Principles of Cyber-Physical Systems Chapter

11.3 Earliest Deadline First

• Horn’s earliest deadline first (EDF) Algorithm (1974) extendsEDD to allow tasks to arrive at any time.

Given a set of n independent tasks with arbitrary ar-rival times, any algorithm that at any instant executesthe task with the earliest absolute deadline among allarrived tasks is optimal w.r.t. minimizing the maxi-mum lateness.

• EDF requires the scheduler to always execute the task with theearliest deadline among all arrived tasks.

• EDF has a dynamic priority making it more difficult toimplement.• A repeated task may be assigned with different priority at

different time.

• EDF can be applied to periodic tasks as well as aperiodic tasksby making deadline be the end of the period.

H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 21 / 44

Page 27: CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11 Schedulinghaozheng/teach/PrinciplesCPS/slides/cha… · CIS 4930/6930: Principles of Cyber-Physical Systems Chapter

Comparison of EDF and RMS

• Favoring RMS:• Scheduling decisions are simpler (fixed vs. dynamic priorities

required by EDF).• EDF scheduler must maintain a list of ready tasks that is sorted

by priority.

• Favoring EDF:• Since EDF is optimal w.r.t. maximum lateness, it is also optimal

w.r.t. feasibility while RMS is only optimal w.r.t. feasibility.• EDF can achieve full utilization where RMS fails to do that.• EDF results in fewer preemptions in practice, and hence less

overhead for context switching.• Deadlines can be different from the period.

H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 22 / 44

Page 28: CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11 Schedulinghaozheng/teach/PrinciplesCPS/slides/cha… · CIS 4930/6930: Principles of Cyber-Physical Systems Chapter

11.3.1 EDF with Precedences

0

1

d1= 2

d2= 5

d3= 4d6= 6

d5= 5

d4= 3

642

3 2 4 5 6EDF

1 2 4 3 5 6LDF

1 2 4 3 5 6EDF*

H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 23 / 44

Page 29: CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11 Schedulinghaozheng/teach/PrinciplesCPS/slides/cha… · CIS 4930/6930: Principles of Cyber-Physical Systems Chapter

Latest Deadline First (Lawler, 1973)

• Latest deadline first (LDF) builds a schedule backwards.

• Given a precedence graph, starting from the leaf nodes,choose a node with no other dependent nodes and with thelatest deadline to be scheduled last, and work backwards.

• LDF is optimal in the sense that it minimizes the maximumlateness.

• It does not require preemption while EDF does.

• However, it requires that all tasks be available and theirprecedences known before any task is executed.• Does not support arrival of tasks.

H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 24 / 44

Page 30: CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11 Schedulinghaozheng/teach/PrinciplesCPS/slides/cha… · CIS 4930/6930: Principles of Cyber-Physical Systems Chapter

LDF with Precedences

0

1

d1= 2

d2= 5

d3= 4d6= 6

d5= 5

d4= 3

642

3 2 4 5 6EDF

1 2 4 3 5 6LDF

1 2 4 3 5 6EDF*

H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 25 / 44

Page 31: CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11 Schedulinghaozheng/teach/PrinciplesCPS/slides/cha… · CIS 4930/6930: Principles of Cyber-Physical Systems Chapter

EDF with Precedences (Chetto et al.,1990)

• With a preemptive scheduler, EDF can be modified to accountfor precedences and to allow tasks to arrive at arbitrary times.

• It adjusts the deadlines and arrival times according to theprecedences.

• For a task τi ∈ T , modify its deadline as follows:

d ′i = min(di , minj∈D(i)

(d ′j − ej))

where D(i) ⊂ T are the tasks that immediately depend on i inthe precedence graph.

• EDF with precedences (EDF*) is optimal in the sense ofminimizing the maximum lateness.

H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 26 / 44

Page 32: CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11 Schedulinghaozheng/teach/PrinciplesCPS/slides/cha… · CIS 4930/6930: Principles of Cyber-Physical Systems Chapter

EDF with Precedences

0

1

d1= 2

d2= 5

d3= 4d6= 6

d5= 5

d4= 3

642

3 2 4 5 6EDF

1 2 4 3 5 6LDF

1 2 4 3 5 6EDF*

H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 27 / 44

Page 33: CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11 Schedulinghaozheng/teach/PrinciplesCPS/slides/cha… · CIS 4930/6930: Principles of Cyber-Physical Systems Chapter

11.4 Scheduling and Mutual Exclusion

• Fixed priority scheduler always executes the enabled tasks withhighest priority.

• When threads access shared resources, they need to use mutexesto ensure data integrity.

• Mutexes can also complicate scheduling.

H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 28 / 44

Page 34: CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11 Schedulinghaozheng/teach/PrinciplesCPS/slides/cha… · CIS 4930/6930: Principles of Cyber-Physical Systems Chapter

Mars Pathfinder

H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 29 / 44

Page 35: CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11 Schedulinghaozheng/teach/PrinciplesCPS/slides/cha… · CIS 4930/6930: Principles of Cyber-Physical Systems Chapter

Priority Inversion: A Hazard with Mutexes

0 2 4 6 8 10

task 1

task 2

task 3

acq

uir

e lo

ck

pre

emp

t

blo

ck

pre

emp

t

rele

ase

do

ne

task 1 blocked

Task 1 has highest priority, task 3 lowest. Task 3 acquires a lock on ashared object, entering a critical section. It gets preempted by task 1,which then tries to acquire the lock and blocks. Task 2 preempts task3 at time 4, keeping the higher priority task 1 blocked for anunbounded amount of time. In effect, the priorities of tasks 1 and 2get inverted, since task 2 can keep task 1 waiting arbitrarily long.

H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 30 / 44

Page 36: CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11 Schedulinghaozheng/teach/PrinciplesCPS/slides/cha… · CIS 4930/6930: Principles of Cyber-Physical Systems Chapter

11.4.2 Priority Inheritance Protocol (PIP)

• When a task blocks attempting to acquire a lock, the taskholding the lock inherits the priority of the blocked task.

0 2 4 6 8 10

task 1

task 2

task 3

acq

uir

e lo

ck

pre

emp

t

blo

ck

rele

ase

do

ne

task 1 blocked

at priority of 1

do

ne

task 2 preempted

H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 31 / 44

Page 37: CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11 Schedulinghaozheng/teach/PrinciplesCPS/slides/cha… · CIS 4930/6930: Principles of Cyber-Physical Systems Chapter

11.4.3 Priority Ceiling Protocol

Priorities can be used to remove certain kinds of deadlocks.

0 2 4 6

task 1

task 2

acq

uir

e lo

ck a

pre

emp

t

block on a

acquire lock b

a

b

block on ba

H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 32 / 44

Page 38: CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11 Schedulinghaozheng/teach/PrinciplesCPS/slides/cha… · CIS 4930/6930: Principles of Cyber-Physical Systems Chapter

11.4.3 Priority Ceiling Protocol (PCP)

• Every lock or semaphore is assigned a priority ceiling equal to thepriority of the highest-priority task that can lock it.• Can one automatically compute the priority ceiling?

• A task τ can acquire a lock only if the task’s priority is strictlyhigher than the priority ceilings of all locks currently held byother tasks.• Intuition: task τ will not later try to acquire these locks held by

other tasks.• Locks that are not held by any task don’t affect the task.

• This prevents deadlocks.

• There are extensions supporting dynamic priorities and dynamiccreations of locks.

H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 33 / 44

Page 39: CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11 Schedulinghaozheng/teach/PrinciplesCPS/slides/cha… · CIS 4930/6930: Principles of Cyber-Physical Systems Chapter

11.4.3 Priority Ceiling Protocol (PCP)

0 2 4 6

task 1

task 2

lock

a

pre

emp

t

prevented from locking bby priority ceiling protocol

a

b

a b

unlock b, then a

a

H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 34 / 44

Page 40: CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11 Schedulinghaozheng/teach/PrinciplesCPS/slides/cha… · CIS 4930/6930: Principles of Cyber-Physical Systems Chapter

11.5 Multiprocessor Scheduling

• Scheduling a fixed finite set of tasks with precedence on a finitenumber of processors with goal to minimize execution time isNP-hard.

• Hu level scheduling algorithm assigns priority to task τ basedon the level.

• Level is the greatest sum of execution times of tasks on a pathin the precedence graph from τ to another task with nodependents.

• It is a critical path method that is not optimal, butapproximates an optimal solution for most graphs.

• List scheduler assigns tasks to processors based on priorities.

H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 35 / 44

Page 41: CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11 Schedulinghaozheng/teach/PrinciplesCPS/slides/cha… · CIS 4930/6930: Principles of Cyber-Physical Systems Chapter

Precedence Example Revisited

0

1

d1= 2

d2= 5

d3= 4d6= 6

d5= 5

d4= 3

642

3 2 4 5 6EDF

1 2 4 3 5 6LDF

1 2 4 3 5 6EDF*

H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 36 / 44

Page 42: CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11 Schedulinghaozheng/teach/PrinciplesCPS/slides/cha… · CIS 4930/6930: Principles of Cyber-Physical Systems Chapter

A Two Processor Parallel Schedule

0

1

42

3 5 6Processor A:

2 4Processor B:

H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 37 / 44

Page 43: CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11 Schedulinghaozheng/teach/PrinciplesCPS/slides/cha… · CIS 4930/6930: Principles of Cyber-Physical Systems Chapter

11.5.1 Scheduling Anomalies andBrittleness

• All thread scheduling algorithms are brittle (i.e., small changescan have big, unexpected consequences).

• Let us consider a schedule for a multiprocessor (or multicore).

• Theorem (Richard Graham, 1976):

If a task set with fixed priorities, execution times, andprecedence constraints is scheduled according to prior-ities on a fixed number of processors, then increasingthe number of processors, reducing execution times,or weakening precedence constraints can increase theschedule length.

H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 38 / 44

Page 44: CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11 Schedulinghaozheng/teach/PrinciplesCPS/slides/cha… · CIS 4930/6930: Principles of Cyber-Physical Systems Chapter

Richard’s Anomalies

0 4 8 12 16

proc1

proc2

proc3

2 6 10 14

3

2

1

4

9

5 7

86

time

e1 = 3

e2 = 2

e3 = 2

e4 = 2

e9 = 9

e8 = 4

e7 = 4

e6 = 4

e5 = 4

H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 39 / 44

Page 45: CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11 Schedulinghaozheng/teach/PrinciplesCPS/slides/cha… · CIS 4930/6930: Principles of Cyber-Physical Systems Chapter

Richard’s Anomalies: Smaller Computation Time

0 4 8 12 16

proc1

proc2

proc3

2 6 10 14

3

2

1

4

9

5 7

86

time

e1 = 3

e2 = 2

e3 = 2

e4 = 2

e9 = 9

e8 = 4

e7 = 4

e6 = 4

e5 = 4

0 4 8 12 16

proc1

proc2

proc3

2 6 10 14

3

2

1

4 9

5

7

8

6

time

H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 40 / 44

Page 46: CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11 Schedulinghaozheng/teach/PrinciplesCPS/slides/cha… · CIS 4930/6930: Principles of Cyber-Physical Systems Chapter

Richard’s Anomalies: More Processors

0 4 8 12 16

proc1

proc2

proc3

2 6 10 14

3

2

1

4

9

5 7

86

time

e1 = 3

e2 = 2

e3 = 2

e4 = 2

e9 = 9

e8 = 4

e7 = 4

e6 = 4

e5 = 4

0 4 8 12 16

proc1

proc2

proc3

2 6 10 14

3

2

1

4

95

7

8

6

time

proc4

H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 41 / 44

Page 47: CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11 Schedulinghaozheng/teach/PrinciplesCPS/slides/cha… · CIS 4930/6930: Principles of Cyber-Physical Systems Chapter

Richard’s Anomalies: Less Precedences

Suppose precedences between task 4 and tasks 7 and 8 are removed.

0 4 8 12 16

proc1

proc2

proc3

2 6 10 14

3

2

1

4

9

5 7

86

time

e1 = 3

e2 = 2

e3 = 2

e4 = 2

e9 = 9

e8 = 4

e7 = 4

e6 = 4

e5 = 4

0 4 8 12 16

proc1

proc2

proc3

2 6 10 14

3

2

1

4

9

5

7

8

6

time

Remove precedences between task 4 and tasks 7 and 8.H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 42 / 44

Page 48: CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11 Schedulinghaozheng/teach/PrinciplesCPS/slides/cha… · CIS 4930/6930: Principles of Cyber-Physical Systems Chapter

Richard’s Anomalies with Mutexes

0 4 8 12

proc1

proc2

2 6 10

3

1

4 5

time

2

proc1

proc2 3

1

4 5

2

0 4 8 122 6 10time

H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 43 / 44

Page 49: CIS 4930/6930: Principles of Cyber-Physical Systems - Chapter 11 Schedulinghaozheng/teach/PrinciplesCPS/slides/cha… · CIS 4930/6930: Principles of Cyber-Physical Systems Chapter

Concluding Remarks

• Scheduling is inherently difficult• SW execution time hardly to predict accurately.• The actual dynamic behavior can be quite different.

• Timing behavior under all known task scheduling strategies isbrittle.

• Small changes can have big (and unexpected) consequences.

• Unfortunately, since execution times are so hard to predict, suchbrittleness can result in unexpected system failures.

H. Zheng (CSE USF) CIS 4930/6930: Principles of CPS 44 / 44