52
Summer Semester 2012 COMPLEX EMBEDDED SYSTEMS Real-Time Scheduling System and Software Engineering Prof. Dr.-Ing. Armin Zimmermann

Real-Time Scheduling - Startseite TU · PDF fileReal-Time Scheduling ... Example on following slide. ... SSE Zimmermann Complex Embedded Systems Real-Time Scheduling – 20 Round Robin

Embed Size (px)

Citation preview

Summer Semester 2012

COMPLEX EMBEDDED SYSTEMS

Real-Time Scheduling

System and Software EngineeringProf. Dr.-Ing. Armin Zimmermann

Real-Time Scheduling – 2Complex Embedded SystemsSSE Zimmermann

Contents

Introduction

Scheduling in Interactive Systems

Real-Time Scheduling Priorities Earliest Deadline First Least Laxity First Periodic Processes

Real-Time Scheduling – 3Complex Embedded SystemsSSE Zimmermann

Introduction

Concurrent Processes need Resources Processor, memory, device, file, … Interdependencies of otherwise independent processes Resources must be managed by the operating system If a process cannot reserve all required resource, it has to

wait (process state: blocked) Scheduler decides which process runs

creation

preemption

dispatch

resourcerequirement

ready queue processor terminated

sema-phore

queues

ready running

blocked

resource allocation

Real-Time Scheduling – 4Complex Embedded SystemsSSE Zimmermann

Introduction

Goals of Scheduling Optimal performance of the system, throughput How? ordering of ready processes Scheduling algorithms try to achieve the best possible

results (on average or for a given process set)

Optimality Criteria Turnaround time, waiting time, response time of

processes Processor utilization Throughput (finished processes per time unit) Meet deadlines: feasibility, schedulability

Real-Time Scheduling – 5Complex Embedded SystemsSSE Zimmermann

Introduction

Classification of Scheduling Algorithms I Static All parameters are known a-priori Scheduling can thus be finished at design time,

thus highly efficient during run time Applicable for closed embedded systems

Dynamic Not all parameters are known before on-line scheduling necessary

Optimality goal can often be achieved only approximately Scheduling requires computational effort Normal OS operation for interactive systems

Real-Time Scheduling – 6Complex Embedded SystemsSSE Zimmermann

Introduction

Classification of Scheduling Algorithms II

Non-preemptive scheduling The operating system does not take away the processor from

the running process, it runs until it ends or gives away the processor

Advantage: simple implementation of process change Disadvantage: not useful for real-time systems, where rapid

reactions to external events are necessary

Preemptive scheduling The operating system interrupts the running process

whenever there is a need to reschedule

Real-Time Scheduling – 7Complex Embedded SystemsSSE Zimmermann

Introduction

Computational Effort of Scheduling Analysis of stochastic and deterministic models of

resources and processes are computationally expensive Example n processes with individual interdependencies, run times, and

ready times Goal: a sequence of process executions that minimizes mean

waiting time Algorithm: in the worst case, all n! possible sequences have

to be constructed and analyzed Exponential complexity, not useful for non-trivial task sets

General Problem of Scheduling is NP hard Thus only simplified models are used (independency)

Real-Time Scheduling – 8Complex Embedded SystemsSSE Zimmermann

Introduction

Simplification of Analysis Independent processes (i.e., no successor/predecessor

relationships) Only the CPU needs to be taken into account

(“processor-bound system”)

(There are transformation rules that can account for dependencies by adjusting ready times and deadlines)

creation

preemption

dispatchready queue processor terminated

ready running

Real-Time Scheduling – 9Complex Embedded SystemsSSE Zimmermann

Introduction

Schedulability and Optimal Algorithms Schedulability is only interesting for real-time systems “timely” if all deadlines are met; hard real-time system model

A timely schedule for a non-real-time task set can always be found, because there are no deadlines

Static scheduling algorithms are called optimalwhen they find a timely plan (if there is one)

Dynamic scheduling algorithms are called optimalwhen thy find a timely plan, if a static algorithm would have been able to find one

Real-Time Scheduling – 10Complex Embedded SystemsSSE Zimmermann

Introduction

Scheduling in Interactive Systems

Real-Time Scheduling Priorities Earliest Deadline First Least Laxity First Periodic Processes

Real-Time Scheduling – 11Complex Embedded SystemsSSE Zimmermann

First Come First Served

Scheduling Based on Arrival Time Simplest algorithm: schedule the processes in the order in

which they arrive in the ready queue (non-preemptive) First-Come-First-Served (FCFS) or FIFO algorithm Assumed to be “fair” among humans What about computer processes?

Mean waiting time: FCFS is not optimal! How to show this? Example Performance of analyzed scheduling algorithm

is worse than another hand-picked schedule

Example on following slide

Real-Time Scheduling – 12Complex Embedded SystemsSSE Zimmermann

First Come First Served

Three processes P1, P2, P3 with computation times C1 = 2, C2 = 3 and C3 = 1 arrive in the sequence P2, P1, P3

P1

P3

P2

1 2 3 4 5 6time

Real-Time Scheduling – 13Complex Embedded SystemsSSE Zimmermann

First Come First Served

FCFS computes the following schedule (Gantt chart):

Process waiting times W2 = 0, W1 = 3 and W3 = 5 Mean waiting time is thus (0 + 3 + 5) / 3 = 8/3 Reason: sometimes, short processes have to wait for

longer ones Conclusion: take run time into account

P1 P3P2

1 2 3 4 5 6time

Real-Time Scheduling – 14Complex Embedded SystemsSSE Zimmermann

Shortest Job First

Scheduling Based on Computation Time Sort processes in the ready queue depending on their run

time, shortest first Shortest Job First Algorithm (SJF)

SJF is optimal for mean waiting time

Real-Time Scheduling – 15Complex Embedded SystemsSSE Zimmermann

Shortest Job First

Using the previous example with ready times = 0

SJF results in the following schedule

Mean waiting time is reduced to 4/3 (half!)

P1

P3

P2

1 2 3 4 5 6time

P1P3 P2

1 2 3 4 5 6time

Real-Time Scheduling – 16Complex Embedded SystemsSSE Zimmermann

Shortest Job First

Proof of Optimality of SJF for Mean Waiting Time Move a shorter process a in front of a longer one c

- increases waiting time of c- reduces waiting time for a

Above: W(c) = x, W(a) = x + c + b Below: W(c) = x + a + b, W(a) = x c > a, thus mean waiting time is reduced

x abctime

x a b ctime

Real-Time Scheduling – 17Complex Embedded SystemsSSE Zimmermann

Shortest Job First

Shortest Job First with Preemption Is the computation time of a new process smaller than the

remaining time of the running, the latter is preempted and the new process becomes running

The previously running process is put into the ready queue depending on its remaining run time

Example:P1

P3

P2

1 2 3 4 5 6time

P4

Real-Time Scheduling – 18Complex Embedded SystemsSSE Zimmermann

Shortest Job First

Mean waiting time with preemption: (2 + 4 + 0 + 0)/4 = 1.5

Non-preemptive: (1 + 4 + 0 + 1.5)/4 = 1.625

P3 P1 P2

1 2 3 4 5 6time

P4P1

7

preemptive

P3 P1 P2

1 2 3 4 5 6time

7

P4Non-

preemptive

Real-Time Scheduling – 19Complex Embedded SystemsSSE Zimmermann

Round Robin

Rotation Principle Each process runs for a short time interval (time slice) At the end of the time, the process in interrupted and put

at the end of the ready queue (preemptive scheduling) If a process terminates (or blocks) during its time, another

one is run Newly created processes are put at the end of the ready

queue

Real-Time Scheduling – 20Complex Embedded SystemsSSE Zimmermann

Round Robin

Example: Ready queue contents at time 0: processes in the sequence P1, P2, P3; time slice = 1

Mean waiting time: (2 + 4 + 2 + 2.5)/4 = 10.5/4 = 2.625

P1

P3

P2

1 2 3 4 5 6time

P4

P3P1 P2

1 2 3 4 5 6Zeit

7

P4

ready queue

P2 P1 P2

2

3

3

1

3

1

1

4

4

2

2

4 2

Real-Time Scheduling – 21Complex Embedded SystemsSSE Zimmermann

Non-RT Scheduling Algorithms

Overview First-in-first-out (FIFO, FCFS) Non-preemptive (cooperative multi tasking) DOS, Windows before NT

Shortest job first (SJF) Selection of the process with the smallest computation time Minimizes mean waiting time: early mainframe computers Preemptive or non-preemptive

Round robin Preemptive, time slice

Not useful for (embedded) real-time systems!

Real-Time Scheduling – 22Complex Embedded SystemsSSE Zimmermann

Introduction

Scheduling in Interactive Systems

Real-Time Scheduling Priorities Earliest Deadline First Least Laxity First Periodic Processes

Real-Time Scheduling – 23Complex Embedded SystemsSSE Zimmermann

Real-Time Scheduling

Real-Time Scheduling requires Assignment of processor to the most important taskPriorities What about conflicts between equally prioritized processes? E.g. round robin

Interruptability of less important tasksPreemptive, interrupts may lead to rescheduling When should be checked? Interrupt (automatically) Discrete scheduling points

clock tick, software timer

Task creation and termination Impossible if preemption is disabled!

Real-Time Scheduling – 24Complex Embedded SystemsSSE Zimmermann

Priorities

Priority Urgency of computation Dispatching of processor to task with highest priority May be assigned statically or dynamically RTOS: user-specified process priorities Priorities may be assigned or adapted by OS Process priority change is an important OS operation Scheduling in a narrower sense: assignment of priorities

Real-Time Scheduling – 25Complex Embedded SystemsSSE Zimmermann

Priorities

Example: Priority structure of Windows NT 7 thread

prioritiesin a process

16 process priorities

Real-Time Scheduling – 26Complex Embedded SystemsSSE Zimmermann

Introduction

Scheduling in Interactive Systems

Real-Time Scheduling Priorities Earliest Deadline First Least Laxity First Periodic Processes

Real-Time Scheduling – 27Complex Embedded SystemsSSE Zimmermann

Real-Time Scheduling

Parameters of Processes

tready time

remaining response time

remaining c. time

starting time completion time deadline

now

process

laxity, slack

computation time

latency

trigger event

jitter

Real-Time Scheduling – 28Complex Embedded SystemsSSE Zimmermann

Real-Time Scheduling

Terms and Parameters Ready Time R Computation Time C Deadline D

Process can often be started after their ready time, thus: starting time tS and completion time tC

Derived values Remaining response time a(t) = D – t Remaining computation time CR(t) Laxity (or slack time) L(t) = a(t) – CR(t)

Real-Time Scheduling – 29Complex Embedded SystemsSSE Zimmermann

Earliest Deadline First

Deadline-Based Scheduling Use only deadlines of processes for scheduling, not

computation times The processor is assigned to the task with the closest

deadline (earliest deadline first, EDF) Most well-known algorithm for independent processes

Non-preemptive: reschedule when the currently running task terminates

Preemptive: reschedule when a new process becomes ready

Real-Time Scheduling – 30Complex Embedded SystemsSSE Zimmermann

Earliest Deadline First

Static EDF with Equal Ready Times Sort processes with ascending deadlines [Jackson 55] Whenever a process is scheduled, check if its deadline is

met or violated For the k-th process it must hold that

k

i ki 1

C D k 1 n=

£ =å

Real-Time Scheduling – 31Complex Embedded SystemsSSE Zimmermann

Earliest Deadline First

Example

EDF is optimal for identical ready times!

P3 P1P21 2 3 4 5 6

time

P1

P3

P2

1 2 3 4 5 6time

deadline

Real-Time Scheduling – 32Complex Embedded SystemsSSE Zimmermann

Earliest Deadline First

Proof (indirect) The processor is always utilized because If the schedule has been timely before adding Pk, it is not any

more afterwards:

The only possibility is then to exchange Pk with one of the already planned processes Pj (j < k)

The sum of execution times remains unchanged We know that and thus

Therefore the exchange makes things worse Thus: If this algorithm does not find a solution, there is none

iR 0=

k

i ki 1

C D=

j kD D£k

i ji 1

C D=

Real-Time Scheduling – 33Complex Embedded SystemsSSE Zimmermann

Earliest Deadline First

Static EDF for Individual Ready Times Non-preemptive scheduling is not optimal:

(counter example)

7

P1

P3

P2

1 2 3 4 5 6t

8

P2P3 P1

1 2 3 4 5 6t

7 8

Real-Time Scheduling – 34Complex Embedded SystemsSSE Zimmermann

Earliest Deadline First

There are timely schedules for the example, e.g.

It can be shown that preemptive EDF is optimal for this case [Horn 74]

For the example:

P2 P3P1

1 2 3 4 5 6t

7 8

P2P3 P1

1 2 3 4 5 6t

7 8

P2

Real-Time Scheduling – 35Complex Embedded SystemsSSE Zimmermann

Earliest Deadline First

What About Multi Processor Systems? An example shows that EDF is not optimal even for the

simple case of equal ready times

7

P1

P3

P2

1 2 3 4 5 6t

8

P2P31 2 3 4 5 6

t7 8

Pr1

P11 2 3 4 5 6

t7 8

Pr2

Real-Time Scheduling – 36Complex Embedded SystemsSSE Zimmermann

Earliest Deadline First

There is a timely schedule:

Obvious cause of the problem: the remaining time is not taken into account!

P1P3

1 2 3 4 5 6t

7 8

Pr1

P2

1 2 3 4 5 6t

7 8

Pr2

Real-Time Scheduling – 37Complex Embedded SystemsSSE Zimmermann

Introduction

Scheduling in Interactive Systems

Real-Time Scheduling Priorities Earliest Deadline First Least Laxity First Periodic Processes

Real-Time Scheduling – 38Complex Embedded SystemsSSE Zimmermann

Least Laxity First

Scheduling Based on Slack / Laxity The processor is assigned to the process with the shortest

remaining delay time (least laxity first, LLF, least slack time)

Laxity: time between earliest completion time and deadline, thus EDF plus remaining computation time

Scheduling for Single-Processor Systems Preemptive LLF and preemptive EDF are equivalent

Real-Time Scheduling – 39Complex Embedded SystemsSSE Zimmermann

Least Laxity First

Example: equal ready times, static schedule

P3 P1P2

1 2 3 4 5 6time

P1

P3

P2

1 2 3 4 5 6time

laxity

Real-Time Scheduling – 40Complex Embedded SystemsSSE Zimmermann

Least Laxity First

Example: different ready times, preemptive schedule

t7

P1

P3

P2

1 2 3 4 5 6 8

P2 P3P1

1 2 3 4 5 6t

7 8

P2 P2P1

Real-Time Scheduling – 41Complex Embedded SystemsSSE Zimmermann

Least Laxity First

Under preemptive LLF, only discrete scheduling points must be considered (t = 1 in the example)

Otherwise, infinitely many process changes may happen (in reality, all computation time is wasted for process changes)

Laxity L of processes over time for the example:

P2 P3P1

1 2 3 4 5 6t

7 8

P2 P2P11

2

3

4

5

L(P3)L(P1)

L(P2)

Real-Time Scheduling – 42Complex Embedded SystemsSSE Zimmermann

Introduction

Scheduling in Interactive Systems

Real-Time Scheduling Priorities Earliest Deadline First Least Laxity First Periodic Processes

Real-Time Scheduling – 43Complex Embedded SystemsSSE Zimmermann

Periodic Processes

Timing Behavior of Recurring Processes Periodic Starts exactly every x time units

Or: starts once inside each time interval of length x

Sporadic Not periodic

An upper bound on the number of events per time unit is known

Aperiodic None of the above (worst case)

No guarantees possible

Real-Time Scheduling – 44Complex Embedded SystemsSSE Zimmermann

Periodic Processes

Independent Periodic Processes Important for embedded real-time applications

(remember basic software architectures) Numerous scheduling algorithms have been considered

Usual assumptions Every task has a period T The deadline of each task equals the end of the period The ready time of each task equals the start of the period

Real-Time Scheduling – 45Complex Embedded SystemsSSE Zimmermann

Periodic Processes

Periodic start of tasks preemption must be possible High practical importance, because fixed priorities can

be assigned to processes, and thus common real-time operating systems can be used

P1P1P1

P3t

7

P1

P2

1 2 3 4 5 6 8 159 10 11 12 13 14 16 1917 18 20

P1 P1 P1

P2 P2 P2

P3T3

T1

T2

Real-Time Scheduling – 46Complex Embedded SystemsSSE Zimmermann

Periodic Processes

Arrival-Rate Based Scheduling The higher the arrival rate of a process, the higher is its

assigned static priority (rate monotonic, RM, [Liu und Leyland 73])

Arrival rate = reciprocal of period Arbitrary different priorities are assigned in the case of

equal arrival rates

Real-Time Scheduling – 47Complex Embedded SystemsSSE Zimmermann

Periodic Processes

P1P1P1

P3t

7

P1

P2

1 2 3 4 5 6 8 159 10 11 12 13 14 16 1917 18 20

P1 P1 P1

P2 P2 P2

P3T3

T1

T2

P3P3P3P3

P2

P1P1P1

t7

P1

P2

1 2 3 4 5 6 8 159 10 11 12 13 14 16 1917 18 20

P1 P1 P1

P2 P2 P2

T3

T1

T2

P11

U3

=

P22

U5

=

P33

U15

=

14U

15S =periods: 3, 5, 15common period = least common multiple : 15

Real-Time Scheduling – 48Complex Embedded SystemsSSE Zimmermann

Periodic Processes

Analysis of the Algorithm Critical instant: maximum response time of a task Occurs when all higher prioritized tasks are also runnable Assume two processes P1, P2 with periods i and comp. t.s Ti

During P2, P1 is executed at most times

We thus require

Overall utilization is thus

Optimal static scheduling algorithm for one processor if

utilization U for n tasks not greater than

Thus for large n the maximum is

nn( 2 1)-

nlim U ln 2 0.69¥

= »

2 1 1 2 2T Té ùt t + £ tê úê ú

2 1é ùt tê úê ú

nii

i 1

TU

== tå

Real-Time Scheduling – 49Complex Embedded SystemsSSE Zimmermann

Periodic Processes

Rate-monotonic scheduling is not optimal in general Counter example

Rate monotonic schedule

P2P2t

7

P1

1 2 3 4 5 6 8 159 10 11 12 13 14 16 1917 18 20

P1 P1 P1

T2T1

P1

P2

P2t

7

P1

1 2 3 4 5 6 8 159 10 11 12 13 14 16 1917 18 20

P1 P1 P1 P1

P2P2 P2 P2 P2

Real-Time Scheduling – 50Complex Embedded SystemsSSE Zimmermann

Periodic Processes

However, EDF computes a timely schedule

It can be shown that there is no scheduling algorithm for periodic processes using static priorities that has a better performance!

P2P2t

7

P1

1 2 3 4 5 6 8 159 10 11 12 13 14 16 1917 18 20

P1 P1 P1 P1

P2

Real-Time Scheduling – 51Complex Embedded SystemsSSE Zimmermann

Periodic Processes

Further Properties Processes that start less frequently are preempted more

often and thus delayed more The process with the highest priority is never delayed In the case of an overload situation (exceeding the

assumptions of the analysis), only some lower-priority processes miss their deadlines

Real-Time Scheduling – 52Complex Embedded SystemsSSE Zimmermann

Periodic Processes

What About Aperiodic Processes? Possibility: one dedicated periodic dummy process

executes aperiodic requests [Lehoczky et al. 87] This process is characterized by its period and assigned

run time (run time / period = assigned max. utilization) A certain part of the processor is thus reserved for

aperiodic requests Utilization for this part can be exactly determined by

assigning the highest priority

Stochastic processes: performance evaluation model analysis to derive parameters of dummy process