Rajib Mall Chapters 1,2,3

343
03/08/10 1 1 Characteristics of Real-Time Systems (Lecture 2) Dr. RAJIB MALL Professor Department Of Computer Science & Engineering IIT Kharagpur.

description

engg book

Transcript of Rajib Mall Chapters 1,2,3

Page 1: Rajib Mall Chapters 1,2,3

03/08/10 11

Characteristics of Real-Time Systems

(Lecture 2)

Dr. RAJIB MALLProfessor

Department Of Computer Science & Engineering

IIT Kharagpur.

Page 2: Rajib Mall Chapters 1,2,3

03/08/10 22

What is a Real-Time System?

• A system is called real-time: Whenever we need to quantitatively

express time to describe its behaviour.

• Recall how to describe the behaviour of a system? List inputs to the system and the

corresponding system response.

Page 3: Rajib Mall Chapters 1,2,3

03/08/10 33

Important Characteristics• An embedded system responds to

events.

Process New Data

External Input Event

External Output Event

Example: An Automobile airbag system.

When the airbag’s motion sensors detect a collision, the system needs to respond by deploying the airbag within 10ms or less.

– or the system fails!

Page 4: Rajib Mall Chapters 1,2,3

03/08/10 44

Embedded Systems = Hardware + RTOS +Application Program

SENSORS D/ACONVERSION

DIAGNOSTICPORT

SOFTWARE

A/DCONVERSION CPU ACTUATORS

MEMORY

HUMANINTERFACE

AUXILLARY SYSTEMS(POWER, COOLING)

ELECTROMECHANICALBACKUP & SAFETY

External

Environment

Page 5: Rajib Mall Chapters 1,2,3

03/08/10 55

• An embedded system responds to external inputs: If response time to an event is too

long, the system fails.

• General purpose Operating Systems:Not designed for real-time use

• Real-time Operating Systems:Help tasks meet their deadline.

What is a Real-Time OS?

Page 6: Rajib Mall Chapters 1,2,3

03/08/10 66

Characteristics of An Embedded System

•Real-time:• Every real-time task is associated with some time constraints, e.g. a Deadline.

•Correctness Criterion:• Results should be logically correct,

• And within the stipulated time.

Page 7: Rajib Mall Chapters 1,2,3

03/08/10 77

Characteristics of an Embedded System cont…

• Safety and Task Criticality: A critical task is one whose failure

causes system failure (example: obstacle avoidance).

A safe system does not cause damage.

A safety-critical real-time system is one where any failure causes severe damage.

Page 8: Rajib Mall Chapters 1,2,3

03/08/10 88

Characteristics of an Embedded System cont…

• Concurrency:A RT system needs to respond to

several independent events.

Typically separate tasks process each independent event.

For the same inputs, the result can be different (Non-determinism).

Page 9: Rajib Mall Chapters 1,2,3

03/08/10 99

Characteristics of Embedded Systems cont…

• Distributed and Feedback Structure

• Custom Hardware: An embedded system is often

implemented on custom H/W that is specially designed and developed for the purpose.

Page 10: Rajib Mall Chapters 1,2,3

03/08/10 1010

Characteristics of Embedded Systems

Feedback structure of real-time systems

Page 11: Rajib Mall Chapters 1,2,3

03/08/10 1111

Characteristics of Embedded Systems cont…

• Reactive: On-going interaction between

computer and environment.

• Stability: Under overload conditions, work

acceptably for at least important tasks.

• Exception Handling:

Page 12: Rajib Mall Chapters 1,2,3

03/08/10 1212

Safety and Reliability

•Independent concepts in traditional systems.

• A safe system does not cause damage even when it fails.

• A reliable system operates for long time without any failure.

Page 13: Rajib Mall Chapters 1,2,3

03/08/10 1313

Safety and Reliability• In traditional systems, safety and

reliability are independent concerns: A system can be safe and unreliable

and vice versa.

Give examples of:• A safe and unreliable system

• A reliable and unsafe system

Page 14: Rajib Mall Chapters 1,2,3

03/08/10 1414

Safety and Reliability cont…

• Interrelated in safety-critical system.A safety critical system is one

for which any failure of the system would result in severe damage.

• Safety can be ensured only through increased reliability.

Page 15: Rajib Mall Chapters 1,2,3

03/08/10 1515

Safety and Reliability• An unreliable system can be made

safe upon a failure: By reverting to a fail-safe state.

• A fail-safe state: No damage can result if a system

fails in this state.

Example: For a traffic light, all lights orange and blinking.

Page 16: Rajib Mall Chapters 1,2,3

03/08/10 1616

Fail-Safe State• The fail-safe state of a word

processing program:The document being processed has

been saved onto the disk.

• Fail-safe states help separate the issues of safety and reliability.Even if a system is unreliable, it

can always be made to fail in a fail-safe state.

Page 17: Rajib Mall Chapters 1,2,3

03/08/10 1717

Safety and Reliability• For a safety-critical system

No fail-safe state exists.• Consider the navigation system on an

aircraft:• When the navigation system fails:• Shutting down the engine can be of

little help! As a result, for a safety-critical

system:• The only way to achieve safety is by

making it reliable.

Page 18: Rajib Mall Chapters 1,2,3

03/08/10 1818

Safety-Critical Systems

• A safety-critical system is one for which a failure can cause severe damages.

• A safety-critical system does not have any fail-safe states:Safety can only be ensured through increased reliability.

Page 19: Rajib Mall Chapters 1,2,3

03/08/10 1919

How to Design a Highly Reliable System?

–Error Avoidance

–Error Detection and Removing

–Fault Tolerance

Page 20: Rajib Mall Chapters 1,2,3

03/08/10 2020

Fault Tolerance in RT System

• The essential idea: Provide redundancy

• Hardware Fault-Tolerance: Masks the effects of a hardware

fault.

• Software Fault-Tolerance: Masks the effects of a program

fault.

Page 21: Rajib Mall Chapters 1,2,3

03/08/10 2121

Fault Tolerance in RT System

–Hardware FT:•Built in self test (BIST)

•Triple modular redundancy

–Software FT:•N-Version programming

•Recovery Blocks

Page 22: Rajib Mall Chapters 1,2,3

03/08/10 2222

Triple Modular Redundancy

C1, C2 and C3 are redundant copies of same component

Page 23: Rajib Mall Chapters 1,2,3

03/08/10 2323

N-version Programming• Software fault tolerance technique

inspired by TMR of hardware: Different teams are employed to

develop the same software. Unsatisfactory performance in

practice. Reason: Faults are correlated in the

different versions. All versions fail for similar reasons.

Page 24: Rajib Mall Chapters 1,2,3

03/08/10 2424

Recovery Blocks

Software Fault Tolerance using recovery blocks

Page 25: Rajib Mall Chapters 1,2,3

03/08/10 2525

Modern Embedded Systems

• Embedded systems incorporate: Application-specific hardware (ASICs, FPGAs

etc.)• performance, low power

Programmable processors: DSPs, controllers etc.

Mechanical transducers and actuators

Application Specific HW

Processor Cores

Analog I/O

Memory

Page 26: Rajib Mall Chapters 1,2,3

03/08/10 2626

Block Diagram of An Embedded System

Processor

control panel

Real-timeOS

controllerprocesses

UIprocesses

ASIC

ProgrammableDSP

ProgrammableDSP

DSPAssembly

Code

DSPAssembly

Code

Dual-portedRAM

CODEC

Page 27: Rajib Mall Chapters 1,2,3

03/08/10 2727

Some Basic Issues(Lecture 3)

Dr. RAJIB MALLProfessor

Department Of Computer Science & Engineering

IIT Kharagpur.

Page 28: Rajib Mall Chapters 1,2,3

03/08/10 2828

Why Have an OS in an Embedded Device?

• Support for: Multitasking, scheduling, and synchronization

Timing aspects.

Memory management

File systems

Networking

Graphics displays

Interfacing wide range of I/O devices

Scheduling and buffering of I/O operations

Security and power Management

Page 29: Rajib Mall Chapters 1,2,3

03/08/10 2929

• Example: A recent cell phone operating system contained over five million lines of code!

• Few, if any projects will have the time and funding: To develop all of this code on their own!

• Typical Embedded OS license fees are a few dollars per device –-- less than a desktop OS

• Some very simple low-end devices might not need an OS: But new devices are getting more complex.

Why Have an OS in an Embedded Device?

Page 30: Rajib Mall Chapters 1,2,3

03/08/10 3030

What is Actually Being Used?

• What Types of Processors are used?

• What Operating Systems are used?

• What Programming Languages are used?

• Will examine data from a 2006 Market Survey of design engineers:

Embedded Systems Design Magazine.

Page 31: Rajib Mall Chapters 1,2,3

03/08/10 3131

Processor Bit Size Used in New Embedded Designs

Data was derived from EETimes and Embedded Systems Design Magazine 2006 Embedded Market Survey

0.00% 10.00% 20.00% 30.00% 40.00% 50.00% 60.00%

4-bit

8-bit

16-bit

32-bit

64-bit

Page 32: Rajib Mall Chapters 1,2,3

03/08/10 3232

Processor Architectures Widely Used in New Embedded Designs

• ARM• X86• PowerPC• MIPS• Xscale (ARM)• Renesas SuperH

Page 33: Rajib Mall Chapters 1,2,3

03/08/10 3333

32-64 bit Annual Processor Sales

0.00% 5.00% 10.00% 15.00% 20.00% 25.00% 30.00% 35.00% 40.00%

PowerPC

SuperH

MIPS

X86

ARM

Processor Sales Volume

Based on 2002 sales data

Page 34: Rajib Mall Chapters 1,2,3

03/08/10 3434

Number of Processors Used in New Embedded Designs

Data was derived from EETimes and Embedded Systems Design Magazine 2006 Embedded Market Survey

0.00% 10.00% 20.00% 30.00% 40.00% 50.00% 60.00%

More Than 5

3-5 Processors

2 Processors

1 Processor

Page 35: Rajib Mall Chapters 1,2,3

03/08/10 3535

Processor Selection Issues• Software Support

OS, Compilers, Debug Tools, Applications• Price• Performance• Power

Battery Life (MIPS/Watt), Cooling (Fan?)• Desktop PC 100 W vs. Battery power 200 mw

• Availability Long term availability, Multiple Vendors?

Page 36: Rajib Mall Chapters 1,2,3

03/08/10 3636

Use of Real-Time OS Kernels in New Embedded Designs

0.00% 10.00% 20.00% 30.00% 40.00% 50.00%

Open Source

Internally Developed

None

Commercial OS

Data was derived from EETimes and Embedded Systems Design Magazine 2006 Embedded Market Survey

Page 37: Rajib Mall Chapters 1,2,3

03/08/10 3737

Pros and Cons of Open Source OS

• Embedded devices are extremely cost sensitive:Even a $1 license fee per device

can make a product uncompetitive.

• For satisfactory performance:The source code often needs to

be fine tuned.

Page 38: Rajib Mall Chapters 1,2,3

03/08/10 3838

Open Source OS: Cons• “Free” OS can increase product development

cost: More time to develop device drivers, increased

labor cost.

More than offset the commercial OS license fees saved.

• Some other licenses may still required: Encoders & decoders, encryption, media player

• Open source license model may require: That you publish your device’s source code

Page 39: Rajib Mall Chapters 1,2,3

03/08/10 3939

Commercial Operating Systems used in New Embedded Designs

0.00% 5.00% 10.00% 15.00% 20.00% 25.00% 30.00%

Others

Palm

Green Hills

Symbian

Wind River

Microsoft Emb.

Data was derived from EETimes and Embedded Systems Design Magazine 2006 Embedded Market Survey

Page 40: Rajib Mall Chapters 1,2,3

03/08/10 4040

Programming Languages Used in New Embedded Designs

0.00% 10.00% 20.00% 30.00% 40.00% 50.00% 60.00% 70.00%

Others

Assembly

Java

C#

C++

C

Data was derived from EETimes and Embedded Systems Design Magazine 2006 Embedded Market Survey

Page 41: Rajib Mall Chapters 1,2,3

03/08/10 4141

Future of Embedded Systems

• Use of multi-core processors:Present operating systems and tools do not make satisfactory utilization of the multiple cores.

• Support of wireless and mobile Internet.

• Power minimization.

Page 42: Rajib Mall Chapters 1,2,3

03/08/10 4242

Modelling Timing Constraints

(Lecture 4)

Dr. RAJIB MALLProfessor

Department Of Computer Science & Engineering

IIT Kharagpur.

Page 43: Rajib Mall Chapters 1,2,3

03/08/10 4343

Types of Real-Time Systems

• Real-time systems are different from the traditional systems: Tasks have deadlines associated with

them.

• A classified based on the consequence of a failure: Hard real-time systems

Soft real-time systems

Firm real-time systems

Page 44: Rajib Mall Chapters 1,2,3

03/08/10 4444

Hard Real-Time Systems• If a deadline is not met:

The system is said to have failed.

• The task deadlines are of the order of micro or milliseconds.

• Many hard real-time systems are safety-critical.

• Examples: Industrial control applications

On-board computers

Robots

Page 45: Rajib Mall Chapters 1,2,3

03/08/10 4545

Firm Real-Time Systems

• If a deadline is missed occasionally, the system does not fail: The results produced by a task after

the deadline are rejected.

Utility

D Time

Page 46: Rajib Mall Chapters 1,2,3

03/08/10 4646

Firm Real-Time Systems

• Examples:A video conferencing application

A telemetry application

Satellite-based surveillance applications

Page 47: Rajib Mall Chapters 1,2,3

03/08/10 4747

Soft Real-Time Systems• If a deadline is missed, the system

does not fail: Only the performance of the system is

said to have degraded.

The utility of a result decreases with time after the deadline.

Utility

D Time

Page 48: Rajib Mall Chapters 1,2,3

03/08/10 4848

Soft Real-Time Systems

• Examples of soft real-time systems:Railway reservation system

Web browsing

In fact, all interactive applications

Page 49: Rajib Mall Chapters 1,2,3

03/08/10 4949

Types of Real-Time Tasks Periodic:

• Periodic tasks repeats after a certainfixed time interval.

Sporadic:• Sporadic tasks recurs at random instants.

Aperiodic:• Same as sporadic except that minimumseparation between two instances can be0.

Page 50: Rajib Mall Chapters 1,2,3

03/08/10 5050

Timing Constraints• A timing constraint:

Defined with respect to some event.

• An event:Can occur at an instant of timeMay also have durationGenerated either by the system

or its environment

Page 51: Rajib Mall Chapters 1,2,3

03/08/10 5151

Events in a Real-Time System

• Events in a real-time system can be classified into:

Stimulus Events

Response Events

Page 52: Rajib Mall Chapters 1,2,3

03/08/10 5252

Stimulus Event• Generated by the environment:

Act on the system.

• Typically asynchronous in nature: Aperiodic Can also be periodic

• Example: A user pressing a button on a

telephone set Stimulus event acts on the telephone

system.

Telephone System

Page 53: Rajib Mall Chapters 1,2,3

03/08/10 5353

Stimulus Event cont…

• Periodic stimulus event example : Periodic sensing of temperature in a

chemical plant.

Page 54: Rajib Mall Chapters 1,2,3

03/08/10 5454

Response Event• Produced by the system:

In response to some stimulus events

• Example: In a chemical plant as soon as the

temperature exceeds 100°C,

The system responds by switching off the heater.

Page 55: Rajib Mall Chapters 1,2,3

03/08/10 5555

Classification of Timing Constraints

• Classification of the timing constraints in a system:

Can help us to quickly identify these from a problem description.

Page 56: Rajib Mall Chapters 1,2,3

03/08/10 5656

Classification of Timing Constraints

• Different timing constraints can broadly be classified into:

Performance constraints

Behavioral constraints

Page 57: Rajib Mall Chapters 1,2,3

03/08/10 5757

Types of Timing Constraints

• Performance constraints:Imposed on the response of the system.

• Behavioral constraints:Imposed on the stimuli generated by the environment.

Page 58: Rajib Mall Chapters 1,2,3

03/08/10 5858

Types of Timing Constraints

• Both performance and behaviorial constraints can be classified into:Delay Constraints

Deadline Constraints

Duration Constraints

Page 59: Rajib Mall Chapters 1,2,3

03/08/10 5959

Delay Constraint• Expresses minimum time delay:

Allowed between the occurrence of two arbitrary events e1 and e2

t(e2) - t(e1) ≥ d if e2 occurs earlier than d then a

delay violation would occur.t(e1) t(e2)

d

Page 60: Rajib Mall Chapters 1,2,3

03/08/10 6060

Deadline Constraint• Expresses the maximum

permissible separation:

Between any two arbitrary events.

t(e2) - t(e1) ≤ d

Page 61: Rajib Mall Chapters 1,2,3

03/08/10 6161

Duration Constraint• A duration constraint on an

event:Specifies the time period over

which the event acts.

• A duration constraint can be:minimum typemaximum type.

Page 62: Rajib Mall Chapters 1,2,3

03/08/10 6262

Duration Constraints• Minumum:

Once a duration event starts:• It must not end before a certain

minimum time.

• Maximum: Once a duration event starts:

• It must end before a certain maximum time.

t(e1) t(e2)

Max

t(e1) t(e2)

Min

Page 63: Rajib Mall Chapters 1,2,3

03/08/10 6363

Timing Constraints in an Example System

PSTN

Page 64: Rajib Mall Chapters 1,2,3

03/08/10 6464

SS Deadline Example• Deadline is defined between two

stimuli. A behavioral constraint.

Imposed on stimulus.

• Once a user completes dialling a digit, He must dial the next digit within the

next 5 seconds.

Otherwise an idle tone is produced.

PSTN

Page 65: Rajib Mall Chapters 1,2,3

03/08/10 6565

RS Deadline Example• Deadline is defined on the stimulus

from the respective response event. A behaviorial constraint.

Imposed on stimulus.

• Once the dial tone appears: The first digit must be dialed within 30

seconds,

Otherwise the system entersan idle state and an idle tone is produced

PSTN

Page 66: Rajib Mall Chapters 1,2,3

03/08/10 6666

RR Deadline Example• Deadline is defined on the

response from another response. A performance constraint.

Imposed on response.

• Once ring tone is given to the callee, Ring back tone must be given to the

caller within two seconds,

Otherwise the call is terminated.

PSTN

Page 67: Rajib Mall Chapters 1,2,3

03/08/10 6767

SR Deadline Example• Deadline is defined on the response

from the respective stimulus. A performance constraint. Imposed on response.

• Once the receiver of the hand set is lifted: The dial tone must be produced by the

system within 2 seconds, otherwise a beeping sound is produced

until the handset is replaced.

PSTN

Page 68: Rajib Mall Chapters 1,2,3

03/08/10 6868

SS Type Delay Constraint

• A behaviorial constraint. Imposed on the environment.

• Once a digit is dialled, The next digit should be dialled

after at least 1 second.

Otherwise, a beeping sound is produced until the call initiator replaces the handset.

PSTN

Page 69: Rajib Mall Chapters 1,2,3

03/08/10 6969

Duration Constraint• Specifies the time interval over which the

event acts.• If you press the button of the handset for

less than 15 seconds, It connects to the local operator.

• If you press the button for any duration lasting between 15 to 30 seconds, It connects to the international operator.

• If you keep the button pressed for more than 30 seconds, Then on releasing it would produce the dial

tone.

Page 70: Rajib Mall Chapters 1,2,3

03/08/10 7070

Timing Constraints

Behaviorial ConstraintsPerformance Constraints

Delay Deadline Duration

RR SR RR SR

Delay Deadline Duration

RS SS RS SS

Page 71: Rajib Mall Chapters 1,2,3

03/08/10 7171

Modelling Timing Constraints (cont…)

(Lecture 5)

Dr. RAJIB MALLProfessor

Department Of Computer Science & Engineering

IIT Kharagpur.

Page 72: Rajib Mall Chapters 1,2,3

03/08/10 7272

Why Model Timing Constraints?

• Modelling time constraints in a system: Can serve as a formal specification

of the system.

May be used to automaticallygenerate code.

Can help to understand real-time behavior.

Page 73: Rajib Mall Chapters 1,2,3

03/08/10 7373

Modelling Time Constraints

• Several approaches can be used. We discuss an approach based on FSM

proposed by Dasarathy (IEEE TSE, 1985).

• A state is defined in terms of the values assumed by some attributes. The states of an elevator may be denoted

in terms of its directions of motion.

Values of the attribute direction define the states up, down, and stationery.

Page 74: Rajib Mall Chapters 1,2,3

03/08/10 7474

FSM

• Transition is annotated with: Enabling event

Action that takes place during transition

S1 S2

e1/set-timer(20 msec)

Page 75: Rajib Mall Chapters 1,2,3

03/08/10 7575

Model of SS Deadline Constraint

• Once a user completes dialling a digit, He must dial the next digit within the next

5 seconds.

Otherwise an idle tone is produced.

Await second digit

AwaitNextdigit

AwaitCallerOnhook

Page 76: Rajib Mall Chapters 1,2,3

03/08/10 7676

Model of RS Deadline Constraint

• Once the dial tone appears: The first digit must be dialed within 30 seconds,

Otherwise the system entersan idle state and an idle tone is produced

Await first digit

AwaitNextdigit

AwaitCallerOnhook

Page 77: Rajib Mall Chapters 1,2,3

03/08/10 7777

Model of SR Deadline Constraint

• Once the receiver is lifted from the hand set: The dial tone must be produced by the system

within 2 seconds,

Otherwise a beeping sound is produced until the handset is replaced.

Await Dialtone

Awaitfirstdigit

AwaitReceiverOnhook

Page 78: Rajib Mall Chapters 1,2,3

03/08/10 7878

Model of RR Deadline Constraint

• Once ring tone is given to the callee, Ring back tone must be given to the caller within

two seconds,

Otherwise the call is terminated.

Await Ringback

tone

Awaitfirstdigit

AwaitReceiverOnhook

Page 79: Rajib Mall Chapters 1,2,3

03/08/10 7979

Model of Delay Constraint

• Once a digit is dialled, The next digit should be dialled after at least 1

second.

Otherwise, a beeping sound is produced until the call initiator replaces the handset.

Await nextevent

Awaitnextdigit

AwaitReceiverOnhook

Page 80: Rajib Mall Chapters 1,2,3

03/08/10 8080

Duration Constraint Example

• If you press the button of the handset for less than 15 seconds, It connects to the local operator.

• If you press the button for any duration lasting between 15 to 30 seconds, It connects to the international operator.

• If you keep the button pressed for more than 30 seconds, Then on releasing it would produce the

dial tone.

Page 81: Rajib Mall Chapters 1,2,3

03/08/10 8181

Model Construction for Duration Example

• To construct the model:

First identify the deadline and delay constraints.

The constraints are for which events?

Page 82: Rajib Mall Chapters 1,2,3

03/08/10 8282

Model of Duration Constraint Example

Await event1

Local operator

Internloperator

Await event2

Await Buttonrelease

Dialtone

Page 83: Rajib Mall Chapters 1,2,3

03/08/10 8383

Exercise 1• When the temperature of a

chemical reactor rises above T°C:The heater needs to be shut-

off within 10mSecOtherwise, the entire plant is

shut-off.

Page 84: Rajib Mall Chapters 1,2,3

03/08/10 8484

Exercise 2• Identify and model time

constraints:Once the start switch of a

wash machine is pressed, Stirring commences:

• Continues until a preset timer expires or the stop button is pressed by the user.

Page 85: Rajib Mall Chapters 1,2,3

03/08/10 8585

Basics of Real-Time Task Scheduling

(Lecture 6)

Dr. RAJIB MALLProfessor

Department Of Computer Science & Engineering

IIT Kharagpur.

Page 86: Rajib Mall Chapters 1,2,3

03/08/10 8686

Introduction• Real-time tasks are generated due

to certain event occurrences:Either internal or external events.

• Example:A task may get generated due to a

temperature sensor sensing high-level.

• When a task gets generated: It is said to be released or arrived .

Page 87: Rajib Mall Chapters 1,2,3

03/08/10 8787

Real-Time Task Scheduling• Task Scheduling:

Essentially refers to the order in which the various tasks are to be executed.

Primary means adopted by an operating system to meet task deadlines.

So, scheduling is an important problem in RTOS.

Page 88: Rajib Mall Chapters 1,2,3

03/08/10 8888

Task Instance• A task typically recurs a large

number of times: Each time triggerred by an event

Each time a task recurs, an instance of the task is said to have been generated.

• The ith time a task T recurs: Task instance Ti is said to have

arrived.

Page 89: Rajib Mall Chapters 1,2,3

03/08/10 8989

Relative and Absolute Deadlines

• Absolute deadline: Counted from time 0.

• Relative deadline: Counted from time of occurrence of

task.

abs

deadline

relative

0 t d

Page 90: Rajib Mall Chapters 1,2,3

03/08/10 9090

Response Time• The time from the occurrence of the

event generating the task: To the time results are produced by the

task.

Response time

0 e t

Page 91: Rajib Mall Chapters 1,2,3

03/08/10 9191

Response Time• For soft real-time tasks:

The response time needs to be minimized.

• For hard real-time tasks:As long as the task completes

within its deadline, • No advantage of completing it any early.

Page 92: Rajib Mall Chapters 1,2,3

03/08/10 9292

Types of Real-Time Tasks Periodic:

• Periodic tasks repeats after a certainfixed time interval.

• A vast majority of real-time tasks areperiodic.

Sporadic:• Sporadic tasks recurs at random instants.

Aperiodic:• Same as sporadic except that minimumseparation between two instances can be0.

Page 93: Rajib Mall Chapters 1,2,3

03/08/10 9393

Phase of a Periodic Task

• Phase for a periodic task: The time from 0 till the occurrence of

the first instance of the task.

Denoted by Φ.

Φ

0 e1 timee2 e3

P P

Page 94: Rajib Mall Chapters 1,2,3

03/08/10 9494

Phase Example• The track correction task starts

2000 mSecs after the launch of the rocket: Periodically recurs every 50 milli

Seconds then on. Each instance of the task requires

a processing time of 8 mSecs and its relative deadline is 50 mSecs.

Φ

0e1 timee2

e3

P P

Page 95: Rajib Mall Chapters 1,2,3

03/08/10 9595

Important Scheduling Terminologies

– Valid Schedule:• At most one task is assigned to a processor at a time.

• No task is scheduled before it is ready.

• Precedence and resource constraints of all tasks are satisfied.

– Feasible Schedule:• Valid schedule is one in which all tasks meet their respective time constraints.

Page 96: Rajib Mall Chapters 1,2,3

03/08/10 9696

Important Scheduling Terminologies

– Proficient Scheduler:• A scheduler S1 is as proficient as another Scheduler S2:

• If whichever tasks that S2 can feasibly schedule so can S1, but not vice versa.

• Equally proficient schedulers:

• If a task set scheduled by one can also be scheduled by the other and vice versa.

Page 97: Rajib Mall Chapters 1,2,3

03/08/10 9797

Important Scheduling Terminologies

Optimal Scheduler:•An optimal scheduler can feasibly schedule any task set that can be scheduled by any other scheduler.

Page 98: Rajib Mall Chapters 1,2,3

03/08/10 9898

Scheduling Points• At these points on time line:

Scheduler makes decision regarding which task to be run next.

• Clock-driven: Scheduling points are defined by

interrupts from a periodic timer.

• Event-driven: Scheduling points defined by task

completion and generation events.

Page 99: Rajib Mall Chapters 1,2,3

03/08/10 9999

Real-Time Task Scheduling cont…

• Significant amount of research has been carried out to develop schedulers for real-time tasks:

Schedulers for uniprocessors

Schedulers for multiprocessors and distributed systems.

Page 100: Rajib Mall Chapters 1,2,3

03/08/10 100100

Task Scheduling on Uniprocessors

• Focus of much research during the 1970s and 80s.

• Real-time task schedulers can be broadly classified into: Clock-driven

Event-driven

Page 101: Rajib Mall Chapters 1,2,3

03/08/10 101101

Clock-Driven Scheduling: Basics

• Decision regarding which job to run next is made only at clock interrupt instants: Timers are used to determine the

scheduling points.

• Which task to be run when and for how long is stored in a table.

Page 102: Rajib Mall Chapters 1,2,3

03/08/10 102102

Clock-Driven Scheduling

• Popular examples:Table-driven scheduler

Cyclic scheduler

• Round robin scheduling is also an example of clock-driven scheduling.

Page 103: Rajib Mall Chapters 1,2,3

03/08/10 103103

Clock-Driven Schedulers• Also called offline schedulers

and also static schedulers:Used extensively in embedded

applications.

• Pro: Very space and time efficient.

• Con: Cannot handle sporadic or aperiodic tasks.

Page 104: Rajib Mall Chapters 1,2,3

03/08/10 104104

Table-Driven Scheduling

TaskStart timeStop time

T1 0 100

T2 101 150

T3 151 225

Page 105: Rajib Mall Chapters 1,2,3

03/08/10 105105

Cyclic Schedulers(Lecture 7)

Dr. RAJIB MALLProfessor

Department Of Computer Science & Engineering

IIT Kharagpur.

Page 106: Rajib Mall Chapters 1,2,3

03/08/10 106106

Task Scheduling on Uniprocessors

• Focus of much research during the 1970s and 80s.

• Real-time task schedulers can be broadly classified into: Clock-driven

Event-driven

Page 107: Rajib Mall Chapters 1,2,3

03/08/10 107107

Clock-Driven Scheduling

• For scheduling n periodic tasks:The schedule is stored in a table.•Repeated forever.

The designer needs to develop aschedule for what period?

• LCM(P1,P2,...,Pn).

Page 108: Rajib Mall Chapters 1,2,3

03/08/10 108108

Clock-Driven Scheduling• Used in low cost applications:

Pro:• Compact: Require very little storage space

• Efficient: Incur very little runtime overhead.

Con:• Inflexible: Very difficult to accommodate dynamic tasks.

• The simplest is table-driven scheduler.

Page 109: Rajib Mall Chapters 1,2,3

03/08/10 109109

Disadvantage of Table-Driven Schedulers

• When the number of tasks are large:Requires setting a timer large

number of times.The overhead is significant:

•Remember that task instance runs only for a few milli or microseconds.

Page 110: Rajib Mall Chapters 1,2,3

03/08/10 110110

Cyclic Schedulers• Cyclic schedulers are very

popular:Extensively being used in the

industry. A large majority of small

embedded applications being manufactured at present use cyclic schedulers.

Page 111: Rajib Mall Chapters 1,2,3

03/08/10 111

Cyclic Schedulers• The schedule is stored for one

major cycle: Precomputed schedule is repeated.

• A major cycle is divided into: One or more minor cycles (frames).

• Scheduling points for a cyclic scheduler: Occur at the beginning of frames.

Page 112: Rajib Mall Chapters 1,2,3

03/08/10 112

Major Cycle• In each major cycle:

The different tasks recur at identical time points.

Major cycleMajor cycle

Frame (minor cycle)

Page 113: Rajib Mall Chapters 1,2,3

03/08/10 113

Major Cycle• The major cycle of a set of tasks

ST={T1,T2,...,Tn} is at least:

LCM(p1,p2,...,pn)

Holds even when tasks have arbitrary phasings.

Can be greater than LCM when F does not divide major cycle.

Page 114: Rajib Mall Chapters 1,2,3

03/08/10 114114

Minor Cycle (Frame)• Each major cycle:

Usually contains an integral number of minor cycles(frames).

• Frame boundaries are marked:

Through interrupts from a periodic timer.

Page 115: Rajib Mall Chapters 1,2,3

03/08/10 115

A Typical Schedule

T2F3

T3F2

T2F1

TaskFrame

Page 116: Rajib Mall Chapters 1,2,3

03/08/10 116116

Minor Cycle (Frame)• Each task is assigned to run in

one or more frames.

• Frame size (F) is an important design parameter while using cyclic scheduler.A selected frame size has to

satisfy a few constraints.

Page 117: Rajib Mall Chapters 1,2,3

03/08/10 117117

Selecting an Appropriate Frame Size (F)

• Minimum context switch: F should be larger than each task size. Sets a lower bound.

• Minimization of table size: F should squarely divide major cycle. Allows only a few discrete frame size.

• Satisfaction of task deadline: Between the arrival of a task and its

deadline:• At least one full frame must exist.

Sets an upper bound

Page 118: Rajib Mall Chapters 1,2,3

03/08/10 118118

Minimum Context Switch

• A task instance must complete running within its assigned frame:

A task might otherwise have to be suspended and restarted in a later frame.

The scheduler would get invoked many times.

Page 119: Rajib Mall Chapters 1,2,3

03/08/10 119

Minimum Context Switch

•To avoid unnecessary context switches:

•Selected frame size should be larger than execution time of each task.

•Sets a lower bound for frame size.

Page 120: Rajib Mall Chapters 1,2,3

03/08/10 120120

Minimization of Table Size• Unless the minor cycle squarely

divides the major cycle: Storing schedule for one major

cycle would not be sufficient.

Schedules in the major cycle would not repeat:• This would

make the size of the table large.

Major cycleMajor cycle

Frame

Page 121: Rajib Mall Chapters 1,2,3

03/08/10 121

Major cycleMajor cycle

Frame

Page 122: Rajib Mall Chapters 1,2,3

03/08/10 122122

Satisfaction of Task Deadline

• Between the arrival of a task and its deadline: At least one full frame must exist.

• If there is not even a single frame: The task would miss its deadline,

• By the time it could be taken up for scheduling, the deadline could be imminent. ti di

F F

Page 123: Rajib Mall Chapters 1,2,3

03/08/10 123123

Satisfaction of Task Deadline

• The worst case for a task occurs when the task arrives just after a frame has started.

ti di

ti di

ti would miss deadline

ti deadline can be met

Page 124: Rajib Mall Chapters 1,2,3

03/08/10 124124

Satisfaction of Task Deadline

• The minimum separation of an arrival time for ti from a frame start:

GCD(F,pi)

• Thus, for all ti

2F-GCD(F,pi) ≤ di must be satisfied

ti di

F F

Page 125: Rajib Mall Chapters 1,2,3

03/08/10 125

Selection of a Suitable Frame Size

• Several frame sizes may satisfy the constraints:• Plausible frames.

• A plausible frame size has been found:• Does not mean that the task set is schedulable.

• The smallest plausible frame size needs to be chosen:• The chances of successful scheduling is higher.

Page 126: Rajib Mall Chapters 1,2,3

03/08/10 126126

Example 1• Compute a suitable frame

size for the following task set:e1 = 1, p1=4, d1=4e2 = 1, p2=5, d2=5e3 = 1.5, p3=20, d3=20

Page 127: Rajib Mall Chapters 1,2,3

03/08/10 127127

Example 2• Compute a suitable frame

size for the following task set:e1 = 1, p1=4, d1=4e2 = 2, p2=6, d2=6e3 = 3, p3=20, d3=20

Page 128: Rajib Mall Chapters 1,2,3

03/08/10 128

What If None of the Frame Sizes is Suitable?

•Possible culprit is a task with large

execution time e.g. (20,100,100):

• Prevents a smaller frame from being chosen.• Try splitting task with large execution time into two or three sub-

tasks.

• Example: T1=(20,100,100) can be split into

(10,100,100) and (10,100,100).

Page 129: Rajib Mall Chapters 1,2,3

03/08/10 129129

Pros and Cons of Cyclic Schedulers

• As number of tasks increases: It becomes very difficult to

select a suitable frame size.

Results in suboptimal schedules. • CPU times in many frames are wasted.

• Results in poor schedulability.

Page 130: Rajib Mall Chapters 1,2,3

03/08/10 130

A Generalized Task Scheduler

• Many practical systems:• Consist of a mixture of periodic, aperiodic, and sporadic tasks.

• How can sporadic and aperiodic tasks be scheduled using a cyclic scheduler?

• Use the slack time and the unused frames:• As and when the spordic and aperiodic tasks arise.

Page 131: Rajib Mall Chapters 1,2,3

03/08/10 131

cyclic-scheduler() { current-task T = SchedTab[k];k = k + 1;k = k mod N;

dispatch-current-Task(T);schedule-sporadic-tasks();

schedule-aperiodic-tasks();

idle(), }

Cyclic Scheduler: Pseudocode

Page 132: Rajib Mall Chapters 1,2,3

03/08/10 132132

Event-Driven Scheduling

(Lecture 8)

Dr. RAJIB MALLProfessor

Department Of Computer Science & Engineering

IIT Kharagpur.

Page 133: Rajib Mall Chapters 1,2,3

03/08/10 133

Event-Driven Schedulers

• Unlike clock-driven schedulers:

These can handle sporadic and aperiodic tasks.

Used in more complex applications.

Page 134: Rajib Mall Chapters 1,2,3

03/08/10 134134

Event-Driven Scheduling• Scheduling points:

Defined by task completion and arrival events.

• Preemptive schedulers:

On arrival of a higher priority task, the running task may be preempted.

• Simplest event-driven scheduler:

Foreground-Background Scheduler

Page 135: Rajib Mall Chapters 1,2,3

03/08/10 135135

Scheduling Points for Event-Driven Schedulers

• Scheduling decisions are made when certain events occur:

•A task becoming ready

•A task completing execution

Page 136: Rajib Mall Chapters 1,2,3

03/08/10 136

Time-Sliced Round Robin Scheduling: A Hybrid Scheduler

•Time-sliced round robin schedulers:• Commonly used in the traditional operating

systems.•We shall not discuss much about it.

•Scheduling points:• Tasks completing or suspending• Clock interrupts

•Less proficient than cyclic and table-driven schedulers:• Foreground task priorities not taken into

account.

Page 137: Rajib Mall Chapters 1,2,3

03/08/10 137137

Event-Driven Schedulers• These are called preemptive

schedulers: When a higher priority task becomes ready any executing lower priority task is preempted.

• These are greedy schedulers: Never keep the processor idle if a

task is ready.

Page 138: Rajib Mall Chapters 1,2,3

03/08/10 138138

Preemptive Scheduling

• Independent tasks execute on a uniprocessor.Two algorithms pretty much summarise the important results in this case:•EDF

•RMA

Page 139: Rajib Mall Chapters 1,2,3

03/08/10 139139

Foreground-Background Scheduler

• Real-time tasks are run as foreground tasks.

Sporadic, aperiodic, and non-real-time tasks are run as background tasks.

• Among the foreground tasks, at every scheduling point:

The highest priority foreground task is scheduled.

• A background task can run:

When no foreground task is ready.

Page 140: Rajib Mall Chapters 1,2,3

03/08/10 140

Background Task Completion Time

•Let TB be the only background task• Its processing time be eB

• The time for it to complete would be:

i

i

B

p

e

ectB

1

Page 141: Rajib Mall Chapters 1,2,3

03/08/10 141

Example 1• Consider a real-time system:

• Tasks are scheduled using foreground-background scheduling.

• There is only one periodic foreground task:• P1=50 msec, e1=100 msec, d1=100 msec

• Background task TB, eB=1020msec.

• Compute the completion time for the background task.

Page 142: Rajib Mall Chapters 1,2,3

03/08/10 142

Example 2

•On account of every context switch :•Assume an overhead of 1 msec.

•Compute the completion time of TB.

Page 143: Rajib Mall Chapters 1,2,3

03/08/10 143143

Event-Driven Static Priority Schedulers

• The task priorities once assigned by the programmer:

Do not change during runtime.

RMA (Rate Monotonic Algorithm) is the optimal static priority scheduling algorithm.

Page 144: Rajib Mall Chapters 1,2,3

03/08/10 144144

Event-Driven DynamicSchedulers

• The task priorities can change during runtime:Based on the relative urgency

of completion of tasks.

EDF (Earliest Deadline First) is the optimal uniprocessor scheduling algorithm.

Page 145: Rajib Mall Chapters 1,2,3

03/08/10 145145

Event-Driven Schedulers• First let us consider the

simplest scenario:Uniprocessor

Independent tasks:•Tasks do not share resources

•There is no precedence ordering among the tasks

Page 146: Rajib Mall Chapters 1,2,3

03/08/10 146146

EDF• EDF is the optimal uniprocessor

scheduling algorithm:• If EDF cannot feasibly schedule

a set of tasks:Then, there can exist no other

scheduling algorithm to do that.

• Can schedule both periodic and aperiodic tasks.

Page 147: Rajib Mall Chapters 1,2,3

03/08/10 147

EDF•At any scheduling point:

•The scheduler dispatches the task

having the shortest deadline among

all ready tasks.

•Preempts any task (with longer

deadline) that might be running.

Page 148: Rajib Mall Chapters 1,2,3

03/08/10 148

EDF Schedulability Check

•Sum of utilizations due to all tasks is less than one:

•Both the necessary and sufficient condition for schedulability.

11

iup

en

ii

i

Page 149: Rajib Mall Chapters 1,2,3

03/08/10 149149

Example 1• Compute a suitable frame

size for the following task set:e1 = 1, p1=4, d1=4e2 = 1, p2=5, d2=5e3 = 5, p3=20, d3=20

Page 150: Rajib Mall Chapters 1,2,3

03/08/10 150

Is EDF Really a Dynamic Priority Scheduling Algorithm?

•If EDF were a dynamic priority

algorithm:

• At any point of time, the priority value

of a task can be determined.

•Also we should be able to show how it

changes with time.

Page 151: Rajib Mall Chapters 1,2,3

03/08/10 151

Dynamic Priority•The longer a task waits in ready queue:

• The higher the chance (probability) of

its being taken up for scheduling.

• We can imagine a virtual priority value

associated with a task:

• Keeps increasing with time until the task is

taken up for scheduling

Page 152: Rajib Mall Chapters 1,2,3

03/08/10 152152

EDF: An Evaluation• EDF is:

Simple Optimal

• But, is rarely used:No commercial operating system

directly supports EDF scheduling.Why?

•Let us examine the disadvantages of EDF.

Page 153: Rajib Mall Chapters 1,2,3

03/08/10 153

Disadvantages of EDF

•Main disadvantages of EDF:

•Poor transient overload handling

•Runtime inefficiency

•Poor support for resource sharing

among tasks.

Page 154: Rajib Mall Chapters 1,2,3

03/08/10 154154

Poor Transient Overload Handling

• Why does transient overload occur? A task might take more time than

estimated.

Too many tasks might arise on some event.

• When an executing task takes more time: It is extremely difficult to predict

which task would miss its deadline.

Page 155: Rajib Mall Chapters 1,2,3

03/08/10 155155

Runtime Inefficiency• EDF is not very efficient:

In an implementation of EDF:• The tasks need to be maintained sorted.

• A priority queue based on task deadlines is required.

The complexity of maintaining a priority queue is:• log(n), n is the number of tasks.

Page 156: Rajib Mall Chapters 1,2,3

03/08/10 156156

Poor Resource Sharing Support

• Support for tasks to share non-premptable resources (critical sections):

Extremely inefficient.

Tasks often miss their deadlines.

We shall elaborate this problem later in our discussions.

Page 157: Rajib Mall Chapters 1,2,3

03/08/10 157157

General EDF Schedulability• When task deadlines differ from task

periods: The schedulability criterion needs to be

generalized:

• If pi<di, it becomes sufficient: But not a necessary condition

1),min(1

n

iii

i

dp

e

Page 158: Rajib Mall Chapters 1,2,3

03/08/10 158158

Implementation of EDF• Simple FIFO queue:A freshly arriving task is inserted at the end of the queue.

• Sorted queue:Priority queue

Page 159: Rajib Mall Chapters 1,2,3

03/08/10 159159

An Efficient Implementation of EDF

• Maximum number of distinct deadlines is fixed.

• A queue is maintained for each distinct deadline.

• When a task arrives: Its absolute deadline is

computed and inserted in Q.

Page 160: Rajib Mall Chapters 1,2,3

03/08/10 160

Efficient EDF Implementation

160

Task Queue

Deadline Levels

1

2

3

4

5

6

Page 161: Rajib Mall Chapters 1,2,3

03/08/10 161

MLF: Variation of EDF• Minimum Laxity First:

• Laxity= relative deadline – time required to complete task execution

• The task that is most likely to fail first is assigned highest priority.

• EDF=MLF when tasks have equal execution times and deadlines.

• Bonus problem: • Analyze performance of MLF wrt EDF.

Page 162: Rajib Mall Chapters 1,2,3

03/08/10 162162

Rate Monotonic Scheduling

(Lecture 9)

Dr. RAJIB MALLProfessor

Department Of Computer Science & Engineering

IIT Kharagpur.

Page 163: Rajib Mall Chapters 1,2,3

03/08/10 163163

RMA• The priority of a task is

proportional to its frequency. The higher the frequency (or lower

the period) of a task, the higher is its priority.

Frequency

Priority

Page 164: Rajib Mall Chapters 1,2,3

03/08/10 164164

RMA• The optimal uniprocessor static

priority scheduling algorithm:

If RMA cannot schedule a set of periodic tasks:

•No other static priority scheduling algorithm can.

• Schedulability test:

1. Utilization Bound

Page 165: Rajib Mall Chapters 1,2,3

03/08/10 165

Utilization Bound 1

•Sum of utilization due to tasks is less than one:

•Necessary condition for schedulability:

•But not the sufficient condition.

11

iup

en

ii

i

Page 166: Rajib Mall Chapters 1,2,3

03/08/10 166166

Sufficient Condition for RMA Schedulability

• Utilization bound II (Liu and Layland 1971):

• ui is the processor utilization due to task Ti

• n is the number of tasks

Utilization bound

Number of tasks

)12(

1

nnui

Page 167: Rajib Mall Chapters 1,2,3

03/08/10 167

Liu and Layland Bound

•The maximum utilization for a schedulable task set:• Falls as the number of tasks increases.

•For a very large number of tasks:•What is the maximum utilization permitted?

692.02ln)12(1

iu

Page 168: Rajib Mall Chapters 1,2,3

03/08/10 168168

Example 1• Check whether the followingtask set is schedulable usingRMA:T1: e1 = 1, p1=4, d1=4T2: e2 = 2, p2=6, d2=6T3: e3 = 3, p3=20, d3=20

Page 169: Rajib Mall Chapters 1,2,3

03/08/10 169

Solution to Example 1

• Therefore the task set is schedulable.

115

11

60

44

20

3

3

1

4

11

iup

enii

i

778.0733.015

11

778.0)1259.1(3)12(3)12( 3

11

iu

nn

Page 170: Rajib Mall Chapters 1,2,3

03/08/10 170170

Liu and Layland Condition• The upper bound on utilization converges

to 69% (ln2):

As the number of tasks approaches infinity

• Liu and Layland's condition is conservative:

If a set of tasks passes Liu and Layland test, then it is definitely RMA schedulable.

But, even if a task set fails Liu-Layland test:

• Still it may be RMA schedulable (completion time theorem).

Page 171: Rajib Mall Chapters 1,2,3

03/08/10 171171

Example 2• Check RMA schedulability of three

periodic tasks: T1: e1=20mSec, p1= d1= 100mSec

T2: e2=40msec, p2= d2= 150mSec

T3: e3=100mSec, p3= d3= 350mSec

• The utilization for these 3 tasks are 0.2+0.267+0.286=0.753

• Liu-Layland bound=3((2)1/3-1) =0.779 Therefore, the tasks are schedulable.

Page 172: Rajib Mall Chapters 1,2,3

03/08/10 172172

Some More Issues in RMA Scheduling

(Lecture 10)

Dr. RAJIB MALLProfessor

Department Of Computer Science & Engineering

IIT Kharagpur.

Page 173: Rajib Mall Chapters 1,2,3

03/08/10 173

RMA Schedulability

• Utilization bound 1:

• Utilization bound 2:(Liu-Layland)

• Schedulability check 3: Liu and Lehoczky’s Completion Time Theorem

11

iup

en

ii

i

)12(

1

nnui

U1U2

Page 174: Rajib Mall Chapters 1,2,3

03/08/10 174174

RMA Schedulability Test- III

• Completion time theorem (Liu and Lehoczky 1989):

If each of a set of tasks individually meets its first deadline under zero phasing:

•Then the task set is RMA schedulable for all task phasings.

Page 175: Rajib Mall Chapters 1,2,3

03/08/10 175175

Completion Time Theorem (Liu and Lehoczky)

• How to check schedulability of a task set? Consider zero phasing for all tasks. Draw up the schedules till the first

deadline of each task. Observe if each task is schedulable. Then the task set is schedulable.

• Drawing the schedule is cumbersome: When the number of tasks is large.

Page 176: Rajib Mall Chapters 1,2,3

03/08/10 176

Time in milliseconds

Completion Time Theorem: Basic Premise

• Worst case completion time for a task: Occurs when it is in phase with its higher

priority tasks.

176

T1=10,30 Φ=0

T2=60,120 Φ=0

T1=10,30 Φ=20

T2=60,120 Φ=0

(a) T1 is in phase with T2

T2 T2T1

20 30 50 60

T2T1

Time in milliseconds80

(b) T1 has a 20 ms phase lag with T2

T1 T1 T1T2 T2 T2

10 30 40 60 70 90

Page 177: Rajib Mall Chapters 1,2,3

03/08/10 177177

Testing Liu and Lehoczky's Criterion

Page 178: Rajib Mall Chapters 1,2,3

03/08/10 178178

Example 3• Consider three periodic tasks–T1: e1=20mSec, p1=100mSec

–T2: e2=30mSec, p2=150mSec

–T3: e3=60mSec, p3=200mSec

• Check whether they are RMA schedulable.

Page 179: Rajib Mall Chapters 1,2,3

03/08/10 179

Answer 3•Checking for Liu-Layland criterion:

• The criterion satisfied:

–Therefore, the task set is schedulable.

78.07.0600

420

200

60

150

30

100

201

iup

enii

i

Page 180: Rajib Mall Chapters 1,2,3

03/08/10 180180

Example 4• Consider three periodic tasks–T1: e1=20mSec, p1=100mSec

–T2: e2=30mSec, p2=150mSec

–T3: e3=90mSec, p3=200mSec

• Check whether the tasks are RMA schedulable.

Page 181: Rajib Mall Chapters 1,2,3

03/08/10 181

Answer 4•Checking for Liu-Layland test:

• Fails Liu-Layland test:

–Before concluding about schedulability, let us check Liu-Lehoczky criterion.

78.085.0600

510

200

90

150

30

100

201

iup

enii

i

Page 182: Rajib Mall Chapters 1,2,3

03/08/10 182

Checking for Lehoczky’s Criterion

182

T1

20 100

Deadline for T1

(a) T1 meets its first deadline

50

T2

(b) T2 meets its first deadline

Deadline for T2

150

T3 T1 T2

Deadline for T3

T3

120 190

(c) T3 meets its first deadline

T1: e1=20mSec, p1=100mSecT2: e2=30mSec, p2=150mSecT3: e3=90mSec, p3=200mSec

Page 183: Rajib Mall Chapters 1,2,3

03/08/10 183

Checking for Lehoczky’s Criterion

•For T1: 20< 100 • Satisfied

•For T2: 30+20*2 =70<150 • Satisfied

•For T3: 90 + 20*2+30*2=190<200• Satisfied

•The task set is schedulable.

T1: e1=20mSec, p1=100mSecT2: e2=30mSec, p2=150mSecT3: e3=90mSec, p3=200mSec

Page 184: Rajib Mall Chapters 1,2,3

03/08/10 184

Practice Problem•Test whether the following task set

is RMA schedulable.

•T1: e1=10, p1=d1=50, Φ1=100

•T2: e2=20,p2=d2=60, Φ2=0

•T3: e3=30, p3=d3=80, Φ3=50

Page 185: Rajib Mall Chapters 1,2,3

03/08/10 185

Further Tests?• What if a task set fails Liu-Lehoczky’s criterion?

• Can we conclude task set is unschedulable?

• Or is it necessary to check further?

• Liu-Lehoczky test checks the worst case scenario (zero

phasings).

• It is possible that the worst case may not occur for a task set and

the task set may actually be schedulable.

Page 186: Rajib Mall Chapters 1,2,3

03/08/10 186

A Basic Question

•Construct an example for which EDF and RMA produce different schedules. •Possibly simplest answer is:• Consider a task set unschedulable in RMA

but schedulable in EDF.

•A nontrivial example would be:• Consider a task set in which a task is

preempted before completion.

Page 187: Rajib Mall Chapters 1,2,3

03/08/10 187

Example•T1: e1=3,p1=8

•T2: e2=6, p2=12

3 8 11

T1 T1

RMA

3 9

T1 T2 T1EDF

123 8 11

T1 T1

RMA

12

Page 188: Rajib Mall Chapters 1,2,3

03/08/10 188188

RMA Under Transient Overload

• RMA is stable under transient overload conditions:When a task gets late:

• It can be guaranteed that it will not affect higher priority tasks.

Reason: Even when a task gets late• It must yield the CPU to a higher priority task --- unlike EDF.

Page 189: Rajib Mall Chapters 1,2,3

03/08/10 189

Implementation of RMA

•A naïve RMA implementation:•Maintain tasks in a FIFO Queue.

• Insertion O(1)

• Searching O(n)

•A better implementation:•Maintain tasks in a priority Q

• Insertion O(log(n))

• Searching O(1)

•Efficient implementation:•Multilevel feedback Q

Page 190: Rajib Mall Chapters 1,2,3

03/08/10 190

Multi-Level Feedback Queue

190

Task Queue

Priority Levels

1

2

3

4

5

6

Bonus Problem:Analyze time and space complexity, etc. and compare with other implementations

Page 191: Rajib Mall Chapters 1,2,3

03/08/10 191191

Deadline Monotonic Scheduling and Other

Issues(Lecture 11)

Dr. RAJIB MALLProfessor

Department Of Computer Science & Engineering

IIT Kharagpur.

Page 192: Rajib Mall Chapters 1,2,3

03/08/10 192192

RMA Schedulability of Harmonically Related Tasks

• A set of periodic tasks is harmonically related, iff: For every pair of tasks Ti and Tk:

• If Pi>Pk, then Pi=n*Pk, where n is an integer. (Example p1=10,p2=20,p3=60)

• A set of harmonically related tasks is RMA schedulable: If the sum total of the utilization due

to the tasks is less than 1.

Page 193: Rajib Mall Chapters 1,2,3

03/08/10 193

Schedulability of a Harmonic Task Set

•By completion time theorem:

•For harmonically related tasks:•Ceiling can be removed since the periods

are integral multiples.

• So,

ik

k

ii

ki pe

p

pe

*

1

1

1,

1

1

1

1

i

k

p

eor

p

e

p

e

in

i

ki

ki

i

Page 194: Rajib Mall Chapters 1,2,3

03/08/10 194

Deadline Monotonic Algorithm (DMA)

• When task deadline and periods are different, i.e., di ≠ pi:RMA is not an optimal scheduling

algorithm.DMA is optimal for such task

sets

• Essence of DMA:Assign priorities based on task

deadlines.194

Page 195: Rajib Mall Chapters 1,2,3

03/08/10 195

Deadline Monotonic Algorithm(DMA)

• When do RMA and DMA produce identical schedules? Relative deadline of every task in a

task set is the same as its period.

• For arbitrary relative deadlines: DMA may produce a feasible

schedule even when RMA fails.

On the other hand, RMA will always fail if DMA fails.

195

Page 196: Rajib Mall Chapters 1,2,3

03/08/10 196

Exercise• Check for schedulability of following

tasks under RMA and DMA. T1 =(e1 = 10 ms, p1 = 50 ms, d1 = 35 ms)

T2 =(e2 = 15 ms, p2 =100 ms, d2 = 20ms)

T3 =(e3 =20 ms, p3 =200 ms,d3 =200ms)

196

Page 197: Rajib Mall Chapters 1,2,3

03/08/10 197

Answer• RMA: Checking Liu-Layland criterion

• The task set is unschedulable

• DMA completion time check:

• T2 meets its first deadline: 15 < 20

• T1 meets its first deadline: 15+20=35 <=35

• T3 meets its first deadline: 15*2+10*4+20=90<200

• The task set is schedulable.

11400

1590

200

20

20

15

35

101

iup

enii

i

Page 198: Rajib Mall Chapters 1,2,3

03/08/10 198

Overhead Due to Context Switching

• Context switching of tasks consumes some time: So far we neglected the overhead due to

context switching.

• When a task arrives: It preempts the currently running lower

priority task.

There may be no preemption if the CPU was idle or a higher priority task was running. 198

Page 199: Rajib Mall Chapters 1,2,3

03/08/10 199

Overhead Due to Context Switching

• In the worst case, each task incurs at most two context switches:

1. When it runs possibly preempting the currently running task.

2. When it completes.

• Let the context switching time be constant and equal c ms.

• Effectively, the execution time of each task increases to (ei + 2*c)

199

T1 T1 T1T2 T2 T2

10 30 40 60 70 90

Page 200: Rajib Mall Chapters 1,2,3

03/08/10 200

Example•Assume 3 periodic tasks:• T1: e1=10mSec, p1= d1= 50mSec

• T2: e2=25msec, p2= d2= 150mSec

• T3: e3=50mSec, p3= d3= 200mSec

•Assume context switching time = 1msec

•Determine whether the task set is schedulable.

Page 201: Rajib Mall Chapters 1,2,3

03/08/10 201

Answer•Effect of context switch:• Execution time of each task increases

at most by 2 msec.

• Task T1: 12msec <50 mSec >>Schedulable

• Task T2: 27+ 12*3=63<150 msec >>Schedulable

• Task T3: 52+ 12*4 + 27*2 =154<200msec >>Schedulable

T1: e1=10mSec, p1= d1= 50mS

T2: e2=25msec, p2=d2= 150msec

T3: e3=50mSec, p3= d3= 200mSec

Page 202: Rajib Mall Chapters 1,2,3

03/08/10 202

Practice Problem• Check for schedulability of following

tasks under RMA. T1 =(e1 = 10 ms, d1 = p1 = 50 ms)

T2 =(e2 = 5 ms, d2 = p2 =20 ms)

T3 =(e3 =9 ms, d3 = p3 =30 ms)

Assume that context switch overhead is 1 ms.

202

Page 203: Rajib Mall Chapters 1,2,3

03/08/10 203

Self Suspension• When does a task suspend execution?

It performs input/output operations

It is waiting for some event to occur

• Self-suspension introduces an additional scheduling point: The OS removes the self-suspended task

from the ready queue and places it in the blocked queue.

The OS then dispatches the next eligible task.

203

Page 204: Rajib Mall Chapters 1,2,3

03/08/10 204

Scheduling Points• Scheduling points need to be

redefined:Task completion, Task arrival, Self-suspension events.

• For simplicity let us assume :A task undergoes at most a single self suspension.

204

Page 205: Rajib Mall Chapters 1,2,3

03/08/10 205

Self Suspension• bti:

Delay that task Ti incurs due its own self suspension and that of all higher priority tasks.

• bi: Worst case self suspension time of task Ti

205

1

1),min(

i

kkkii bebbt

Page 206: Rajib Mall Chapters 1,2,3

03/08/10 206

Revised RMA Condition• Lehoczky’s criterion needs to be

modified to consider self suspension:

• If a task undergoes multiple self suspensions:Then expression needs to be

changed. 206

ik

k

ii

kii pe

p

pbte

*

1

1

Page 207: Rajib Mall Chapters 1,2,3

03/08/10 207

Exercise• Consider the following set of

periodic real-time tasks: T1 = (e1=10 ms, p1=50 ms)

T2 = (e2=25 ms, p2=150 ms)

T3 = (e3=50 ms, p3=200 ms) self-suspension times: b1= 3 ms,

b2=3 ms and b3= 5 ms.

• Determine whether the tasks are schedulable using RMA.

207

Page 208: Rajib Mall Chapters 1,2,3

03/08/10 208

Answer• The tasks are already ordered

according to their priorities.

• Applying the revised RMA condition, For T1:

• T1 will meet its deadline

For T2:

• T2 will meet its deadline 208

ms 500310

ms 15050

150*10)33(25

Page 209: Rajib Mall Chapters 1,2,3

03/08/10 209

Answer For task T3,

• T3 also would meet its first deadline

209

ms 200150

200*25

50

200*10)633(50

Page 210: Rajib Mall Chapters 1,2,3

03/08/10 210

Self Suspension with Context Switch

• In a fixed priority preemptable system: Each task suffers at most two context

switches.

• Self suspension introduces: At most two other context switches.

• With context switch overhead and single self suspension: Execution time of a task Ti increases at

most to (ei + 4*c) 210

Page 211: Rajib Mall Chapters 1,2,3

03/08/10 211

Handling Critical Tasks With Long Periods

•What if task criticalities turn out to be different from task priorities?

•Simply raising the priority of a critical task:•Will make the RMA schedulability check

results inapplicable.

•A solution proposed by Sha and Raj Kumar’s period transformation (1989).

Page 212: Rajib Mall Chapters 1,2,3

03/08/10 212

Period Transformation Technique• A critical task is logically divided into many small

subtasks.

• Let Ti be a critical task that is split into k subtasks:• Each one has execution time ei/k and deadline di/k.

• This is done virtually at a conceptual level:• Rather than making any changes physically to the task itself.

Page 213: Rajib Mall Chapters 1,2,3

03/08/10 213

Period Transformation: Example

• Consider 2 tasks:• T1: e1=5, p1=d1=20 msec

• T2: e2=8, p1=d1=30 msec

• Assume that T2 is a critical task:• Should not miss deadline even under transient

underload.

• T2a: e2a=4, p2=d2=15msec

• Period transformation can also be used to improve the schedulability of a task set.

Bonus problem: What is the impact on schedulability?

Page 214: Rajib Mall Chapters 1,2,3

03/08/10 214214

Few Other Issues in Using RMA

(Lecture 12)

Dr. RAJIB MALLProfessor

Department Of Computer Science & Engineering

IIT Kharagpur.

Page 215: Rajib Mall Chapters 1,2,3

03/08/10 215

Handling Aperiodic and Sporadic Tasks• It is difficult to assign high priority values

to sporadic tasks:•A burst of sporadic task arrivals could

overload the system:• Cause many tasks to miss deadlines.

• Low priorities can be accorded:• But some sporadic tasks might be critical.

• The aperiodic server technique may be used.

Page 216: Rajib Mall Chapters 1,2,3

03/08/10 216

Sporadic Tasks

•Two kinds of sporadic tasks:

•High priority: Emergency events

•Non-critical: Background jobs (logging)

•Background jobs can be deferred during

transient overload:

• Tolerate long response time anyway.

•High priority tasks:

•Handled by converting them to periodic

tasks.

Page 217: Rajib Mall Chapters 1,2,3

03/08/10 217

Aperiodic Server•Selects aperiodic and sporadic tasks at

appropriate times after they arise:

• Passes them to RMA scheduler.

•Server deposits a ticket at start:

•Replenished at the expiration of a certain

replenishment period.

• If a ticket is available, the task is transferred

to RMA scheduler.

Page 218: Rajib Mall Chapters 1,2,3

03/08/10 218

Differable and Sporadic Servers

• Based on ticket creation policy -- there are two kinds of

aperiodic servers:• Differable

• Sporadic

• Sporadic server results in higher schedulable utilization:• Lends itself more easily to analysis.

• However, it is more complex to implement.

Page 219: Rajib Mall Chapters 1,2,3

03/08/10 219

Differable Server•Tickets are replenished at regular

intervals:

• Independent of actual usage.

•When no task is sent over a duration:

• Tickets are accumulated.

•Deviates from RMA strict periodic

execution model:

• Leads to conservative system design

and low processor utilization.

Page 220: Rajib Mall Chapters 1,2,3

03/08/10 220

Sporadic Server•Ticket replenishment time:•Depends on exact ticket usage time.

•As soon as a ticket is used:•The system sets a timer

•When timer goes off, any used tickets is replaced.

Page 221: Rajib Mall Chapters 1,2,3

03/08/10 221

Sporadic Servercont…

•Guarantees a minimum separation

between two instances of a task.

•Helps to consider a sporadic task as a

periodic task for:

•Schedulability analysis

•Priority assignment

Page 222: Rajib Mall Chapters 1,2,3

03/08/10 222222

Insufficient Number of Priorities

• All RTOS have a restricted number of priority levels (why?).When the number of tasks is

more than the number of available priority values, the schedulability of the system decreases.

Page 223: Rajib Mall Chapters 1,2,3

03/08/10 223223

Priority Grid• Assigning priorities using

a priority grid:

Uniform scheme

Arithmetic scheme

Geometric scheme

Logarithmic scheme

Page 224: Rajib Mall Chapters 1,2,3

03/08/10 224

Uniform Scheme•If there are N tasks and n priority

levels, then:

•n/N number of task are assigned to

each level.

•Rest of the tasks are distributed among

the lower priority levels.

Page 225: Rajib Mall Chapters 1,2,3

03/08/10 225

Example

•Consider 6 tasks and 4 priority levels.

•T1=(5,10), T2=(10,20), T3=(20,40), T4=(30,60), T5=(40,80); T6=(50,100)

•Priority 1: T1

•Priority 2: T2

•Priority 3: T3,T4

•Priority 4: T5,T6

Page 226: Rajib Mall Chapters 1,2,3

03/08/10 226

Arithmetic Scheme•Consider N tasks:•N=r+2r+3r+4r+....nr

• r tasks having the shortest periods are assigned to the highest priority level,

•2r tasks are assigned the next highest priority level, and so on.

Page 227: Rajib Mall Chapters 1,2,3

03/08/10 227

Geometric Scheme•Consider N tasks:

•N=r+r2+r3+r4+.... rn

• r tasks having the shortest periods are assigned to the highest priority level,

•r2 tasks are assigned the next highest priority level, and so on.

Page 228: Rajib Mall Chapters 1,2,3

03/08/10 228

Logarithmic Scheme•Experiments show that the logarithmic

grid works the best.

•Basic idea:

• Shorter period tasks should be allotted

distinct priority levels as much as

possible.

•Assume: r = (pmax/pmin) 1/n

Page 229: Rajib Mall Chapters 1,2,3

03/08/10 229

Logarithmic Scheme•Consider N number of tasks:•N=r+r2+r3+r4+.... rn

• r tasks having the shortest periods are assigned to the highest priority level,

•r2 tasks are assigned the next highest priority level, and so on.

Page 230: Rajib Mall Chapters 1,2,3

03/08/10 230230

Insufficient Number of Priorities: Example

• r=(Pmax/Pmin)1/nprio

• Pmin=1msec, Pmax=100,000 and nprio=32

• R=1.43

• The gridlines become 1.00, 1.43, 2.04, ...

Page 231: Rajib Mall Chapters 1,2,3

03/08/10 231

Dealing With Task Jitter

•Task jitter:

•Magnitude of variation in arrival or

completion times of a task.

•Certain applications require jitter be

minimized as much as possible.

•Arrival time jitter:

•Latest arrival - Earliest arrival

Page 232: Rajib Mall Chapters 1,2,3

03/08/10 232

Approach 1: To Deal With Task Jitter

•Applicable in situations where:

•Set of tasks is highly schedulable

•One or two tasks have high jitter

requirements.

•Assign these tasks high priorities

Page 233: Rajib Mall Chapters 1,2,3

03/08/10 233

Approach 2: To Deal With Task Jitter

• Assume a task set is barely schedulable.

• Each task with jitter constraints is split into two: • One which computes the output,

• One which passes the output on.

• Set the second task's priority to very high values:• Its period is the same as that of the first task.

Page 234: Rajib Mall Chapters 1,2,3

03/08/10 234

Quiz• Which of the following is an /(are)

important concern(s) of real-timeoperating systems:

1. Average response time

2. Average throughput

3. Worst case execution time

4. Best case execution time

Page 235: Rajib Mall Chapters 1,2,3

03/08/10 235

Identify True or False• Cyclic scheduler is more proficient compared table-driven

schedulers.

• Unlike table-driven schedulers,

cyclic schedulers do not require to store a precomputed

schedule.

• In a non-preemptive event-driven

task scheduler, scheduling decisions are made only at the

arrival and completion of tasks.

Page 236: Rajib Mall Chapters 1,2,3

03/08/10 236

Identify True or False• For scheduling a set of soft real-time tasks on a

uniprocessor:• RMA is a better scheduling algorithm compared

to time-sliced round-robinalgorithm.

•A set of periodic real-time tasks are being scheduled on a uniprocessor using RMA scheduling: • Assume they have similar arrival time jitter .• All tasks would show similar completion time

jitter.

Page 237: Rajib Mall Chapters 1,2,3

03/08/10 237

Practice Problem• Identify a constraint on the task set

which will make RMA as proficient as EDF.

• In any implementation of EDF:• The scheduler needs to frequently

examine the ready queue of the tasks at regular intervals to determine which task should start running next.

Page 238: Rajib Mall Chapters 1,2,3

03/08/10 238238

Resource Sharing Among Real-Time

Tasks(Lecture 13)

Dr. RAJIB MALLProfessor

Department Of Computer Science & Engineering

IIT Kharagpur.

Page 239: Rajib Mall Chapters 1,2,3

03/08/10 239239

Introduction• So far, the only resource that

we considered is CPU.

CPU is serially reusable

Can be used by one task at a time

The task can be preempted at any time without affecting correctness.

Page 240: Rajib Mall Chapters 1,2,3

03/08/10 240

Critical Section

•Tasks in reality need to share many

types of resources:

• Files, data structures, devices.

• These are nonpreemptable resources

•A piece of code in which a shared

nonpreemptable resource is accessed:

• Called a critical section in the operating

systems literature.

Page 241: Rajib Mall Chapters 1,2,3

03/08/10 241241

Critical Section Execution• Traditional operating system

solution to execute critical sections: Semaphores.

• However, in real-time systems this solution does not work well ---results in: Priority inversion

Unbounded priority inversion

Page 242: Rajib Mall Chapters 1,2,3

03/08/10 242242

Priority Inversion• A task instance executing a

critical section:

Cannot be preempted.

• Consqeuence: A higher priority task keeps waiting:

While the lower priority task progresses with its computations.

Page 243: Rajib Mall Chapters 1,2,3

03/08/10 243243

Priority Inversion• When a resource needs to be

shared in the exclusive mode.

A task may be blocked by a lower priority task which is already holding the resource.

Page 244: Rajib Mall Chapters 1,2,3

03/08/10 244244

Unbounded Priority Inversion

• Consider the following situation:A low priority task is holding a

resource.

A high priority task is waiting

An intermediate priority task which does not need resource preempts the low priority task.

Page 245: Rajib Mall Chapters 1,2,3

03/08/10 245

Unbounded Priority Inversion Example

T6

T5

T4

T3

T2

T1

CPU Usage Time

Lock CR

Lock CR

Unlock CR

Page 246: Rajib Mall Chapters 1,2,3

03/08/10 246246

Unbounded Priority Inversion

• Number of priority inversions suffered by a high priority task :Can be too many causing it to miss

its deadline.

• Most celebrated example:Mars path finder

Page 247: Rajib Mall Chapters 1,2,3

03/08/10 247

Mars Pathfinder•Landed on the Mars surface on July 4th,

1997. • Bounced onto the Martian surface

surrounded by airbags.

•Deployed the Sojourner rover.

•Gathered and transmitted voluminous data back to Earth:• Included the panoramic pictures now available

on the Web.

Page 248: Rajib Mall Chapters 1,2,3
Page 249: Rajib Mall Chapters 1,2,3

03/08/10 249

Mars Pathfinder

Page 250: Rajib Mall Chapters 1,2,3

03/08/10 250

Page 251: Rajib Mall Chapters 1,2,3

03/08/10 251

Mars Pathfinder Bug•Pathfinder began experiencing total system resets:•Each resulting in loss of data.

•The newspapers reported these failures using terms such as:•Software glitches

•The computer was trying to do too many things at once, etc.

Page 252: Rajib Mall Chapters 1,2,3

03/08/10 252

Debugging Mars Pathfinder

•The real-time kernel used was VxWorks

(Wind River Systems Ltd.)

•RMA scheduling of threads was used

•Pathfinder contained:

•A information bus -- a shared memory

•Used for passing information between

different spacecraft components.

Page 253: Rajib Mall Chapters 1,2,3

03/08/10 253

Debugging Mars Pathfinder

•VxWorks can be run in trace mode:

• Interesting system events: context switches,

uses of synchronization objects, and

interrupts are recorded.

• JPL engineers spent hours running exact

spacecraft replica in lab:

•Replicated the precise conditions under

which the reset occurred.

Page 254: Rajib Mall Chapters 1,2,3

03/08/10 254

Debugging Mars Pathfinder• VxWorks mutex object:

• Accepts a boolean parameter indicating whether priority inheritance should be performed.

• It was clear to the JPL engineers:• Turning ON priority inheritance would prevent the resets.• Initialization parameters were stored in global variables. • A short C program was uploaded to the spacecraft.

Page 255: Rajib Mall Chapters 1,2,3

03/08/10 255

Solution for Simple Priority Inversion

• Longest duration for which a simple priority inversion can occur:Bounded by the duration for which a lower priority task needs the resource in exclusive mode.

.255

Page 256: Rajib Mall Chapters 1,2,3

03/08/10 256

Solution to Simple Priority Inversion•A simple priority inversion can be

tolerated:

•Limit the time for which a task

executes its critical section.

•A simple priority inversion can be

limited to tolerable levels by careful

programming.

Page 257: Rajib Mall Chapters 1,2,3

03/08/10 257257257

Unbounded Priority Inversion• Consider the following situation:

A low priority task is holding a resource:• A high priority task is waiting.

An intermediate priority task which does not need resource:• Preempts the low priority task.

There can be an unlimited number of such intermediate tasks.

Page 258: Rajib Mall Chapters 1,2,3

03/08/10 258

Unbounded Priority Inversion

258

TH

TI1 TI2 TI3

TL

R

Page 259: Rajib Mall Chapters 1,2,3

03/08/10 259259259

Unbounded Priority Inversion

• Number of priority inversions suffered by a high priority task can be unbounded:Can cause the task to miss its

deadline.

In the worst case:•The high priority task might have to wait indefinitely.

Page 260: Rajib Mall Chapters 1,2,3

03/08/10 260260

Priority Inheritance Protocol Sha and Rajkumar

• The main idea behind this scheme:

• A task in critical section cannot be preempted:It should be allowed to complete as early as possible.

Page 261: Rajib Mall Chapters 1,2,3

03/08/10 261

Priority Inheritance Protocol

•How do you make a task complete as early as possible?•Raise its priority, so that low priority tasks

are not able to preempt it.

•By how much should its priority be raised?•Make its priority as much as that of the task

it is blocking.

Page 262: Rajib Mall Chapters 1,2,3

03/08/10 262262

Priority Inheritance Protocol Sha and Rajkumar

• When a resource is busy:Requests to lock the resource are queued in FIFO order.

Then apply the inheritance clause after a higher priority task blocks. TH

TL

R

Page 263: Rajib Mall Chapters 1,2,3

03/08/10 263263

Inheritance Clause

• The priority of the task in the critical section:Raised to equal the highest priority task in the queue.

TH

TL

R

Page 264: Rajib Mall Chapters 1,2,3

03/08/10 264

Priority Inheritance Protocol (PIP)Sha and Rajkumar

• As soon as the task releases the resource, It gets back its original priority value

if it is holding no other critical resource.

• In case it is holding other critical resources: It inherits priority of the highest

priority task waiting for resource.264

Page 265: Rajib Mall Chapters 1,2,3

03/08/10 265

PIP: Pseudocodeif the required resource is free,

then grant itif the required resource is being held by a higher

priority task, then wait for the resource

if the required resource is held by a lower priority task, then

{• Wait for the resource• The low priority task holding the resource

acquires the priority of highest priority task waiting for the resource.

} 265

Page 266: Rajib Mall Chapters 1,2,3

03/08/10 266

Understanding PIP

•How does PIP prevent unbounded

priority inversions?

•The priority of low priority task holding

resource is raised to that of the waiting

high priority task:

• Intermediate priority tasks can not

preempt it.

Page 267: Rajib Mall Chapters 1,2,3

03/08/10 267

Working of PIP

267

pri(Ti)=5 pri(Ti)=5 pri(Ti)=10 pri(Ti)=5

pri(Tj)=10 pri(Tj)=10

Ti

CRTiTi

CR

Ti

CR

Tj Tj Tj

CR

pri(Tj)=10

Instance 1 Instance 2 Instance 3 Instance 4

Page 268: Rajib Mall Chapters 1,2,3

03/08/10 268268268

Shortcomings of the Basic Priority Inheritance Scheme

• PIP suffers from two important drawbacks: Deadlocks

Chain blocking

• PIP is susceptible to chain blocking:

Also does nothing to prevent deadlocks

Page 269: Rajib Mall Chapters 1,2,3

03/08/10 269269269

Deadlocks• Consider two tasks T1 and T2 accessing

critical resources CR1 and CR2.

• Assume:

T1 has a higher priority than T2

T2 starts running first

• T1: Lock R1, Lock R2, Unlock R2, Unlock R1

• T2: Lock R2, Lock R1, Unlock R1, Unlock R2

Page 270: Rajib Mall Chapters 1,2,3

03/08/10 270

Chain Blocking• A task needing to use a set of

resources is said to undergochain blocking:If each time it needs a resource, it undergoes priority inversion.

• Example:Assume a high-priority task T1needs several resources 270

Page 271: Rajib Mall Chapters 1,2,3

03/08/10 271

Chain Blocking Example

271

T2

CR1

CR2

T1

Waiting for CR1

T1

T2 CR2

Waiting for CR2

CR1

T2 CR2

T1 CR1

T2 executing T1 executing T2 executing

T1 blocked T2 blocked T1 blocked

Snapshot 1 Snapshot 2 Snapshot 3

Page 272: Rajib Mall Chapters 1,2,3

03/08/10 272

Practice Questions

•What do you understand by priority

inversion?

•(T/F) When several tasks share a set of

critical resources,

• Is it possible to avoid priority inversion

altogether by using a suitable task

scheduling algorithm?

Page 273: Rajib Mall Chapters 1,2,3

03/08/10 273

Practice Question

•When priority inheritance is used:

•What do you understand by inheritance-

related inversion?

•When a set of real-time tasks share certain

critical resources using the priority

inheritance protocol:

• The highest priority task does not suffer any

inversions. (T/F)

Page 274: Rajib Mall Chapters 1,2,3

03/08/10 274

Practice Question

•When priority inheritance scheme is used, a task needing a resource undergoes priority inversions due to:•A higher priority task holding the resource•A lower priority task holding the resource•An equal priority task holding the resource• Either a higher or a lower priority task

holding the resource

Page 275: Rajib Mall Chapters 1,2,3

03/08/10 275

Practice Question

• Using semaphores of traditional operating systems,

what is the maximum duration for which a task may

undergo priority inversion:

1. Longest duration for which a higher priority task uses a

shared resource.

2. Longest duration for which a lower priority task uses a

shared resource.

3. Sum of the durations for which different lower priority

tasks may use the shared resource.

4. Greater than the longest duration for which a lower

priority task uses a shared resource

Page 276: Rajib Mall Chapters 1,2,3

03/08/10 276276

Highest Locker and Priority Ceiling Protocols

(Lecture 14)

Dr. RAJIB MALLProfessor

Department Of Computer Science & Engineering

IIT Kharagpur.

Page 277: Rajib Mall Chapters 1,2,3

03/08/10 277277

Highest Locker Protocol

• During the design of a system A ceiling priority value is assigned to all

resources.

The ceiling priority is equal to the highest priority of all tasks needing to use that resource.

• When a task acquires a resource: Its priority value is raised to the

ceiling priority of that resource.

Page 278: Rajib Mall Chapters 1,2,3

03/08/10 278278278

Highest Locker Protocol (HLP)

• Addresses the shortcomings of PIP: However, introduces new complications.

Addressed by Priority Ceiling Protocol(PCP).

Easier to first understand working of HLP and then PCP.

• During the design of a system: A ceiling priority value is assigned to all

critical resources.

The ceiling priority is equal to the highest priority of all tasks using that resource.

Page 279: Rajib Mall Chapters 1,2,3

03/08/10 279

Ceiling Priority of a Resource

•When a task acquires a resource:• Its priority value is raised to the ceiling

priority of that resource.

T1 T2 T3

RCeil(R) =

max-prio(T1,T2,T3)

Page 280: Rajib Mall Chapters 1,2,3

03/08/10 280

Example

T1 T2 T3

R Ceil(R) =

max-prio(T1,T2,T3) =2

prio(T1)=5

prio(T2)=2

prio(T3)=8

Page 281: Rajib Mall Chapters 1,2,3

03/08/10 281

Highest Locker Protocol (HLP)

• If higher priority values indicate higher priority (e.g., Windows):

• If higher priority values indicate lower priority (e.g., Unix):

281

}) |)(max({)( ijji RneedsTTpriRCeil

}) |)(min({)( ijji RneedsTTpriRCeil

Page 282: Rajib Mall Chapters 1,2,3

03/08/10 282

Highest Locker Protocol (HLP)

• As soon as a task acquires a resource R: Its priority is raised to Ceil(R)Helps eliminate the problems of:

•Unbounded priority inversions,•Deadlock, and •Chain blocking.

• However, introduces inheritance blocking. 282

Page 283: Rajib Mall Chapters 1,2,3

03/08/10 283

Example

T1 T2 T3

R

Ceil(R) =

max-prio(T1,T2,T3) =2

T1

RT5T4

T2

Page 284: Rajib Mall Chapters 1,2,3

03/08/10 284

Highest Locker Protocol (HLP)• Theorem:

When HLP is used for resource sharing:• Once a task gets any one of the resource

required by it, it is not blocked any further.

• Corollary 1: Under HLP, before a task is granted one

resource:• All the resources required by it must be free.

• Corollary 2: A task can not undergo chain blocking in

HLP. 284

Page 285: Rajib Mall Chapters 1,2,3

03/08/10 285285285

Highest Locker Protocol

• Avoids unbounded priority inversion

• Prevents deadlock (Corollary 1) T1: lock R1, Lock R2, Unlock R2, Unlock R1

T2: lock R2, Lock R1, Unlock R1, Unlock R2

• Prevents unbounded priority inversion.

Page 286: Rajib Mall Chapters 1,2,3

03/08/10 286286286

Shortcomings of HLP• Inheritance blocking occurs:

When the priority value of a low priority task holding a resource is raised to a high value.

Intermediate priority tasks not needing the resource:•Cannot execute and undergo priority inversion.

Page 287: Rajib Mall Chapters 1,2,3

03/08/10 287

Inheritance Inversion Example

T1 T2 T3

R

Ceil(R) =

max-prio(T1,T2,T3) =2

T1

RT5T3

Page 288: Rajib Mall Chapters 1,2,3

03/08/10 288

Shortcomings of HLP• Due to the problem of inheritance-

related priority inversion.: HLP is rarely used in real applications.

This may lead to several intermediate priority tasks to miss their deadlines.

• Priority Ceiling Protocol: Extension of PIP and HLP to

overcome their drawbacks.

Can you list the drawbacks?288

Page 289: Rajib Mall Chapters 1,2,3

03/08/10 289289

Priority Ceiling Protocol• Each resource is assigned a ceiling

priority: Like in HLP

• An operating system variable denoting highest ceiling of all locked semaphores is maintained.We will call it Current System

Ceiling (CSC).

Page 290: Rajib Mall Chapters 1,2,3

03/08/10 290

Priority Ceiling Protocol (PCP)

• Difference between PIP and PCP:

PIP is a greedy approach

• Whenever a request for a resource is made, the resource is promptly allocated if it is free.

PCP is not a greedy approach

• A resource may not be allocated to a requesting task even if it is free.

290

Page 291: Rajib Mall Chapters 1,2,3

03/08/10 291

PCP: CSC• At any instant of time,

CSC = max({Ceil(CRi)|CRi is currently in use})

At system start,• CSC is initialized to zero.

• Resource sharing among tasks in PCP is regulated by two rules:Resource Grant Rule.Resource Request Rule. 291

Page 292: Rajib Mall Chapters 1,2,3

03/08/10 292

PCP: Resource Grant Rule

• Resource grant rule has two clauses:Resource request clause

•Applied when a task request a resource.

Inheritance clause•Applied when a task is made to wait for a resource.

292

Page 293: Rajib Mall Chapters 1,2,3

03/08/10 293293

PCP: Resource Request Clause

• Unless a task holds a resource that set the current system ceiling:It can lock a resource only if its priority is greater than CSC.

Page 294: Rajib Mall Chapters 1,2,3

03/08/10 294294294

PCP: Resource Request Clause

If a task Ti is holding a resource CRj whose ceiling priority equals CSC, Then:

The task is granted access to the resource.

Otherwise, Ti is not granted CRj, unless pri(Ti) > CSC.

Page 295: Rajib Mall Chapters 1,2,3

03/08/10 295295295

PCP: Resource Request Clause

• In both (a) and (b):If Ti is granted access to the resource CRj, and if CSC < Ceil(CRj):•Then CSC is set to Ceil(CRj).

Page 296: Rajib Mall Chapters 1,2,3

03/08/10 296296296

PCP: Inheritance Clause• If a task is prevented from

locking a resource:The task holding the resource inherits the priority of the blocked task:•If the priority of the task holding the resource is lower than that of the blocked task.

Page 297: Rajib Mall Chapters 1,2,3

03/08/10 297

Features of PCP• In PCP, a task when granted a

resource: It does not immediately acquire the

ceiling priority of the resource.

On acquiring a resource, the priority of the task does not change.

• The priority of a task changes: Only when the inheritance clause is

applied. 297

Page 298: Rajib Mall Chapters 1,2,3

03/08/10 298298298

PCP: Analysis• Prevents deadlocks.

• Prevents chain blocking.

• Prevents unbounded priority inversion.

• Limits inheritance-related inversion.

Page 299: Rajib Mall Chapters 1,2,3

03/08/10 299

PCP: Resource Release Rule• If a task Ti releases a critical

resource CRj that it was holding and if Ceil(CRj) is equal to CSC, Then, CSC = max({Ceil(CRk)|CRk is any

resource remaining in use}).

Else, CSC remains unchanged.

• The priority of Ti is also updated: Reverts to its original priority, or

Reverts to the highest priority of all tasks waiting for any resource which Tiis still holding 299

Page 300: Rajib Mall Chapters 1,2,3

03/08/10 300

Example

Ceil(R) =

max-prio(T1,T2,T3) =10

T1 T2 T3

R

T4

CR1

Page 301: Rajib Mall Chapters 1,2,3

03/08/10 301

Working of PCP

301

pri(T2)=2 pri(T2)=2 pri(T2)=5 pri(T2)=5

pri(Tj)=5 pri(T3)=3

T2

CRT1T1

CR

T1

CR

T3 T3 T3

CR

pri(Tj)=5

Instance 1 Instance 2 Instance 3 Instance 4

CSC=10 CSC=10

T4

CR1CSC=10

Page 302: Rajib Mall Chapters 1,2,3

03/08/10 302302302

Types of Priority Inversions in PCP

• Direct inversion

• Inheritance-related inversion

• Avoidance-related inversion

Page 303: Rajib Mall Chapters 1,2,3

03/08/10 303303303

Direct Inversion• A lower priority task is

holding the resource CR:Higher priority task waits for the resource.

TH

TL

CR

Page 304: Rajib Mall Chapters 1,2,3

03/08/10 304

Identify The Direct Inversions

T1 T2 T3

R1

T4 T5 T6

R3

R2

Assume tasks have been sorted in order of priority

Page 305: Rajib Mall Chapters 1,2,3

03/08/10 305305

Inheritance-related Inversion

• When a low priority task is holding a resource and a high priority task is waiting for it: The priority of the low priority task is

raised.

An intermediate priority task not needing that resource:• Undergoes inheritance-related inversion.

Page 306: Rajib Mall Chapters 1,2,3

03/08/10 306

Inheritance-Related Inversion

306

TLCR

TH

TI

Page 307: Rajib Mall Chapters 1,2,3

03/08/10 307

Identify The Inheritance-Related Inversions

T1 T2 T3

R1

T4 T5 T6

R3

R2

Assume tasks have been sorted in order of priority

Page 308: Rajib Mall Chapters 1,2,3

03/08/10 308308

Analysis of PCP(Lecture 15)

Dr. RAJIB MALLProfessor

Department Of Computer Science & Engineering

IIT Kharagpur.

Page 309: Rajib Mall Chapters 1,2,3

03/08/10 309309309

Avoidance-Related Inversion

• Consider that a low priority task is holding a resource: CSC is made equal to the ceiling of the

resource being held.

A higher priority task, whose priority is lower than the CSC, needs a resource currently not in use:• Undergoes avoidance-related inversion

• Due to the resource grant rule

• Also called priority ceiling-related or deadlock-avoidance inversion.

Page 310: Rajib Mall Chapters 1,2,3

03/08/10 310

Avoidance-Related Inversion

310

TL

CR1

THCeil(CR1) = 10

CSC=10

pri(TH) = 8

CR2

Page 311: Rajib Mall Chapters 1,2,3

03/08/10 311

Identify The Avoidance-Related Inversions

T1 T2 T3

R1

T4 T5 T6

R3

R2

Assume tasks have been sorted in order of priority

Page 312: Rajib Mall Chapters 1,2,3

03/08/10 312

Avoidance-Related Inversion

• Theorem:Tasks are single-blocking under

PCP.• Once a task acquires a resource, it cannot undergo any inversion.

Corollary 1:• Under PCP a task can undergo at most one inversion during its execution.

312

Page 313: Rajib Mall Chapters 1,2,3

03/08/10 313

Why is PCP Deadlock Free?

•Deadlocks occur only when •Different tasks hold parts of each other's

required resources.

• They request for the resources being held by each other.

•But under PCP, when one task is executing with some resource, •Any other can not hold a required

resource.

Page 314: Rajib Mall Chapters 1,2,3

03/08/10 314

How is Unbounded Priority Inversion Avoided?

•A task suffers unbounded priority inversion, when • It is waiting for a lower priority task to

release a resources required by it.

• In the mean while intermediate priority tasks preempt the low priority task from CPU usage.

Page 315: Rajib Mall Chapters 1,2,3

03/08/10 315

How is Unbounded Priority Inversion Avoided?

•Whenever a high priority task waits for a resource held by a low priority task, •The lower priority task inherits the

priority of high priority task.

• Intermediate priority tasks can not preempt the low priority task from CPU usage.

Page 316: Rajib Mall Chapters 1,2,3

03/08/10 316

How is Chain Blocking Avoided?

•Already we have proved:•Resource sharing among tasks under PCP is single blocking. •This would give the clue how chain blocking is avoided.

Page 317: Rajib Mall Chapters 1,2,3

03/08/10 317

Inversion Analysis

T1 T2 T3

R1

T4 T5 T6

R3R2

Direct Blocking

T5

T6

8T4

1T3

8T2

82T1

T6T5T4T3T2T1

Page 318: Rajib Mall Chapters 1,2,3

03/08/10 318

8T5

T6

T4

T3

8T2

T1

T6T5T4T3T2T1

Inheritance Inversion

T5

T6

8T4

1T3

T2

T1

T6T5T4T3T2T1

Avoidance Inversion

Page 319: Rajib Mall Chapters 1,2,3

03/08/10 319

Analysis of Inversions

•Each inversion table is an upper triangular matrix:•Why?•A task does not suffer any inversions due to higher priority task.

Page 320: Rajib Mall Chapters 1,2,3

03/08/10 320

Maximum Inversion for a Task

•A task can suffer at best one of direct, inheritance, or avoidance-related inversion.

•Therefore the maximum inversion that a task can suffer is:• The maximum of the entry of the

corresponding row of the inversion table.

Page 321: Rajib Mall Chapters 1,2,3

03/08/10 321321

Handling Task Dependencies

(Lecture 16)

Dr. RAJIB MALLProfessor

Department Of Computer Science & Engineering

IIT Kharagpur.

Page 322: Rajib Mall Chapters 1,2,3

03/08/10 322

Plan of This Lecture

•We shall first complete:•Our incomplete discussions on resource sharing.

•We shall then discuss how scheduling algorithms can be extended:•To handle task dependencies.

Page 323: Rajib Mall Chapters 1,2,3

03/08/10 323

Priority Inversions Under PCP

•PCP reduces priority inversions:

•As compared to HLP.

•Higher priority tasks may still suffer from three main types of inversions:•Direct inversions

•Inheritance-related inversions

•Avoidance-related inversions

Recall

Page 324: Rajib Mall Chapters 1,2,3

03/08/10 324

Inversion Analysis Exercise 1

T1 T2 T3

R1

T4 T5 T6

R3

R2

Assume tasks have been sorted in increasing order of priority.

Page 325: Rajib Mall Chapters 1,2,3

03/08/10 325

Inversion Analysis Exercise 2

T1 T2 T3

R1

T4 T5 T6

R3

R2

Assume tasks have been sorted in increasing order of priority

Page 326: Rajib Mall Chapters 1,2,3

03/08/10 326326

Liu and Lehoczky Condition Under Resource Sharing

• Let bi denote: The longest time for which a task Ti can undergo

priority inversions due to resource sharing.

Ti will meet its first deadline if,

where pi is the period of task Ti and

iji

jj

iii pe

p

peb

)*(

1

1

npppp ...321

Page 327: Rajib Mall Chapters 1,2,3

03/08/10 327327

PCP for Dynamic Priority Systems

• The priority ceiling values: Need to change dynamically with time.

A solution: Each time the priority of a task changes:• Update the priority ceiling of each resource and the current system ceiling.

However, this would incur unacceptablyhigh processing overhead.

Page 328: Rajib Mall Chapters 1,2,3

03/08/10 328

Comparison of Resource Sharing Protocols

• PIP:Simplest --- requires minimal

support from the OS.Effectively overcomes the

unbounded priority inversion problem.

However, tasks may suffer from chain blockings and deadlocks.

328

Page 329: Rajib Mall Chapters 1,2,3

03/08/10 329

Comparison of Resource Sharing Protocols

• HLP:Requires moderate support from

the OS.Solves the chain blocking and

deadlock problems. Intermediate priority tasks:

•May suffer from inheritance-related inversions.

329

Page 330: Rajib Mall Chapters 1,2,3

03/08/10 330

Comparison of Resource Sharing Protocols

• PCP:Overcomes shortcomings of PIP.

• Free from deadlocks and chain blocking.

Low chances of inheritance-related inversions:

Priority of a task on acquiring a resource does not change :• Until a higher priority task requests the resource. 330

Page 331: Rajib Mall Chapters 1,2,3

03/08/10 331

Quiz•When a set of real-time tasks share

certain critical resources using priority ceiling protocol (PCP):• The highest priority task does not suffer any

inversions.

•Under PCP, a task not requiring any resource:•May still undergo priority inversion for some

duration.

Page 332: Rajib Mall Chapters 1,2,3

03/08/10 332

Quiz•Construct an example to justify:

•Why PCP is not a greedy algorithm?

•Why PIP is a greedy algorithm?

•Why under dynamic priority protocols:

•Supporting resource sharing among real-

time tasks is difficult.

Page 333: Rajib Mall Chapters 1,2,3

03/08/10 333

Handling Task Dependencies

• So far, our discussions assumed no dependencies exist among tasks:

However, in practical situations this is far from the truth.

Existing scheduling techniques need to be suitably modified.

333

Page 334: Rajib Mall Chapters 1,2,3

03/08/10 334334334

Table-Driven Algorithm• Arrange the tasks in increasing order of

their deadlines• Do

Scan the list from the rightmost end

Find a yet to be scheduled tasks whose all successors have been scheduled. • Schedule it as late as possible.

• While there are tasks yet to be scheduled

• Move all the tasks forward as much as possible

Page 335: Rajib Mall Chapters 1,2,3

03/08/10 335

Example• Determine a feasible schedule for

real-time a task set {T1, T2, …T5}

• T1= (2, 8)

• T2= (5 , 25)

• T3= (6 , 24)

• T4= (10, 50)

• T5 =(7, 48)335

Page 336: Rajib Mall Chapters 1,2,3

03/08/10 336

Precedence Relationships

336

T1

T2

T5

T3

T4

Page 337: Rajib Mall Chapters 1,2,3

03/08/10 337

SolutionStep 1: Arrangement of tasks in ascending order

of task deadlines:T1 T3 T2 T5 T4

Step 2: Schedule tasks as late as possible without violating precedence constraints:

0 6 8 14 20 25 33 40 50

Step 3: Move tasks as early as possible without altering the schedule

0 2 8 13 20 30

T1 T3 T2 T5 T4

T1 T3 T2 T5 T4

Page 338: Rajib Mall Chapters 1,2,3

03/08/10 338

Extending EDF and RMA-based Schedulers

• Precedence constraints can be handled with the following modifications to the basic algorithm: Do not enable a task until all its

predecessors complete their execution.

Check the tasks waiting to be enabled (on account of its predecessors completing their executions) after every task completes.

338

Page 339: Rajib Mall Chapters 1,2,3

03/08/10 339

RMA Scheduling of Tasks with Precedence Relations

Scheduler{T1, T2}

Conventional task set

T1 T2 Modify task parameters Scheduler

Task set with precedence constraints

Page 340: Rajib Mall Chapters 1,2,3

03/08/10 340

Modifying Task Parameters for RMA

•Rj* ≥ Max (Rj, Ri*) where Ri* is the modified ready time of the task Ti

•Priority Prioi ≥ Prioj

Page 341: Rajib Mall Chapters 1,2,3

03/08/10 341

Modifying Ready Times for RMA: Example

T1

1T2

2

T3

3

T4

1

T5

5

2006550T5

70

40

25

15

10

di

20055T6

20010T4

20030T3

20026T2

20010T1

pieiΦiTask

Task Set

T6

5

Page 342: Rajib Mall Chapters 1,2,3

03/08/10 342

Modifying the Ready times for RMA

T1

1

T2

2

T3

2

T4

1

T5

3

R1 = 0 R2 = 6

R3 = 0R4 = 0

R5 = 0

R3’ = max(R1, R3)

R3’ = 0

R4’ = max(R1, R2,R4)

R4’ = 6

R5’ = max(R3’, R4’,R5)

R5’ = 6

2006556T5

70

40

25

15

10

di

20055T6

20016T4

20030T3

20026T2

20010T1

pieiΦiTask

Initial Task Parameters

T6

4

R6’ = 5

Page 343: Rajib Mall Chapters 1,2,3

03/08/10 343

Modifying Ready Times for RMA: Example

T1

1T2

2

T3

3

T4

1

T5

3

200

200

200

200

200

200

pi

56556T5

70

40

25

15

10

di

655T6

416T4

330T3

226T2

110T1

Priority

eiΦiTask

Initial Task Parameters

T6

4