Top Banner
Embedded Systems Introduction to Real- Time Operating Systems C.-Z. Yang http://syslab.cse.yzu.edu.tw/~czyang Sept.-Dec. 2001
73

Embedded Systems Introduction to Real-Time Operating Systems

Jan 06, 2016

Download

Documents

gamba

Embedded Systems Introduction to Real-Time Operating Systems. C.-Z. Yang http://syslab.cse.yzu.edu.tw/~czyang Sept.-Dec. 2001. OS Architectures. A traditional UNIX system architecture. OS Architectures. A monolithic OS kernel. OS Architectures. A real-time kernel implementation. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Embedded  Systems Introduction to Real-Time Operating Systems

Embedded SystemsIntroduction to Real-Time

Operating Systems C.-Z. Yang

http://syslab.cse.yzu.edu.tw/~czyang

Sept.-Dec. 2001

Page 2: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

2

元智大學資訊工程系

OS Architectures

• A traditional UNIX system architecture

Page 3: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

3

元智大學資訊工程系

OS Architectures

• A monolithic OS kernel

Page 4: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

4

元智大學資訊工程系

OS Architectures

• A real-time kernel implementation

Page 5: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

5

元智大學資訊工程系

OS Architectures

• A micro-kernel RTOS

Page 6: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

6

元智大學資訊工程系

Case Study: Real Time or Real Linux? A Realistic Alternative

Paul N. Leroux

Technology Analyst

QNX Software Systems Ltd.

Page 7: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

7

元智大學資訊工程系

Using Linux as a Real-time OS

• For the embedded systems designer, Linux poses a dilemma.– Linux has a rich legacy of source code, and industry-stand

ard POSIX APIs.

– The standard Linux kernel can't deliver the "hard" real-time capabilities.

• Several innate problems– Process scheduling

– Preemptible kernel

Page 8: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

8

元智大學資訊工程系

Process Scheduling

• Preemptive priority– Rather than use a preemptive priority-based scheduler, as

an RTOS would, Linux implements a "fairness" policy so that every process gets a reasonable opportunity to execute.

– In fact, the OS will sometimes interrupt a high-priority process to give a lower-priority process a share of CPU time.

Page 9: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

9

元智大學資訊工程系

Preemption

• The standard Linux kernel isn't preemptible.– A high-priority process can never preempt a kernel call, bu

t must instead wait for the entire call to complete - even if the call was invoked by the lowest-priority process in the system.

Page 10: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

10

元智大學資訊工程系

Real Time Implemented Outside of Linux

• Running Linux as a task on top of a realtime kernel– Any tasks that require deterministic scheduling also run in

this preemptible kernel, but at a higher priority than Linux.

Page 11: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

11

元智大學資訊工程系

The Dual-kernel model

• System Operations– The realtime kernel always gets first dibs on hardware

interrupts.

– Otherwise, the kernel will pass the interrupt to Linux for processing.

• Advantages– All this work is invisible to applications running in the

Linux environment - except, of course, for the CPU cycles lost to the realtime kernel and its tasks.

– System development is very flexible.

Page 12: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

12

元智大學資訊工程系

Shortcomings of The Dual-kernel model

• Duplicated coding efforts– Tasks running in the realtime kernel can't make full use of

existing Linux system services.

• Fragile execution environment– Tasks running in the realtime kernel don't benefit from the

robust MMU-protected environment.

• Limited portability– With the dual-kernel approach, realtime tasks aren't Linux

processes at all, but threads and signal handlers written to a small subset of the POSIX API or, in some cases, a non-standard API.

Page 13: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

13

元智大學資訊工程系

Shortcomings of The Dual-kernel model

• No determinism for existing Linux applications and drivers– Because Linux processes don't run in the realtime kernel,

they don't gain any deterministic behavior.

• Limited design options– The APIs supported by the realtime kernel provide only a

subset of the services provided by standard POSIX and Linux APIs.

Page 14: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

14

元智大學資訊工程系

"Pure" Realtime Linux?

• It allows developers to use a standard Linux kernel and programming model.

• It helps address the low-latency requirements of reactive, event-driven systems.

• However, – such low-latency patches don't address the complexity of

most realtime environments.

– For example, modifications of the Linux driver and virtual file system (VFS) are needed.

Page 15: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

15

元智大學資訊工程系

Difficulties in a Pure Realtime Linux

• In any case, it's unclear which realtime modifications, will eventually be integrated into the standard Linux kernel.

• After all, most Linux-based systems rely on the kernel's current approach, which sacrifices predictability to achieve higher overall throughput.

Page 16: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

16

元智大學資訊工程系

The Best of Both Worlds

• The RTOS should be able to– allow Linux developers to keep their existing tools, source

code, and programming model

– maintain the key benefits, such as easier troubleshooting and OS customization, of Linux's open source model

– address the shortcomings of realtime Linux extensions

Page 17: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

17

元智大學資訊工程系

Several Problems

• How well can an RTOS support the Linux programming model?– The answer lies, to a great degree, in the POSIX APIs adop

ted by Linux.

– As a result, an RTOS can support the same POSIX APIs as Linux without adopting Linux's non-deterministic kernel.

• A need for a new lingua franca among embedded Linux developers.

Page 18: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

18

元智大學資訊工程系

QNX model

• A realtime, microkernel architecture

Page 19: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

19

元智大學資訊工程系

Embedded Linux: a new definition

• As a set of APIs, along with a test suite to measure conformance.

• As a result,– the RTOS inherently supports embedded Linux

applications, while simultaneously providing all the benefits of a true RTOS designed from the ground up for embedded systems.

Page 20: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

20

元智大學資訊工程系

More Benefits

• A tougher runtime model– This model provides an environment for realtime

applications that's inherently more robust than Linux - and certainly much tougher than the unprotected realtime kernels used in the dual-kernel approach.

• A unified environment– The realtime and non-realtime environments are one and

the same.

Page 21: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

21

元智大學資訊工程系

More Benefits

• Less duplicated effort– All drivers run in user space, so they can be developed

using standard source-level tools and techniques.

Page 22: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

22

元智大學資訊工程系

Background

Page 23: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

23

元智大學資訊工程系

An Embedded RTOS

• At boot-up time, your application usually gets control first, and it then starts the RTOS.

• Many RTOSs do not protect themselves as carefully from your application as do desktop OS.– For many embedded systems, it may not matter if the appli

cation takes the RTOS down with it: the whole system will have to be rebooted anyway.

Page 24: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

24

元智大學資訊工程系

An Embedded RTOS

• To save memory RTOSs typically include just the services that you need for your embedded system and no more.

• Most RTOSs allow you to configure them extensively before you link them to the application.

Page 25: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

25

元智大學資訊工程系

Several Purchase Options

• Run faster

• Use less memory

• Have a better API

• Have better debugging tools

• Support more processors

• Have more already-debugged network drivers

Page 26: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

26

元智大學資訊工程系

Tasks and Task States

Page 27: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

27

元智大學資訊工程系

Tasks

• The basic building block

• Three states

Page 28: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

28

元智大學資訊工程系

The Scheduler

• It keeps track of the state of each task and decides which one task should go into the running state.

• The schedulers in most RTOSs are entirely simpleminded about which task should get the processor:– They look at priorities you assign to the tasks.

• The scheduler assume that you knew what you were doing when you set the task priorities.

Page 29: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

29

元智大學資訊工程系

State Transitions

• A task will only block because it decides for itself that it has run out of things to do.

• While a task is blocked, it never gets the microprocessor.

• The shuffling of tasks between the ready and running states is entirely the work of the scheduler.

Page 30: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

30

元智大學資訊工程系

Some Common Questions

• How does the scheduler know when a task has become blocked or unblocked?– Through a collection of RTOS function calls.

• What happens if all the tasks are blocked?– The scheduler will spin in some tight loop somewhere

inside of the RTOS, waiting for something to happen.

Page 31: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

31

元智大學資訊工程系

Some Common Questions

• What if two tasks with the same priority are ready?– It depend upon which RTOS you use.

– At least one system solves this problem by making it illegal to have two tasks with the same priority.

– Some other RTOSs will time-slice between two such tasks.

– Some will run one of them until it blocks and then run the other.

Page 32: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

32

元智大學資訊工程系

Some Common Questions

• If one task is running and another, higher-priority task unblocks, does the task that is running get stopped and moved to the <ready> state right away?– A preemptive RTOS will stop a lower-priority task as

soon.

– A non-preemptive RTOS will only take the microprocessor away from the lower-priority task when that task blocks.

Page 33: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

33

元智大學資訊工程系

A Simple Example

• The classic situation

This task will be unblocked as soon as the user pushes a button.

Page 34: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

34

元智大學資訊工程系

A Simple Example

• The computational task

Page 35: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

35

元智大學資訊工程系

System Operations

• Transitions

Page 36: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

36

元智大學資訊工程系

Initialization Code

• Assigning the priorities

Page 37: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

37

元智大學資訊工程系

Features of Using an RTOS

• Two tasks can be written independently of one another, and the system will still respond well.

• The RTOS will make the response good whenever the user presses a button by turning the microprocessor over to the task that responds to the buttons immediately.

Page 38: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

38

元智大學資訊工程系

Tasks and Data

Page 39: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

39

元智大學資訊工程系

Context

• Each task has its own private context.– the register values,

– a program counter,

– a stack.

• All other data is shared among all of the tasks in the system.– Global

– static

– initialized

– ...

Page 40: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

40

元智大學資訊工程系

An Example

• A common data area

Page 41: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

41

元智大學資訊工程系

Sharing Data

• Two main functionsvRespondToButton vCalculateTankLevels

Page 42: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

42

元智大學資訊工程系

Shared-data problems

• Another example– Task2 interrupts Task1

Page 43: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

43

元智大學資訊工程系

A clearer examination

• The assembly code

Page 44: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

44

元智大學資訊工程系

A clearer examination

• The flow

Page 45: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

45

元智大學資訊工程系

Reentrancy

• Reentrant functions are – functions that can be called by more than one task and that

will always work correctly.

– Even if the RTOS switches from one task to another in the middle of executing the function.

Page 46: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

46

元智大學資訊工程系

Three Rules

• A reentrant function may not use variables in a nonatomic way unless they are stored on the stack of the task that called the function or are otherwise the private variables of that task.

• A reentrant function may not call any other functions that are not themselves reentrant.

• A reentrant function may not use the hardware in a nonatomic way.

Page 47: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

47

元智大學資訊工程系

A Review of C Variables

• Where will the variables in the following code be stored?

Page 48: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

48

元智大學資訊工程系

Applying the Reentrant Rules

• Is this example reentrant?

Page 49: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

49

元智大學資訊工程系

Gray Areas of Reentrancy

• A reentrant function may not use non-stack variables in a non-atomic way.

Is incrementing cErrors atomic?

Page 50: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

50

元智大學資訊工程系

A closer look

• A 8051 code • A 80x86 code

Page 51: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

51

元智大學資訊工程系

Semaphores and Shared Data

Page 52: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

52

元智大學資訊工程系

A Railroad Story

• The scenario

Page 53: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

53

元智大學資訊工程系

RTOS Semaphors

• take/release

• TakeSemaphore() and ReleaseSemaphore()

• A binary semaphore– Only one task can have the semaphore at a time.

Page 54: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

54

元智大學資訊工程系

A Typical Use

• The tank application

Page 55: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

55

元智大學資訊工程系

The Sequence of Events

• If the user presses a button while the levels task is still modifying the data and still has the semaphore,– The RTOS will switch to the “button task,” just as before,

moving the levels task to the ready state.

– When the button task tries to get the semaphore by calling TakeSemaphore it will block because the levels task already has the semaphore.

– The RTOS will then look around for another task to run.

– When the levels task releases the semaphore by calling ReleaseSemophore, the button task will no longer be blocked.

Page 56: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

56

元智大學資訊工程系

Execution Flow

• The flow

Page 57: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

57

元智大學資訊工程系

Execution Flow

• The flow

Page 58: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

58

元智大學資訊工程系

The Nuclear Reactor System

• Semaphore-related functions– OSSemPost(): release the semaphore

– OSSemPend(): take the semaphore

– OSSemCreate(): initialize the semaphore

• Related data structures– OS_EVENT: the data representing the semaphore

– WAIT_FOREVER: indicates that the task making the call is willing to wait forever

• Other functions– OSTimeDly(): block functions

Page 59: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

59

元智大學資訊工程系

The Code

• The data structures

Page 60: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

60

元智大學資訊工程系

The Code

• The main function

Page 61: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

61

元智大學資訊工程系

The Code

• Two tasks

A potential bug!

Page 62: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

62

元智大學資訊工程系

Reentrancy and Semaphores

• Now adding a semaphore to the previous code

Atomic now!

Page 63: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

63

元智大學資訊工程系

Multiple Semaphores

• Some RTOS allows you to have as many semaphores as you like.

• The advantages– In a system with only one semaphore, if the lower-priority

task takes the semaphore to change data, the higher-priority task might block waiting for the semaphore.

• How does the RTOS know which semaphore protects which data?– It doesn’t.– You must decide what shared data each of your semaphores

protects!

Page 64: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

64

元智大學資訊工程系

Semaphores as a Signaling Device

• Another common use of semaphores is as a simple way to communicate– from one task to another or

– from an interrupt routine to a task.

• For example,– printing task

– formatting task

Page 65: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

65

元智大學資訊工程系

The code

• The data structures

Page 66: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

66

元智大學資訊工程系

The functions

• Functions

Page 67: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

67

元智大學資訊工程系

The functions

• Functions

Page 68: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

68

元智大學資訊工程系

Semaphore Problems

• Do semaphores represent the solutions to all of our shared-data problems?– No!

• Semaphores should be also carefully used.

Page 69: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

69

元智大學資訊工程系

Semaphore Problems

• Forgetting to take the semaphore

• Forgetting to release the semaphore

• Taking the wrong semaphore

• Holding a semaphore for too long

• Causing a deadly embrace

Page 70: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

70

元智大學資訊工程系

An Example

• Priority inversion

Page 71: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

71

元智大學資訊工程系

Another Example

• Deadly embrace– Two tasks may all block.

Page 72: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

72

元智大學資訊工程系

Semaphore Variants

• Counting semaphores

• Resource semaphores

• Mutex semaphores

Page 73: Embedded  Systems Introduction to Real-Time Operating Systems

[email protected] Embedded Systems - Introduction to Real-Time Operating Systems

73

元智大學資訊工程系

Ways to Protect Shared Data

• Disabling interrupts

• Using semaphores

• Disabling task switch