Top Banner
Copyright © 2012 Embedded Systems Committee Real Real - - Time OS Time OS (RTOS) (RTOS)
66
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: Rtos

Copyright © 2012 Embedded Systems Committee

RealReal--Time OSTime OS(RTOS)(RTOS)

Page 2: Rtos

Copyright © 2012 Embedded Systems Committee

Agenda

• Basic Definitions

• Introduction to RTOS

• Scheduling Algorithms

• Reentrancy

• Shared Resources

• RTOS APIs

Page 3: Rtos

Copyright © 2012 Embedded Systems Committee

Agenda

• Basic Definitions

• Introduction to RTOS

• Scheduling Algorithms

• Reentrancy

• Shared Resources

• RTOS APIs

Page 4: Rtos

Copyright © 2012 Embedded Systems Committee

Basic Definitions

• What are the Real-Time systems?– “Those systems in which the correctness of system

depends not only on the logical result of the computation, but also on the time at which the results are produced”

Page 5: Rtos

Copyright © 2012 Embedded Systems Committee

Basic Definitions

• Specifications of a Real-Time system include both:– Logical: Produces correct outputs.

– Temporal: Produces outputs at the right time.

Page 6: Rtos

Copyright © 2012 Embedded Systems Committee

Basic Definitions

• Types of Real-Time requirements are:– Hard: Failure to meet constraint is fatal.

– Soft: Late completion degrades software quality.

Page 7: Rtos

Copyright © 2012 Embedded Systems Committee

Basic Definitions

• Misconceptions:– “Real-Time computing is equivalent to fast

computing”

– Truth is:“Real-Time computing is equivalent to predictable computing”

Page 8: Rtos

Copyright © 2012 Embedded Systems Committee

Basic Definitions

• Misconceptions:– “Real-Time programming assembly coding”

– Truth is:“It is better to automate (as much as possible) Real-Time system design, instead of relying on a clever hand-crafted code”

Page 9: Rtos

Copyright © 2012 Embedded Systems Committee

Basic Definitions

• Misconceptions:– “Real-Time means performance engineering”

– Truth is:“In Real-Time computing, timeliness is always more important than performance.

Page 10: Rtos

Copyright © 2012 Embedded Systems Committee

Basic Definitions

• Misconceptions:– RTOS introduce considerable amount of overhead

on CPU

– Truth is:An RTOS typically only require between 1% to 4% of a CPU time.

Page 11: Rtos

Copyright © 2012 Embedded Systems Committee

Basic Definitions

Embedded SystemsEmbedded Systems

Real-Time Systems

Real-Time Embedded Systems

Page 12: Rtos

Copyright © 2012 Embedded Systems Committee

Agenda

• Basic Definitions

• Introduction to RTOS

• Scheduling Algorithms

• Reentrancy

• Shared Resources

• RTOS APIs

Page 13: Rtos

Copyright © 2012 Embedded Systems Committee

Introduction to RTOS

Page 14: Rtos

Copyright © 2012 Embedded Systems Committee

Introduction to RTOS

• Basic Services provided by OS:– Task Management.

– Intertask Communication & synchronization.

– Timers.

– Device I/O Supervision.

– Dynamic Memory Allocation.

Page 15: Rtos

Copyright © 2012 Embedded Systems Committee

Introduction to RTOS

• What are tasks?- A task─ an independent process.

- No task can call another task. unlike the normal C function which can call another function.

void task(void){

/*Some Initialization Code*/for(;;){

/*Task Code*/}

}

Page 16: Rtos

Copyright © 2012 Embedded Systems Committee

Introduction to RTOS

• What is multitasking?

Page 17: Rtos

Copyright © 2012 Embedded Systems Committee

Introduction to RTOS

• What is context?

RAM

ROMPC

SP

CPU Registers

Page 18: Rtos

Copyright © 2012 Embedded Systems Committee

RAM

Task1 Stack

Introduction to RTOS

• What is context?

ROM

Task2 Stack

Task1 Control Block

Task2 Control Block

Task 1CPU Registers

Task 2CPU Registers

Page 19: Rtos

Copyright © 2012 Embedded Systems Committee

Introduction to RTOS

• What is context?

Page 20: Rtos

Copyright © 2012 Embedded Systems Committee

Introduction to RTOS

• Task Life Cycle:

Page 21: Rtos

Copyright © 2012 Embedded Systems Committee

Introduction to RTOS

• What is the difference between RTOS & GPOS?

RTOS

GPOS

Number of Tasks That Can Be Scheduled

Task Switching Time

Page 22: Rtos

Copyright © 2012 Embedded Systems Committee

Introduction to RTOS

• Characteristics of an RTOS?– Reliability

– Predictability

– Performance

– Compactness

– Scalability

Page 23: Rtos

Copyright © 2012 Embedded Systems Committee

Agenda

• Basic Definitions

• Introduction to RTOS

• Scheduling Algorithms

• Reentrancy

• Shared Resources

• RTOS APIs

Page 24: Rtos

Copyright © 2012 Embedded Systems Committee

Scheduling Algorithms

• What is a scheduler?– It is part of the kernel which decides which task can

run when.

– There are many algorithms for scheduling & they can be categorized into two main categories.

Page 25: Rtos

Copyright © 2012 Embedded Systems Committee

Scheduling Algorithms

• Non-preemptive (Suppose it is priority based):

TimeTask1

Page 26: Rtos

Copyright © 2012 Embedded Systems Committee

Scheduling Algorithms

• Non-preemptive:

TimeTask1

ISR

Page 27: Rtos

Copyright © 2012 Embedded Systems Committee

Scheduling Algorithms

• Non-preemptive:

TimeTask1

ISRTask2

is Ready

Page 28: Rtos

Copyright © 2012 Embedded Systems Committee

Scheduling Algorithms

• Non-preemptive:

TimeTask1

ISR

Task1

Task2 is

Ready

Page 29: Rtos

Copyright © 2012 Embedded Systems Committee

Scheduling Algorithms

• Non-preemptive:

TimeTask1

ISR

Task1

Task2Although task2 is higher in priority than task1, task 1 gets to finish before task2 gets to start.

Task2 is

Ready

Page 30: Rtos

Copyright © 2012 Embedded Systems Committee

Scheduling Algorithms

• Preemptive (Suppose it is priority based):

TimeTask1

Page 31: Rtos

Copyright © 2012 Embedded Systems Committee

Scheduling Algorithms

• Preemptive:

TimeTask1

ISR

Page 32: Rtos

Copyright © 2012 Embedded Systems Committee

Scheduling Algorithms

• Preemptive:

TimeTask1

ISR

Page 33: Rtos

Copyright © 2012 Embedded Systems Committee

Scheduling Algorithms

• Preemptive:

TimeTask1

ISR

Task2

Page 34: Rtos

Copyright © 2012 Embedded Systems Committee

Scheduling Algorithms

• Preemptive:

TimeTask1

ISR

Task1

Task2

Page 35: Rtos

Copyright © 2012 Embedded Systems Committee

Agenda

• Basic Definitions

• Introduction to RTOS

• Scheduling Algorithms

• Reentrancy

• Shared Resources

• RTOS APIs

Page 36: Rtos

Copyright © 2012 Embedded Systems Committee

Reentrancy

char x;void foo1(void){

x++;}

char x;foo1:

mov R1,x;

add R1,1;

mov x,R1;

C Assembly

/*Some Code*/foo1();

/*Some Code*/

Task1 Task2/*Some Code*/

foo1();

/*Some Code*/

Page 37: Rtos

Copyright © 2012 Embedded Systems Committee

Reentrancyx = 1

Task 1: R1=0 Task 2: R1=0

Page 38: Rtos

Copyright © 2012 Embedded Systems Committee

Reentrancyx = 1

Task 1: R1=1 Task 2: R1=0mov R1,x;

Page 39: Rtos

Copyright © 2012 Embedded Systems Committee

Reentrancyx = 1

Task 1: R1=2 Task 2: R1=0mov R1,x;

add R1,1;

Page 40: Rtos

Copyright © 2012 Embedded Systems Committee

Reentrancyx = 2

Task 1: R1=2 Task 2: R1=0mov R1,x;

add R1,1;

mov x,R1;

Page 41: Rtos

Copyright © 2012 Embedded Systems Committee

Reentrancyx = 2

Task 1: R1=2 Task 2: R1=0mov R1,x;

add R1,1;

mov x,R1;

Context SwitchingTask2 is ready

Page 42: Rtos

Copyright © 2012 Embedded Systems Committee

Reentrancyx = 2

Task 1: R1=2 Task 2: R1=2mov R1,x;mov R1,x;

add R1,1;

mov x,R1;

Page 43: Rtos

Copyright © 2012 Embedded Systems Committee

Reentrancyx = 2

Task 1: R1=2 Task 2: R1=3mov R1,x;

add R1,1;

mov x,R1;

mov R1,x;

add R1,1;

Page 44: Rtos

Copyright © 2012 Embedded Systems Committee

Reentrancyx = 3

Task 1: R1=2 Task 2: R1=3mov R1,x;

add R1,1;

mov x,R1;

mov R1,x;

add R1,1;

mov x,R1;

Page 45: Rtos

Copyright © 2012 Embedded Systems Committee

Reentrancy (Another Scenario)x = 1

Task 1: R1=0 Task 2: R1=0

Page 46: Rtos

Copyright © 2012 Embedded Systems Committee

Reentrancyx = 1

Task 1: R1=1 Task 2: R1=1mov R1,x;

Page 47: Rtos

Copyright © 2012 Embedded Systems Committee

Reentrancyx = 1

Task 1: R1=1 Task 2: R1=1mov R1,x;

Context SwitchingTask2 is ready

Page 48: Rtos

Copyright © 2012 Embedded Systems Committee

Reentrancyx = 1

Task 1: R1=1 Task 2: R1=1mov R1,x; mov R1,x;

Page 49: Rtos

Copyright © 2012 Embedded Systems Committee

Reentrancyx = 1

Task 1: R1=1 Task 2: R1=2mov R1,x; mov R1,x;

add R1,1;

Page 50: Rtos

Copyright © 2012 Embedded Systems Committee

Reentrancyx = 2

Task 1: R1=1 Task 2: R1=2mov R1,x; mov R1,x;

add R1,1;

mov x,R1

Page 51: Rtos

Copyright © 2012 Embedded Systems Committee

Reentrancyx = 2

Task 1: R1=1 Task 2: R1=2mov R1,x; mov R1,x;

add R1,1;

mov x,R1;

Context SwitchingTask2 is back to waiting

Page 52: Rtos

Copyright © 2012 Embedded Systems Committee

Reentrancyx = 2

Task 1: R1=2 Task 2: R1=2mov R1,x;

add R1,1;

mov R1,x;

add R1,1;

mov x,R1;

Page 53: Rtos

Copyright © 2012 Embedded Systems Committee

Reentrancyx = 2

Task 1: R1=2 Task 2: R1=2mov R1,x;

add R1,1;

mov x,R1;

mov R1,x;

add R1,1;

mov x,R1;

Page 54: Rtos

Copyright © 2012 Embedded Systems Committee

Reentrancy

• When to doubt your function reentrancy?– When your function accesses a global variable while

this variable is accessed in:• ISR

• Another task

• Hardware module.

Page 55: Rtos

Copyright © 2012 Embedded Systems Committee

Reentrancy

• How to make a non-reentrant function reentrant?– Either using critical section or any task

synchronization services provided by the OS.

Page 56: Rtos

Copyright © 2012 Embedded Systems Committee

Reentrancy

• Critical Section:– Context Switching always happens after an interrupt.

– If I disabled interrupts during the time I don’t want the schedule nor any ISR interrupts the running task then this part of code is reentrant.

char x;void foo1(void){

DI;x++;EI;

}

Page 57: Rtos

Copyright © 2012 Embedded Systems Committee

Agenda

• Basic Definitions

• Introduction to RTOS

• Scheduling Algorithms

• Reentrancy

• Shared Resources

• RTOS APIs

Page 58: Rtos

Copyright © 2012 Embedded Systems Committee

Shared Resources

• Tasks always race each other for resources.

• Resources could be many thing like HW modules, memory & even CPU time.

• We have to control the tasks resources access to avoid data corruption or basically any undesirable behavior.

• Semaphores are just one methods of controlling resources access.

Page 59: Rtos

Copyright © 2012 Embedded Systems Committee

• Semaphore vs. mutex

Shared Resources

Page 60: Rtos

Copyright © 2012 Embedded Systems Committee

IntertaskCommunication

• global variable

RAM

XTask 1 write X

Task 2 read X

Page 61: Rtos

Copyright © 2012 Embedded Systems Committee

IntertaskCommunication

• Mailboxes- Any task can send a message to a mailbox and any task can receive a message from a mailbox

Page 62: Rtos

Copyright © 2012 Embedded Systems Committee

IntertaskCommunication

• Message Queues

Page 63: Rtos

Copyright © 2012 Embedded Systems Committee

Agenda

• Basic Definitions

• Introduction to RTOS

• Scheduling Algorithms

• Reentrancy

• Shared Resources

• RTOS APIs

Page 64: Rtos

Copyright © 2012 Embedded Systems Committee

RTOS APIs “uCOS-III”• OSTaskCreate(Task ptr, arg, prio, stack size)

• OSSemCreate(Sem ptr, counter)

• OSSemPost(Sem ptr)

• OSSemPend(Sem ptr,Timeout)

• OSTmrCreate(Tmr ptr, period, callback)

• OSTmrStart, OSTmrStop(Tmr ptr)

• OSMemCreate(mem ptr, block size, n blocks)

• OSMemPut(mem ptr, blk ptr)

• OSMemGet(mem ptr)

Page 65: Rtos

Copyright © 2012 Embedded Systems Committee

References

• uC/OS-II, Jean Labrosse

• Operating Systems: Design & Implementation, Andrew Tanenbaum

• Embedded.com

Page 66: Rtos

Copyright © 2012 Embedded Systems Committee

Website: www.escommittee.net

Contact Us: [email protected]

FB: Embedded Systems Committee