Vous êtes sur la page 1sur 44

Defence University College Of Engineering

Department of CIT

Course Name: Real Time Embedded System


Course Code: CT-4511
Credit Hours: 4-3-3
Pre-requisite : CT-3222

Instructor information
Name: Berhe G.
Office Location: CRD
Phone NO.: +251-967-261091
Email : berheg21@gmail.com
Week 3
Chapter Four and Five
Fixed and dynamic priority Scheduling

Outline:
 Introduction

 Task Assignment and Scheduling

 Uniprocessor Scheduling algorithm

 Fixed priority scheduling

 Dynamic priority scheduling

Prepared By: berhe G/her


Introduction
• A schedule is an assignment of tasks to the processor time so that
each task is executed until completion. In other words, it is a
reservation of spatial (processor, memory) and temporal (time)
resources for a given task set.

– A schedule is said to be feasible if it meets all application


constraints for a given task set.

– A set of tasks is said to be schedulable if there exists at least


one scheduling algorithm that can generate a feasible
schedule.

– A scheduling algorithm is said to be optimal with respect to


schedulability if it can always find a feasible schedule
whenever any other scheduling algorithm can do so.
Introduction
• Schedulability Analysis

– The tasks must be scheduled to run in order to perform required


functionality

– Schedulability Analysis determines whether all tasks can be


scheduled to run and meet their deadlines while achieving
optimal processor utilization.

– It looks at only temporal requirements, nonfunctional


requirements

– Commonly used practiced analytical method is Rate Monotonic


Analysis(RMA).
Task assignment and Scheduling
• How to assign tasks to processors and to schedule them in
such a way that deadlines are met.
• Scheduling is the act of choosing which event to process at
a given time (which task to execute at a given time).
• Non-Real-Time Scheduling:
– Primary Goal: Maximize performance
– Secondary Goal: Ensure fairness
– Typical metrics:
• Minimize response time
• Maximize throughput
Example:
• FCFS (First-Come-First-Served),
• RR (Round-Robin)
Conti….
• Real-Time Scheduling:
– Primary goal: ensure predictability

– Secondary goal: ensure predictability

– Typical metrics:

• Guarantee miss ration = 0 (hard real-time)


• Guarantee Probability(missed deadline) < X% (firm real-time)
• Minimize miss ration / maximize completion ration (firm
real-time)
• Minimize overall tardiness; maximize overall usefulness (soft
real-time)
Example:
• EDF (Earliest Deadline First)
• LLF (Least Laxity First),
• RMS (Rate-Monotonic Scheduling),
• DMS (Deadline Monotonic Scheduling)
Factors affecting scheduling
– Tasks

• Number of tasks, priorities

– Task characteristics

• Periodicity

• Timing constraints

– Task precedence constraints

– Resource requirements

– Inter-task interactions
Conti…
• Release time: the time which all the data that are required to begin
executing the task are available.
• Deadline: The time by which the task must complete its execution.
It may be hard or soft depending on the nature of the
corresponding task.
• Relative deadline: (Absolute deadline - the release time) i.e if task
Ti has a relative deadline di and is released at time “t” , it must be
executed by time t + di.
• Absolute deadline: The time by which the task must be completed,
that is t+di.
• Feasible assignment/schedule: we say feasible if all the tasks start
after their release and complete before their deadline.
Deadline Scheduling
• Real-time applications are not concerned with speed but with
completing tasks
• In this approach, scheduler is provided with information about task
deadlines.
• Information used
– Ready time
– Starting deadline
– Completion deadline
– Processing time
– Resource requirements
– Priority
– Subtask scheduler
 The deadline scheduler gets the tasks to run before they miss their
deadlines, by preemptively “borrowing” running time from tasks
that normally have higher priorities.
Classification of Scheduling Algorithms
All scheduling algorithms

Static scheduling Dynamic scheduling


(or offline, or clock driven) (or online, or priority driven)

Static-priority Dynamic-priority
scheduling scheduling
Classification of scheduling algorithms
• A schedule is feasible iff
 All the tasks/processes start after their release time
and complete before their deadlines
• Scheduling Policy may be determined
– Pre-run-time
• Schedule created offline
• Not unlike Cyclic Executive approach
– Run-time
• Schedule determined online as tasks arrive
– Needs to be done quickly!
– Can have static or dynamic process priority
Fixed vs Dynamic Priority Algorithms
• Fixed/static-priority algorithm
– Assign the same priority to all the jobs in each task.
– Scheduler can preempt the current process to start a new
process. Priorities are set pre-execution
Examples: Rate-monotonic scheduling (RMS), Deadline
Monotonic scheduling (DM)
a. Rate-monotonic (RM) algorithm: It assigns priorities to tasks
based on their periods: the shorter the period, the higher the
priority. Hence, the higher the rate, the higher the priority.
b. Deadline-monotonic (DM) algorithm: It assigns priorities to
jobs according to their relative deadlines: the shorter the
relative deadline, the higher the priority.
• Dynamic-priority algorithm:
– It can assign different priorities to the individual jobs in each
task. By dynamic, we mean task-level dynamic and job level
fixed.
– The scheduler can assign, and possibly also redefine, process
priorities at run-time
Conti…..
• Most real-time scheduling algorithms of practical interests assign
job-level fixed priorities.
• Jobs are assigned different priority-level in different tasks.
• Once a job is placed in the ready job queue according to the priority
assigned to it, its order with respect to other jobs in the queue
remains fixed.
Example:
a. Earliest Deadline First (EDF),
b. Least Completion Time (LCT),
c. Least Slack Time (LST)
EDF Algorithm
• Earliest-Deadline-First (EDF) algorithm assigns priorities to jobs
according to their deadlines. The earlier the deadline, the higher
the priority.
• This algorithm is optimal when used to schedule jobs on a
processor as long as preemption is allowed and jobs do not contend
for resources.
Conti….
• Schedulability Test: Test to determine whether a feasible
schedule exists
– Sufficient Test
• If test is passed, then tasks are definitely schedulable
• If test is not passed, tasks may be schedulable, but not
necessarily
– Necessary Test
• If test is passed, tasks may be schedulable, but not
necessarily
• If test is not passed, tasks are definitely not schedulable
– Exact Test (= Necessary + Sufficient)
• The task set is schedulable if and only if it passes the test.
Conti….
• Preemptive Scheduling
– Task execution is preempted and resumed later
– Preemption occurs to execute higher priority task.
– Offers higher schedulability
– Involves higher scheduling overhead due to context
switching
• Non-preemptive Scheduling
– Once a task starts executing, it completes its full
execution
– Offers lower schedulability
– Less overhead due to less context switching
Uniprocessor Task Scheduling
• Initial Assumptions:

– Each task is periodic

– Periods of different tasks may be different

– Worst-case task execution times are known

– Relative deadline of a task is equal to its period

– No dependencies between tasks: they are independent

– Only resource constraint considered is execution time

– No critical sections
– Preemption costs are negligible
– Tasks must be completed for output to have any value
Conti….
• Terminology: Temporal Scope of a Task
– C - Worst-case execution time of the task
– D - Deadline of tasks, latest time by which the task
should be complete
– R - Release time
– n - Number of tasks in the system
–  - Priority of the task
– P - Minimum inter-arrival time (period) of the task
• Periodic: inter-arrival time is fixed
• Sporadic: minimum inter-arrival time
• Aperiodic: random distribution of inter-arrival
times
Conti…
Common assumptions for both RM and EDF algorithms:
1. All tasks are preemptable and the cost of preemption is negligible.

2. Only processing requirements are significant; memory, I/O, and


other resource requirements are negligible.

3. All tasks are independent; there are no precedence constraints.


Static-Priority Algorithms
1. Rate-monotonic scheduling
– Best known fixed-priority algorithm, Run time, Static priority
and Preemptive.
– Scheduling policy:
• The tasks with the shortest period gets the highest priority
• The rate (of job releases) is the inverse of the period, so jobs
with shorter period have higher priority
– Additional assumptions:
4. All tasks are periodic
5. For all Ti: Di = Pi
Example, consider a system of 3 tasks:
• T1 = (4, 1) ⇒ rate = 1/4 fi = 1/Ti
• T2 = (5, 2) ⇒ rate = 1/5 : Frequency (=rate)
• T3 = (20, 5) ⇒ rate = 1/20
• Relative Priorities : T1 > T2 > T3
Conti…
Example 1: p1 = (10,20,20) p2 = (15,30,30) utilization is 100%

Exercise:
– Check for p2 = (10,30,30), does it met the deadline if
utilization reduced to 83%?
Conti….
Example 2: Tasks t1=(1,4) , t2=(2,5) , t3=(5,20)
• 1st instant Task 1 runs to completion
• 1st inst Task 2 runs to completion
• 1st inst Task 3 runs for 1 unit
..at Eu=4, Task 1 released -- preempts Task 3
• 2nd inst Task 1 runs to completion .... At Eu =5, Task 2
released
• 2nd inst Task 2 runs to completion
• 1st inst Task 3 runs for 1 unit .... At Eu = 8, Task 1
released---- preempts Task3
• 3rd inst Task 1 runs to completion
• 1st inst Task 3 runs for 1 unit .... At Eu = 10, 3rd inst of Task
2 released --preempts 3
• At Eu = 15, 1st inst Task 3 completes…. CPU idle Eu 18-20
• At Eu = 20, all 3 tasks released ……Cycle repeats
–Total utilization(U) = 0.25+0.4+0.25= 0.9
122312231322133213 1
RMS Schedulability Test
• Key metric is
– Processor utilization: Sum of compute time divided by period for
each process:
U =  ci / pi
• No schedule can possibly exist if U > 1
– No processor can be running 110% of the time
• Fundamental result:
– RMS schedule always exists if U < n (2 1/n – 1) , where n =# of tasks
• For RMS feasible schedulability:

 Necessary schedulability test for RMS: U ≤ 1

 Sufficient schedulability test for RMS: U ≤ n (2 1/n – 1)


General schedulability test Criteria
• Sufficient condition: Total processor utilization is not
greater than n(21/n - 1)
– If U <= n(21/n -1) --------- where n is number of tasks
• RM will definitely produce feasible schedule
• No need for further analysis
– However
• RM may produce feasible schedule where U > n(21/n -1)
i.e. Sufficient but not necessary condition
• Recall Example above : CPU U = 0.9 but still schedulable
– Depends on particular task characteristics
• If U > n(21/n -1)
– Need to perform further schedulability analysis
– As “n “ increases, bound -> 69%
Response time analysis 2
1. Response time analysis is a feasibility test
• Thus exact , hence necessary and sufficient
2. If the task set passes the test then all its tasks will meet
all their deadlines
3. If it fails the test then , at run time, some tasks will miss
their deadline and FPS tells us exactly which.
• Unless the computation time estimation (the WCET)
themselves turn out to be pessimistic.
RM Schedulability Analysis
Example: Taskset T1, T2, T3, T4 where P1 < p2 < p3 < p4

• Task 1
– Highest priority.. never preempted
– Will run immediately once released
– For Task 1 to be feasibly scheduled…Only condition to
check is that e1<=p1
• Include Task 2 in task set

– Can only preempted by Task 1 and will be executed iff can


find sufficient time e2 over period [0, p2]

– Say Task 2 completes at time t within [0, p2]

– How many times did Task 1 run over [0, t]?

• Each iteration must also complete in this interval [0,t]


RM Schedulability Analysis
– Over interval [0,t], Task 1 completes [t/p1]
– Must satisfy condition
• t = e2 + e1 [t/p1]
– Need to find t over interval [ 0, p2]
– Only need to check this when new instances of task 1 are
released
– Find integer k such that:
• k p1 >= k e1 + e2 and k p1 <= p2
• Include Task 3
– Can be preempted by Task 1 and 2
– Need to find t over [0, p3] ……. t=[ t/p1] e1 + [t/p2] e2 +e3
– Need to check only at multiples of p1 and/or p2
• Similar analysis for Task 4
– Can be preempted by Task 1, 2, 3
RM Schedulability Analysis
Example: Consider Task set T1(20,100), T2(30,180), T3(80,240), T4(100,400).
• Check points
– W1: {100} • Task 1/2/3/4 is RM Schedulable iff
– W2: {100,180}
• e1 + e2+ e3+ e4 <= 100 or …. (False:230)
– W3: {100,180,200,240}
• 2e1 + e2+ e3+ e4 <= 180 or ….. (False:250)
– W4: {100,180,200,240,300,360,400}
• For n = 4 • 2e1 + 2e2+ e3+ e4 <= 200 or …(False:280)
– n(21/n -1) = 0.76 • 3e1 + 2e2+ e3+ e4 <= 240 or …. (False:300)
– U = 0.95 (0.2+0.166+0.33+0.25) • 3e1 + 2e2+ 2e3+ e4 <=300 or …. (False:380)
• Further analysis • 4e1 + 2e2+ 2e3+ e4 <=360 or … (False:400)
• Task 1 is RM Schedulable iff
• 4e1 + 3e2+ 2e3+ e4 <=400 …. (False:430)
• e1<=100 (True)
• By including Task 4, not RM schedulable
• Task 1/2 is RM Schedulable iff
• e1 + e2 <= 100 or ….(True: 50)
• 2 e1 + e2 <= 180 …. (True: 70)
• Task 1/2/3 is RM Schedulable iff
• e1 + e2+ e3 <= 100 or …. (False: 130)
• 2 e1 + e2 + e3 <= 180 or ….. (True:150)
• 2 e1 + 2e2 + e3 <= 200 or …(True:180)
• 3 e1 + 2e2+ e3<= 240 …. (True:200)
Response time analysis-1
• The worst-case response time Ri of task Ti is first
calculated and then checked(trivially) with its deadline
• Where I is the interference from higher priority tasks
• Ri ≤ Di
• Ri = Ci+Ii
• Calculating R:
• During R, each higher priority task will execute a
number of times
• The ceiling function [ƒ] gives the smallest integer
greater than the fractional number ƒ on which it acts.

Example: The ceiling of 1/3 is 1, of 6/5 is 2 and of 6/ is 2


• The total interference suffered by Ti from Tj in Ri is
given by [Ri/Tj]*Cj.
Response time Equation
Response time Equation
Example: task set D
Task set D : a(7,3) , b (12, 3), c(20,5)
Total utilization: 0.4285+ 0.25 + 0.25= 0.9285

Ra =3 wc 0 = 5 + 3 + 3=11 ,
wc 1 = 5 + [11/7]x 3=10
wb 0 = 3 +3=6
wc 2= 5 + [10/7] x 3 + [10/12]x3=13
wb 1 = 3 + [6/7]x 3=6
wc 3= 5 + [13/7] x 3 + [13/12]x3=15
wb 2= 3 + [6/7] x 3=6
wc 4= 5 + [15/7] x 3 + [15/12]x3=16
wc 5= 5 + [16/7] x 3 + [16/12]x3=16
Rb=6
Rc=16
Example: task 1 (40,100), task 2(40, 150), task 3(100,350)
• Total utilization=0.4+0.267+0.286= 0.953>B(3)=0.779!
• UB test is inclusive: we need Precise test
• but we do have U(T1)+U(T2)= 0.4+0.267= 0.667<U(2)=0.828
• so we need to calculate R3 only!
• Calculate response time for task 3
• R30 = C1+C2+C3= 180
• R31 = C3+[R30/T1]*C1+[R30/T2]*C2
=100+ [180/100]*40+[180/150]*40
=100+2*40+2*40=260
• R32 =C3+[R31/T1]*C1+[R31/T2]*C2
=100+ [260/100]*40+[260/150]*40=300
• R33 =C3+[R32/T1]*C1+[R32/T2]*C2
=100+ [300/100]*40+[300/150]*40=300 (done)

Task 3 is schedulable and so are the others!


2. Earliest Deadline First (EDF)
• Run-time, Dynamic and Preemptable
• A task with a shorter deadline has a higher priority
• Task priorities are re-evaluated when tasks released/completed
• Whenever a new task arrive, sort the ready queue so that the task
closest to the end of its period assigned the highest priority
• Preempt the running task if it is not placed in the first of the queue
in the last sorting
• Schedulability Test: Utilization Bound (Liu & Layland)
– Sufficient and Necessary test:
– Total utilization of task set must not exceed 1
– If U <=1, then task set is EDF schedulable
• Performance :
– EDF is optimal for uniprocessor scheduling algo. i.e. if EDF can’t
feasibly schedule a task set on a uniprocessor , there is no other
scheduling algorithm that can.
– If you’re overloaded, ensures that a lot of tasks don’t complete
– Gives everyone a chance to fail at the expense of the later tasks
Conti…
Example 1: EDF Meeting a Deadline: p1 = (10,20,20) p2 = (15,30,30)

1
Utilization is 100%
2

P2 takes priority because its deadline is sooner

Example 2: T1=(4,1), T2=(5,2), T3(7,2)….(Period, Execution time)


Conti….
EDF Algorithm:
• If D=T
– Shortest of the deadline of given task is selected as higher
priority to be executed .
– The highest priority task is executed first.

• If D!=T

– If D>T : the task is not schedulable probably which means


it misses the deadline.

– If D< T
1. The task set is just checked for the feasible solution.
2. Feasible solution is found by using the processor
Demand Analysis.
Example:
Example: T1(1,4) , T2(2,5) , T(5,20) all tasks released at time 0
Total utilization(U) = 0.25+0.4+0.25= 0.9
• 1st inst Task 1 runs 1st as earliest deadline of 4
• 1st inst Task 2 runs to completion
• 1st inst Task 3 runs for 1 unit.. Note that deadline is 20
..at Eu=4, Task 1 released -- preempts Task 3 as deadline is 8
• 2nd inst Task 1 runs to completion ..At Eu =5, Task 2 released
• 2nd inst Task 2 runs to completion as deadline is 10
• 1st inst Task 3 runs for 1 unit
.. At Eu = 8, Task 1 released---- preempts Task3
• 3rd inst Task 1 runs to completion
• 1st inst Task 3 runs for 1 unit
.. At Eu = 10, Task 2 released … preempts task 3 as deadline is 15
• At Eu = 12, Task 1 runs as deadline 16<20
• At Eu = 15, task 2 released and runs with deadline 20
• At Eu = 16, task 1 released with deadline 20…no preemption

122312231322133221 1
Exercise
Task Computation time Time Period (D=T)
A 1 3
B 1 6
C 1 5
D 3 10

1. Check the schedulability for EDF , if yes schedule using timeline


graph method for EDF
2. Check the schedulability of the task sets for RMS using
a. Utilization method (checking both sufficient and necessary
conditions )
b. Using the response time analysis method (precise method)
c. Using timeline graph method
Priority Inversion
• Consider tasks with mutual exclusion constraints.

• Priority inversion is a phenomenon that occurs when a higher-


priority task is blocked by a lower-priority task.

– Direct blocking: a high-priority task must not preempt the


exclusive resource use by a low-priority task.

– Indirect blocking of a high-priority task by a medium-


priority task – the medium priority task preempts a low-
priority task that holds a shared resource – has to be
avoided.

Critical Sections are source of priority inversion problem


Solutions for Priority inversion
1. Priority Inheritance:
– When a low-priority task blocks one or more tasks of higher
priority, it temporarily assumes the highest priority of a task it
blocks.

– When lower priority job blocks a higher priority job, it inherits


priority of the blocked job. Or
– A lower priority owner of a resource needed by a higher priority
task is shortly boosted to higher priority .
Disadvantage:
– Does not prevent deadlocks in a program with circular lock
dependencies.
– A chain of blocking may occur: blocking duration can be
substantial, though bounded.
Solutions for Priority inversion
2. Priority Ceiling Protocol:
– It is based on priority inheritance, but uses a Priority Ceiling
(maximum limit) for each critical section.
– Each process has a default priority.
– Each resource is assigned a priority ceiling (max. limit) equal to
the priority of the highest priority task that can use the resource.

– At each time instant a task executes at a dynamic priority that is


the maximum of its own static priority and the ceiling values of
all resources that it has locked.

– A task can only assume a new resource if the task’s priority is


higher than the priority ceilings of all the resources locked by
other tasks.

Advantage : Prevents deadlocks and unbound priority inversion


Conti…..
• The priority ceiling protocol is the same as priority inheritance
protocol, except that a task, T, can be blocked from entering a
critical section if there exists any semaphore currently held by some
other task whose priority ceiling is greater than or equal to the
priority of T.
Exercise:
1. What is scheduling? State the strategy for scheduling.

2. Discuss off-line versus on-line scheduling

3. Compare and contrast periodic, aperiodic and sporadic tasks

4. Discus preemptive and non-preemptive priority scheduling


and describe situations in which a task should not be
preempted.
5. Given task set ti(ci, pi):=T1(1,4), T2(2,6), T3(1,10) by applying
the UB Test
a. What is the utilization of each task?
b. Is the task set schedulable by RM ?
c. Draw the timeline.
d. Is the task set schedulable by RM if C3=2?
Exercise:
6. Check the schedulability of the following task sets by RM and
EDF, if yes schedule them using timeline graph method.

a. T1 = {(8,4),(12,4),(20,4)}

b. T2 ={(8,4),(10,2),(12,3)}

c. T3 ={(3,1),(4,2),(6,1)}
7. Assume the periodic processors P1=(1,4,4), P2=(1,3,3) and
P3=(2,6,6) which shall be scheduled on a single processor
a. Give the processor utilization
b. Can the RMA produce a feasible schedule? In such a case give
a schedule.
c. Can the EDF algorithm produce a feasible schedule? In such a
case, give a schedule.
d. Compare RM and EDF scheduling. What is the advantage
and disadvantage of each method?
Exercise:
8. Construct a set of periodic tasks (with release times, execution
times and periods) which can be scheduled feasibly by the EDF
algorithm, but not by the RM algorithm.
9. A schedule can be constructed using methods like EDF and
Indicate whether the following statements are true and explain
your answer briefly
a. With EDF a higher utilization is possible than with RMS
b. RMS is more efficient than EDF
c. RMS is more general than EDF (i.e., applicable in more
situations).
d. Explain the difference between deadline monotonic scheduling
and Earliest Deadline First.
10. Determine whether the following set of periodic real time tasks are
schedulable on a uniprocessor using RMA. (Start time, Processing
Time, Period, deadline).
T1(20,25,150,100), T2(40,7,40,40),T3(60,10,60,50),T4(25,10,30,20).