• 沒有找到結果。

Real-Time/Embedded Operating Systems & Resource Management

N/A
N/A
Protected

Academic year: 2022

Share "Real-Time/Embedded Operating Systems & Resource Management"

Copied!
23
0
0

加載中.... (立即查看全文)

全文

(1)

Real-Time/Embedded Operating Systems & Resource Management

Tei-Wei Kuo, Ph.D.

ktw@csie.ntu.edu.tw

Dept. of Computer Science &

Information Engineering National Taiwan University Taipei, Taiwan, ROC

Contents

ƒ Overview

ƒ A General Architecture of Real-Time/

Embedded Operating Systems

ƒ Scheduling Strategies & System Analysis

ƒ Process Synchronization over IPC

ƒ Summary

(2)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.

Overview

ƒ The Purposes of Operating Systems

ƒ Convenience

ƒ Efficiency

ƒ Characteristics of Many Real-Time/

Embedded Applications

ƒ More specific in their applications.

ƒ More drastic for their failures.

Overview

ƒ A Typical Control System Example

ƒ Rates -sensors & actuators, peripheral, control program

ƒ Phases - takeoff, cruise, and landing, etc.

sensors

actuators environment controlled

process

Task Executions

Clock

Display operator

(3)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.

Overview

ƒ Potential Timing Hazards:

Loop

…...

Sensor();

……..

computation……

……..

t = time();

SleepTime := ReadyTime + PERIOD - t;

ReadyTime = ReadyTime + PERIOD;

Sleep(SleepTime);

EndLoop;

Loop Size?

Time Elapsed Here?

Timer Granularity?

Real Sleep Time?

???Multiprogramming???

Overview

ƒ General Concerns:

ƒ Could I verify the performance of my system?

ƒ How to avoid timing hazards?

ƒ Is there any good way in scheduling processes or allocating resources?

Æ Understand your operating system and hardware, and use resources intelligently!

(4)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.

Contents

ƒ Overview

ƒ A General Architecture of Real-Time/

Embedded Operating Systems (RTOS’s)

ƒ Scheduling Strategies & System Analysis

ƒ Process Synchronization over IPC

ƒ Summary

A General Architecture of RTOS’s

ƒ Various Requirements

ƒ Predictability – Verifiability

ƒ Reliability – Strictness of Deadline Violations

ƒ Reconfigurability – System Size and Functionality

ƒ Efficiency of System Components – Time Granularity, Threads, and Resource

Management

ƒ Variable Models of Task Communication – Characteristics of Applications

(5)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.

A General Architecture of RTOS’s

ƒ Objectives in the Design of Many RTOS’s

ƒ Efficient Scheduling Mechanisms

ƒ Good Resource Management Policies

ƒ Predictable Performance

ƒ Common Functionality of Many RTOS’s

ƒ Task Management

ƒ Memory Management

ƒ Resource Control, including devices

ƒ Process Synchronization

A General Architecture

Bottom Half Top Half processes User

Space

OS

hardware

Timer expires to

• Expire the running process’s time quota

• Keep the accounting info for each process

System calls such as I/O requests which may cause the releasing CPU of a process!

Interrupts for Services

(6)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.

A General Architecture

ƒ 2-Step Interrupt Services

ƒ Immediate Interrupt Service

ƒ Interrupt priorities > process priorities

ƒ Time: Completion of higher priority ISR, context switch, disabling of certain interrupts, starting of the right ISR (urgent/low-level work, set events)

ƒ Scheduled Interrupt Service

ƒ Usually done by preemptable threads

ƒ Remark: Reducing of non-preemptable code, Priority Tracking/Inheritance (LynxOS), etc.

ISR

I

Interrupt/ISR Latency

SecheduledService

IST Latency

A General Architecture

ƒ Scheduler

ƒ A central part in the kernel

ƒ The scheduler is usually driven by a clock interrupt periodically, except when voluntary context switches occur – thread quantum?

ƒ Timer Resolution

ƒ Tick size vs Interrupt Frequency

ƒ 10ms? 1ms? 1us? 1ns?

ƒ Fine-Grained hardware clock

(7)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.

A General Architecture

ƒ Memory Management

ƒ No protection for many embedded systems

ƒ Memory-locking to avoid paging

ƒ Process Synchronization

ƒ Sources of Priority Inversion

ƒNonpreemptible code

ƒ Critical sections

ƒA limited number of priority levels, etc.

Contents

ƒ Overview

ƒ A General Architecture of Real-Time/

Embedded Operating Systems (RTOS’s)

ƒ Scheduling Strategies & System Analysis

ƒ Process Synchronization over IPC

ƒ Summary

(8)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.

Scheduling Strategies &

System Analysis

ƒ Why a process in my application does not meet its deadline?

ƒ Factors:

ƒ Impacts from the executions of higher- priority processes – preemption cost

ƒ Lengthy execution time of the process

ƒ Blocking time from lower-priority processes – priority inversion

Scheduling Strategies &

System Analysis

ƒ Possible Questions:

ƒ How do I assign priorities to processes?

ƒ How are my processes scheduled by the OS?

ƒ How long is the blocking time/non-

preemptable critical sections (from lower- priority processes or interrupts)?

Æ Understand your schedulers

ƒ Fixed-Priorities or Dynamic Priorities

ƒ Preemptive or Non-Preemptive Scheduling

(9)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.

Scheduling Strategies & System Analysis Scheduling Strategy

ƒ Major Components of a Scheduler

ƒ Priority Assignment Policy

ƒThe number of priority levels, e.g., 256?

ƒAging Effects?

ƒ Priority-Driven Scheduling Mechanism

ƒPriority Queue

ƒThread Quantum?

ƒPreemption Lock – Disabling of Preemption

ƒetc.

Rate Monotonic Scheduling Algorithm

ƒ Assumptions:

ƒ all periodic fixed-priority processes

ƒ relative deadline = period

ƒ independent process - no non-preemptable resources

ƒ Rate Monotonic (RM) Scheduling Algorithm

ƒ RM priority assignment: priority ~ 1/period.

ƒ preemptive priority-driven scheduling.

ƒ Example: T1 (p1=4, c1=2) and T2 (p2=5, c1=1)

T1 T2 T1 T2 Time

0 1 2 3 4 5 6 7 8

(10)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.

Rate Monotonic Scheduling Algorithm

ƒ Critical Instant 1

ƒ An instant at which a request of the process have the largest completion/response time.

ƒ An instance at which the process is requested simultaneously with requests of all higher priority processes

ƒ Usages

ƒ Worst-case analysis

ƒ Fully utilization of the processor power

ƒ Example: T1 (p1=4, c1=2) and T2 (p2=5, c1=1)

1 Liu and Layland, “Scheduling Algorithms for multiprogramming in a hard real-time Environment,” JACM, vol. 20, no. 1, January 1973, pp. 46-61.

T1 T2 T1 T2 Time

0 1 2 3 4 5 6 7 8

T2 T2

Rate Monotonic Scheduling Algorithm

ƒ Schedulability Test:

ƒ A sufficient but not necessary condition

ƒ Achievable utilization factorα

ƒ of a scheduling policy P -> any process set with total utilization factor no more than α is schedulable.

ƒ Given n processes, α =

ƒ Stability:

ƒ Let processes be sorted in RM order. The ith process is schedulable if

ƒ An optimal fixed priority scheduling algorithm

c p

i i

( )

n 21/n −1

( )

c pj i

j j

i= i

1

21/ 1

(11)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.

Schedulability Tests – A Sufficient Condition

Theorem 0 [Liu&Layland 73]: Aset of n periodic processes is schedulable if the total utilization factor of the process set is no larger than

Theorem 1 [Kuo, et al. 00]: Suppose that Ti-1 is schedulable. Let k be the number of roots in Ti.

If the total utilization factor of Tiis no larger than

then Tiis schedulable.

) 1 2

( 1nn

) 1 2

( 1kk

15 60

3

20

5

Schedulability Tests – Effects of Interrupts

Task Γ1 : C1 =20ms, P1 =100ms, U1=0.2 Task Γ2 : C2 =40ms, P2 =150ms, U2=0.267 Interrupt : Cint=60ms, Pint=200ms, Uint=0.3 Task Γ3 :C3 =20ms, P3 =350ms, U3=0.057

Γ1 Γ2

Γ3 Int

10 100 200 300

Exec with RM priority

Γ1 Γ2

Γ3 Int

10 100 200 300

Exec with an Interrupt priority

The last task was not affected!

(12)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.

Schedulability Tests – Effects of Priority Mapping

ƒ Ready Queue

Priority-decreasing

0-3 4-7 8-11

proc[i] proc[j]

proc[k]

allproc

zombproc

freeproc

proc[j]

proc[m]

proc[n]

exit() wait()

Let processes be sorted in RM order. The ith process is schedulable if

( )

(c ) /

p

c B

p i

j j

i i

i j

i + + i

=

1

1 21 1

Rate Monotonic Analysis – A Sufficient & Necessary Condition

ƒ Rate Monotonic Analysis (RMA) 2

ƒ Basic Idea:

Before time t after the critical instance of process τi, a high priority process τj may request amount of

computation time.

ƒ Formula:

ƒ A sufficient and necessary condition and many extensions...

2 Sha, “An Intorduction to Rate Monotonic Analysis,” tutorial notes, SEI, CMU, 1992

Time

c t

j p

j

t

deadline of τi

t pj

0

for some t in

{kp jj| =1,..., ;i k=1,..., pi/pj }

( ) i

i j

j j

i t d

p c t t

W

== 1

(13)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.

Rate Monotonic Analysis – A Sufficient & Necessary Condition

ƒ A RMA Example:

ƒ T1(20,100), T2(30,150), T3(80, 210), T4(100,400)

ƒ T1

ƒ c1 <= 100

ƒ T2

ƒ c1 + c2 <= 100 or

ƒ 2c1 + c2 <= 150

ƒ T3

ƒ c1 + c2 + c3 <= 100 or

ƒ 2c1 + c2 + c3 <= 150 or

ƒ 2c1 + 2c2 + c3 <= 200 or

ƒ 3c1 + 2c2 + c3 <= 210

ƒ T4

ƒ c1 + c2 + c3 + c4 <= 100 or

ƒ 2c1 + c2 + c3 + c4 <= 150 or

ƒ .... Time

W3(t)

50 100 150 200 130

150 170 190 210

Rate Monotonic Scheduling Algorithm

ƒ RM was chosen by

ƒ Space Station Freedom Project

ƒ FAA Advanced Automation System (AAS)

ƒ RM influenced

ƒ the specs of IEEE Futurebus+

ƒ RMA is widely used for off-line analysis of time-critical systems.

(14)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.

Earliest Deadline First Scheduling Algorithm

ƒ Assumptions (similar to RM):

ƒ all periodic dynamic-priority processes

ƒ relative deadline = period

ƒ independent process - no non-preemptable resources

ƒ Earliest Deadline First (EDF) Scheduling Algorithm:

ƒ EDF priority assignment: priority ~ absolute deadline.

i.e., arrival time t + relative deadline d.

ƒ preemptive priority-driven scheduling

ƒ Example: T1(c1=1, p1=2), T2(c2=2, p2=7)

T1 T2 Time

0 1 2 3 4 5 6 7 8

T1 T2 T1 T1 T2

Earliest Deadline First Scheduling Algorithm

ƒ Schedulability Test:

ƒ A sufficient and necessary condition

ƒ Any process set is schedulable by EDF iff

ƒ EDF is optimal for any independent process scheduling algorithms

ƒ However, its implementation has

considerable overheads on OS’s with a fixed- priority scheduler and is bad for (transiently) overloaded systems.

c p

j

j j

i=

1 1

(15)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.

Contents

ƒ Overview

ƒ A General Architecture of Real-Time/

Embedded Operating Systems (RTOS’s)

ƒ Scheduling Strategies & System Analysis

ƒ Process Synchronization over IPC

ƒ Summary

Synchronization & IPC

ƒ Why Inter-Process Communication (IPC)?

ƒ Exchanging of Data and Control Information!

ƒ Why Process Synchronization?

ƒ Protect critical sections!

ƒ Ensure the order of executions!

(16)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.

Synchronization & IPC

ƒ Common Facility for IPC?

ƒ Shared Memory

ƒ Message Transmission

ƒ Common Facility for Synchronization?

ƒ Semaphores

ƒ Signals

Synchronization & IPC

ƒ Shared Memory

ƒ Characteristics: High

bandwidth and low latency, but very primitive!

ƒ Needs: Ways to

synchronize its access to avoid racing conditions!

ƒPotential deadlocks/

livelocks/blocking problems

ƒe.g., semaphores, bakery algorithm, etc.

flag[i]=TRUE;

while flag[j]

;

flag[i]=FALSE;

key=TRUE;

for(swap(lock,key); key==TRUE; ) swap(lock,key) ;

lock=FALSE;

Example 1:

Example 2: (initially, lock=FALSE)

Deadlock?

Livelocks?

(17)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.

Synchronization & IPC

ƒ Message Transmission

ƒ Characteristics: Simple & clean

interface with various extensions: m:m communication, multi-machines

ƒ Needs: A priority-based message transmission/notification/processing mechanism

ƒe.g., message priority, non-blocking library functions, notification of msg arrivals, servicing order of msgs (with respect to other threads in the system), etc.

A B

OS

Synchronization & IPC

ƒ General Concerns:

ƒ How to enforce mutual exclusion?

ƒWe should not rely on OS scheduling to avoid race conditions!

ƒ How to process critical messages first?

ƒThere is a tight coupling between

message processing and OS scheduling!

ƒ After all, we want to manage the priority inversion problem!

ƒ How to let critical jobs be done with minimized interferences from less important jobs!

(18)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.

Synchronization & IPC

ƒ Solutions??

ƒ Synchronization Methods

ƒ Priority-Driven Resource Scheduling Support!

ƒ Popular Approaches

ƒ Semaphore-Based Synchronization?!

ƒSignals are too slow.

ƒ Priority Inheritance?!

ƒCeiling?

Synchronization & IPC – Signals?

ƒ The Design of Signal Mechanisms

ƒ Inform processes/threads of the occurrences of exceptions or events

ƒ Posting of a signal to a process

ƒ An appropriate signal is added to the set of pending signals for the process.

ƒ Delivering within the context of the receiver

if (sig = CURSIG(p)) postsig(sig)

ƒ Signal handlers: user-mode routines

A B

OS

(19)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.

Synchronization & IPC – Signals

ƒ Questions?

ƒ How fast can a signal be delivered?

ƒ Complicated actions done by OS

ƒ The length of a signal handler?

ƒ Signal handlers are executed prior to returning control to the user code.

ƒ Real-Time Support

ƒ Application-defined signals

ƒ Queuing of signals while being blocked.

ƒ Signals are delivered in the priority order.

ƒ etc

Synchronization & IPC – Can we manage the priority inversion problem?

ƒ What are the sources of priority inversion?

ƒ Synchronization and mutual exclusion

ƒ Nonpreemptable regions of code

ƒ FIFO of any other non-priority-based queues

ƒ Interrupts

ƒ A limited number of priorities

(20)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.

Synchronization & IPC – Can we manage the priority inversion problem?

ƒ Popular Synchronization Methods

ƒ Nonpreemptable Critical Sections

ƒ Highest Locker’s Priority

ƒ Priority Inheritance

ƒ Priority Ceiling

1. Nonpreemptible Critical Section Critical sections are executed at an

“infinitely” high priority!

Synchronization Protocols

τL τM

τH

Note that τH M have no intention to enter a critical section!

Time

(21)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.

Synchronization Protocols

2. Highest Locker’s Priority Protocol

Execute critical section at the priority of the highest-priority task that may lock the semaphore(/resource); higher-priority tasks may preempt the critical section.

τL τM τH τvH

Note that τvHis no longer blocked by τL

Time

3. Basic Inheritance Protocol (BIP) [Sha87]

Execute the critical section at the priority of the highest-priority task being blocked; higher-priority tasks may preempt the critical section.

τL S1

τM τH τvH

S1

S2

S2

S2

t

blocked blocked

Time

- Note that τM is no longer blocked until necessary.

- However, system may be deadlocked or have chained blocking!

Synchronization Protocols

S2 S1 & S2

(22)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.

4. Priority Ceiling Protocol [Sha87]

BIP + a “Priority Ceiling” rule about when to grant lock requests (see [Sha 87, 90] )

τL S1

τM τH τvH

S1

S2 S1

blocked Time

- No deadlock & chained blocking at the cost of reducing the concurrency level of the system.

- Blocked-at-most-once.

Synchronization Protocols

blocked

S1

Summary of Synchronization Methods

Yes Yes

Priority Ceiling

No Bounded Priority Inheritance

Yes Yes

Highest Locker’s Priority

Yes Yes

Non-preemptible Critical Section

Deadlock Avoidance Blocked

at Once

(23)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.

Contents

ƒ Overview

ƒ A General Architecture

ƒ Scheduling Strategies

ƒ Inter-Process Communication

ƒ Summary

Summary

ƒ Understand your operating systems and hardware!

ƒ There is no substitute for

intelligent resource allocation

methods!

參考文獻

相關文件

Tei-Wei Kuo, Embedded System and Wireless Networking Lab, National Taiwan University.. Real-Time

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2001.. Operating

If we recorded the monthly sodium in- take for each individual in a sample and his/her blood pressure, do individuals with higher sodium consumption also have higher blood

A Complete Example with equal sample size The analysis of variance indicates whether pop- ulation means are different by comparing the variability among sample means with

The natural structure for two vari- ables is often a rectangular array with columns corresponding to the categories of one vari- able and rows to categories of the second

Project 1.3 Use parametric bootstrap and nonparametric bootstrap to approximate the dis- tribution of median based on a data with sam- ple size 20 from a standard normal

All rights reserved.... All

* All rights reserved, Tei-Wei Kuo, National Taiwan University,