eln5622 embedded systems class 8 spring, 2003 aaron itskovich [email protected]

36
ELN5622 Embedded Systems Class 8 Spring, 2003 Aaron Itskovich [email protected]

Upload: lester-perkins

Post on 26-Dec-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: ELN5622 Embedded Systems Class 8 Spring, 2003 Aaron Itskovich itskovich@rogers.com

ELN5622Embedded Systems

Class 8

Spring, 2003

Aaron [email protected]

Page 2: ELN5622 Embedded Systems Class 8 Spring, 2003 Aaron Itskovich itskovich@rogers.com

Overview

Operating Systems vs. Executives Real-Time Operating Systems Desirable Attributes Examples

Page 3: ELN5622 Embedded Systems Class 8 Spring, 2003 Aaron Itskovich itskovich@rogers.com

Operating Systems vs. Executives

Desktop Systems– General purpose– Deep functionality– Generous resources– Multiple I/O channels

Embedded Systems– Special purpose– Limited functionality– Limited resources– Single I/O channel

Page 4: ELN5622 Embedded Systems Class 8 Spring, 2003 Aaron Itskovich itskovich@rogers.com

Operating Systems vs. Executives (cont.)

So, why do we need an OS?– Well, we don't, always

But...– The natural decomposition of a system might

involve multiple tasks (threads)

– Abstracting-out hardware dependencies allows us to run on multiple or one processors without or with small changes in application software

– Fast embedded processors are getting cheap

Page 5: ELN5622 Embedded Systems Class 8 Spring, 2003 Aaron Itskovich itskovich@rogers.com

Operating Systems vs. Executives (cont.)

Page 6: ELN5622 Embedded Systems Class 8 Spring, 2003 Aaron Itskovich itskovich@rogers.com

Real-Time Operating Systems

A Real-Time OS (RTOS) means:– Upper-bound on response time to external events

• e.g., interrupts, timer

– Fast context switch time Hard real-time

– Missed deadline causes system malfunction (e.g., plane crashes, assembly line stops)

Soft real-time– Missed deadline causes system degradation (Not so

bad for example might drop a few frames of video)

Page 7: ELN5622 Embedded Systems Class 8 Spring, 2003 Aaron Itskovich itskovich@rogers.com

Real-Time Operating Systems (cont.)

Preemptable kernel– Longest path through the kernel is short– Interrupts are not disabled for a long time

POSIX– Portable Operating System Interface (Unix)– Standard API for real-time Oss

Process– Unit of resource allocation

Thread– Unit of scheduling

Page 8: ELN5622 Embedded Systems Class 8 Spring, 2003 Aaron Itskovich itskovich@rogers.com

Context Switches

Pseudo-Parallelism– Threads run concurrently

Steps:– Choose a thread to run– Save current context (PC, registers, MMU info)– Load new context– Run

A context switch between sibling threads is cheaper than for non-sibling threads

Page 9: ELN5622 Embedded Systems Class 8 Spring, 2003 Aaron Itskovich itskovich@rogers.com

Scheduling

Meeting deadlines requires preemption– FIFO scheduling– Round-robin scheduling– Preemptive-priority scheduling

Running

ReadyWaiting

1

23

4

1: Preempted

2: Scheduled

3: Wait (e.g., I/O)

4: Signal (e.g., I/O complete)

Page 10: ELN5622 Embedded Systems Class 8 Spring, 2003 Aaron Itskovich itskovich@rogers.com

FIFO Scheduling

Thread runs until it voluntarily gives up CPU– Cooperative scheduling– To do I/O

Page 11: ELN5622 Embedded Systems Class 8 Spring, 2003 Aaron Itskovich itskovich@rogers.com

FIFO Scheduling (cont.)

Process Burst Time

P0 7P1 15P2 6P3 3

P0 P1 P3

0 7 28 31

P2

22

Page 12: ELN5622 Embedded Systems Class 8 Spring, 2003 Aaron Itskovich itskovich@rogers.com

Round-Robin Scheduling

Each thread runs for a given timeslice CPU does a context switch after timeslice

expires Fair scheduling policy

Page 13: ELN5622 Embedded Systems Class 8 Spring, 2003 Aaron Itskovich itskovich@rogers.com

Round-Robin Scheduling (cont.)

Process Arrival Time Burst Time

P0 0 7P1 1 15P2 2 6P3 3 3

P0 P2 P1

0 15 31

P3

12

P1

84 18

P0

22 24

P2 P1

*Assume timeslice = 4

Page 14: ELN5622 Embedded Systems Class 8 Spring, 2003 Aaron Itskovich itskovich@rogers.com

Preemptive-Priority Scheduling

Each thread has a priority OS guarantees highest priority thread always

runs OS takes CPU from a running, lower-priority

thread

1

0

2

63

...

P1 P4

P3 P0

P5

P2

Pidle

Page 15: ELN5622 Embedded Systems Class 8 Spring, 2003 Aaron Itskovich itskovich@rogers.com

Preemptive-Priority Scheduling (cont.)

Process Arrival Time Burst Time Priority

P0 0 7 3P1 1 15 4P2 2 6 1P3 3 3 2

P0 P3 P1

0 16 31

P0

11

P2

82

Page 16: ELN5622 Embedded Systems Class 8 Spring, 2003 Aaron Itskovich itskovich@rogers.com

Measures of OS Performance

The main measures of OS performance– Size of OS footprint– Interrupt latency– Context switch time– Hardware supported– Memory protection support

Page 17: ELN5622 Embedded Systems Class 8 Spring, 2003 Aaron Itskovich itskovich@rogers.com

Measures of OS Performance (cont.)

Til

Tint

Tiret

Interrupt Latency

Interrupt Processing Time

Interrupt Termination Time

TilTint

Tiret

InterruptOccurs

InterruptHandler Runs

Interrupt HandlerFinishes

Interrupted ProcessContinues Execution

time

Interrupt service time

Page 18: ELN5622 Embedded Systems Class 8 Spring, 2003 Aaron Itskovich itskovich@rogers.com

Measures of OS Performance (cont.)

Context switch time– QNX Neutrino

Processor Speed (MHz) Context Switch (µsec)

7400 G4 PowerPC 460 0.6

R527X MIPS 166 2.3

AMD-K-1 555 0.6

SH-4 200 1.9

SA-1110 StrongARM 207 1.8

Page 19: ELN5622 Embedded Systems Class 8 Spring, 2003 Aaron Itskovich itskovich@rogers.com

Communicating processes

Despite process abstraction, different computations (processes) sometimes want to communicate.

Communication can increase the modularity and robustness of programs– (one process for each action, the application

can continue if one of the sub processes fails)

Page 20: ELN5622 Embedded Systems Class 8 Spring, 2003 Aaron Itskovich itskovich@rogers.com

Communicating processes (cont)

Shared memory as a way of communication between processes– processes all have access to a shared are of

memory where one process can change a vari-able that can be seen by all other processes.

– How to deal with racing conditions?

Process 1 Process 2

Sh

ared

Mem

ory

Page 21: ELN5622 Embedded Systems Class 8 Spring, 2003 Aaron Itskovich itskovich@rogers.com

Race conditions

An atomic action is an indivisible action - an action taken by a process than cannot be interrupted by a context switch. When accesses to a shared variable are not atomic, call it a race condition.

Page 22: ELN5622 Embedded Systems Class 8 Spring, 2003 Aaron Itskovich itskovich@rogers.com

Race condition example

shared int x =3;process_1 () { x =x +1; print x;}

process_2 () { x =x +1; print x;}Note that although x = x +1 looks like one

statement, it probably compiles into at least 2 and probably three machine instructions. A likely translation is:LOAD r1<-xADD r1+1->r1STORE r1->x

Page 23: ELN5622 Embedded Systems Class 8 Spring, 2003 Aaron Itskovich itskovich@rogers.com

Race condition example(cont)

If the program is controlling radiation therapy, maybe somebody's life at risk.

Process 1 Process 2 variable

4 5 5

5 4 5

4 4 4

Page 24: ELN5622 Embedded Systems Class 8 Spring, 2003 Aaron Itskovich itskovich@rogers.com

Critical section

Only code that is manipulating shared data needs to worry about them.

Sections of code that are accessing shared data are called critical sections or critical regions

Ensure that only one process is ever executing code in the critical section (Mutual exclusion – Mutex)

Page 25: ELN5622 Embedded Systems Class 8 Spring, 2003 Aaron Itskovich itskovich@rogers.com

Mutual exclusion Simple and Wrong

int flag;

void enter_region(int process) {

while ( flag == 1 );

flag = 1;

}

void leave_region(int process) {

flag = 0;

}

Page 26: ELN5622 Embedded Systems Class 8 Spring, 2003 Aaron Itskovich itskovich@rogers.com

Alteration

int turn;

void enter_region(int process) {

while ( turn != process ) ;

}

void leave_region(int process) {

int other = 1 - process;

turn = other;

}

Page 27: ELN5622 Embedded Systems Class 8 Spring, 2003 Aaron Itskovich itskovich@rogers.com

Dekker’s Algorithmint favored_process;int interested[2];

void enter_region(int process) {int other = 1-process;interested[process] = TRUE;while ( interested[other] ) {

if ( favored_process == other ) {interested[process] = FALSE;while ( favored_process == other );interested[process] = TRUE;

}}

}void leave_region(int process) {

int other = 1 - process;favored_process = other;interested[process] = FALSE;

}

Page 28: ELN5622 Embedded Systems Class 8 Spring, 2003 Aaron Itskovich itskovich@rogers.com

Hardware assistance

Disable interrupts– Breaks process isolation– Turning off interrupts for any period of time

is extremely hazardous. test-and-set instruction, which makes

testing a flag and resetting it’s value an atomic action.

Page 29: ELN5622 Embedded Systems Class 8 Spring, 2003 Aaron Itskovich itskovich@rogers.com

Resource contention and resource access control

Resources, Resource Access, and How Things Can Go Wrong:

The Mars Pathfinder Incident Resources, Critical Sections, Blocking Priority Inversion, Deadlocks Nonpreemptive Critical Sections Priority Inheritance Protocol Priority Ceiling

Page 30: ELN5622 Embedded Systems Class 8 Spring, 2003 Aaron Itskovich itskovich@rogers.com

Mars Pathfinder Incident

Landing on July 4, 1997 “experiences software glitches” Pathfinder experiences repeated Resets

after starting gathering of metrological data.

Resets generated by watchdog process. Timing overruns caused by priority

inversion.

Page 31: ELN5622 Embedded Systems Class 8 Spring, 2003 Aaron Itskovich itskovich@rogers.com

Priority inversion

Page 32: ELN5622 Embedded Systems Class 8 Spring, 2003 Aaron Itskovich itskovich@rogers.com

How to solve priority inversion

Priority inheritance – Priority Inheritance means that when a thread waits on

a mutex owned by a lower priority thread, the priority of the owner is increased to that of the waiter.

Ceiling protocol– Priority Ceiling means that while a thread owns the

mutex it runs at a priority higher than any other thread that may acquire the mutex

– A beneficial feature of the priority ceiling solution is that tasks can share resources simply by changing their priorities, thus eliminating the need for semaphores

Page 33: ELN5622 Embedded Systems Class 8 Spring, 2003 Aaron Itskovich itskovich@rogers.com

Deadlock

In case you are using more than one mutex you can get deadlock

Page 34: ELN5622 Embedded Systems Class 8 Spring, 2003 Aaron Itskovich itskovich@rogers.com

Mailboxes Known Memory Location with Key

– Post - write to the location– Pend - read from the location

Scheduler allows Post and Pend operations If no key in the mailbox, the task blocks Accept or Check operation - don’t block if no key Scheduler checks for keys and unblocks tasks Mailbox Interrupt may force this (no busy

waiting)

Page 36: ELN5622 Embedded Systems Class 8 Spring, 2003 Aaron Itskovich itskovich@rogers.com

Lab

Load the “echo” program to the evaluation board.