Top Banner
Real Time Operating Systems Sanjiv Malik
90

Real Time Operating System Concepts

Jun 17, 2015

Download

Technology

sanjivmalik

This slideshow discusses the basics concepts of the RTOS
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: Real Time Operating System Concepts

Real Time Operating Systems

Sanjiv Malik

Page 2: Real Time Operating System Concepts

Topics

• Real Time Systems• Real Time Operating Systems & VxWorks• Application Development• Loading Applications• Testing Applications

Page 3: Real Time Operating System Concepts

Real Time Systems

• Real-time is the ability of the control system to respond to any external or internal events in a fast and deterministic way.

• We say that a system is deterministic if the response time is predictable.

Page 4: Real Time Operating System Concepts

Real Time Systems

• The lag time between the occurrence of an event and the response to that event is called latency

• Deterministic performance is key to Real-time performance. performance.

Page 5: Real Time Operating System Concepts

Real Time System

• High speed execution:– Fast response– Low overhead

• Deterministic operation:– A late answer is a wrong answer.

Page 6: Real Time Operating System Concepts

Real Time SystemsMemory

Mgmt

Device

Drivers

Network

Stack

Kernel

Page 7: Real Time Operating System Concepts

vxWorks

• What is vxWorks ?– vxWorks is a networked RTOS which

can also be used in distributed systems.– vxWorks topics

• Hardware Environment Requirements• Development tools• Testing

Page 8: Real Time Operating System Concepts

Hardware requirements

• vxWorks runs on range of platforms• MC680x0• MC683xx• Intel i960• Intel i386• R3000• SPARC based systems

Page 9: Real Time Operating System Concepts

What is a real time OS

• A real time OS is a operating system which will help implement any real time system

Page 10: Real Time Operating System Concepts

RTOS Requirements

• Multitasking• Intertask communications• Deterministic response• Fast Response• Low Interrupt Latency

Page 11: Real Time Operating System Concepts

Uni tasking

• Sample Application

Page 12: Real Time Operating System Concepts

Uni tasking• One task controlling all the components is a

loop.• arm ( )

{ for (;;)

{ if (shoulder needs moving)

moveShoulder( ) ; if (elbow needs moving)

moveElbow( ); if (wrist need moving)

moveWrist( ); . . . .

}}

Page 13: Real Time Operating System Concepts

Multitasking Approach

• Create a separate task to manipulate each joint:

joint ( ) {

for (;;){

wait; /* Until this joint needs moving */ moveJoint ( );

} }

Page 14: Real Time Operating System Concepts

Multitasking and Task Scheduling

• Task State Transition

Pending Ready Delayed

Suspended

taskInit()

Page 15: Real Time Operating System Concepts

Multitasking and Task Scheduling

Page 16: Real Time Operating System Concepts

Multitasking and Task Scheduling

• Manages tasks.• Transparently interleaves task execution,

creating the appearance of many programs executing simultaneously and independently.

Page 17: Real Time Operating System Concepts

Multitasking and Task Scheduling• Uses Task Control Blocks (TCBs) to keep track of

tasks.– One per task.– WIND_TCB data structure defined in taskLib.h– O.S. control information

• e.g. task priority, • delay timer,• I/O assignments for stdin, stdout, stderr

– CPU Context information• PC, SP, CPU registers, • FPU registers FPU registers

Page 18: Real Time Operating System Concepts

Multitasking and Task Scheduling

• Task Context.– Program thread (i.e) the task program counter– All CPU registers– Optionally floating point registers– Stack dynamic variables and functionals calls– I/O assignments for stdin, stdout and stderr– Delay timer– Timeslice timer– Kernel control structures– Signal handlers– Memory address space is not saved in the context

Page 19: Real Time Operating System Concepts

Multitasking and Task Scheduling

• To schedule a new task to run, the kernel must: :– Save context of old executing task into

associated TCB. – Restore context of next task to execute from

associated TCB.• Complete context switch must be very fast

Page 20: Real Time Operating System Concepts

Multitasking and Task Scheduling

• Task Scheduling– Pre-emptive priority based scheduling– CPU is always alloted to the “ready to run” highest

priority task– Each task has priority numbered between 0 and 255– It can be augmented with round robin scheduling.

Page 21: Real Time Operating System Concepts

Priority Scheduling• Work may have an inherent precedence. • Precedence must be observed when allocating

CPU.• Preemptive scheduler is based on priorities. • Highest priority task ready to run (not pended

or delayed) is allocated to the CPU

Page 22: Real Time Operating System Concepts

Priority Scheduling• Reschedule can occur anytime, due to:

– Kernel calls.– System clock tick

Page 23: Real Time Operating System Concepts

Priority based pre-emption

Task t1

Task t2

Task t3

Task t2

Task t1

TIME

t3 completes

t2 completes

t2 preempts t1

t3 preempts t2

•Priority

Page 24: Real Time Operating System Concepts

Round Robin Scheduling

Task t4

TIME

t4 completes

t4 preempts t2

t1 t2 t3 t1 t2 t2 t3

Page 25: Real Time Operating System Concepts

Kernel Time Slicing• To allow equal priority tasks to preempt

each other, time slicing must be turned on:– kernelTimeSlice (ticks)– If ticks is 0, time slicing turned off

• Priority scheduling always takes precedence. – Round-robin only applies to tasks of the same

priority..

Page 26: Real Time Operating System Concepts

Performance Enhancements• All task reside in a common address

space

doCommFunc ( int data) {……

}

Common SubroutinetTaskA

TaskA() {

doComFunc(10)

}

TaskB() {

doComFunc(20)

}

tTaskB

10

TASK STACKS

20

Page 27: Real Time Operating System Concepts

VxWorks Real Time System

text

data

bss

RAMtTaskA

fooSet(10)

fooSet(10)

tTaskB

fooLib

int fooVal;

void fooSet(int x)

{

fooVal = x;

}

Page 28: Real Time Operating System Concepts

Performance Enhancements• All tasks run in privileged mode

Page 29: Real Time Operating System Concepts

How real time OS meets the real time requirements.

• Controls many external components. – Multitasking allows solution to mirror the

problem. – Different tasks assigned to independent

functions.– Inter task communications allows tasks to

cooperate.

Page 30: Real Time Operating System Concepts

How real time OS meets the real time requirements.

• High speed execution– Tasks are cheap (light-weight). – Fast context switch reduces system overhead.

• Deterministic operations– Preemptive priority scheduling assures

response for high priority tasks.

Page 31: Real Time Operating System Concepts

RTOS Requirements

• Small Codesize• Run mostly on single card system

Page 32: Real Time Operating System Concepts

Overview of Multitasking

• Low level routines to create and manipulate tasks are found in taskLib..

• A VxWorks task consists of: – A stack (for local storage such as automatic

variables and parameters passed to routines). – A TCB (for OS control).is not specific to a

task.

Page 33: Real Time Operating System Concepts

Overview of Multitasking

• Code is not specific to a task.– Code is downloaded before tasks are spawned. – Several tasks can execute the same code (e.g.,

printf( ))

Page 34: Real Time Operating System Concepts

Creating a TasktaskSpawn

Stack TCB

foo ( ){

….}

Page 35: Real Time Operating System Concepts

int taskSpawn ( name , priority, options, stackSize, entryPt, arg1, … arg10)– name Task name– priority Task priority (0-255)– options Task Options eg VX_UNBREAKABLE

– stackSize size of stack to be allocated– entryPt Address of code to execute ( initial PC

)

– arg1 … arg10 Arguments to entry point routine.arguments to entry point routine.

Creating a Task

Page 36: Real Time Operating System Concepts

• Assigned by kernel when task is created. • Unique to each task. • Efficient 32 bit handle for task. • May be reused after task exits. • If tid is zero, reference is to task making

call (self).

Task IDs

Page 37: Real Time Operating System Concepts

• Relevant taskLib routines:– taskIdSelf( ) Get ID of calling task– taskIdListGet( ) Fill array with Ids of all

existing tasks.– taskIdVerifty( ) Verify a task ID is valid

Task IDs

Page 38: Real Time Operating System Concepts

• Provided for human convenience.– Typically used only from the shell (during

development).– Use task Ids programmatically.

• Should start with a t. – Then shell can interpret it as a task name.– Default is an ascending integer following a t.

Task Names

Page 39: Real Time Operating System Concepts

• Doesn’t have to be unique (but usually is).• Relevant taskLib routines: routines:

– taskName( ) Get name from tid.– taskNameToId( ) Get tid from task name.

Task Names

Page 40: Real Time Operating System Concepts

• Range from 0 (highest) to 255 (lowest).• No hard rules on how to set priorities. There

are two (often contradictory) “rules of thumb”:– More important = higher priority.– Shorter deadline = higher priority.

• Can manipulate priorities dynamically with: – taskPriorityGet (tid, &priority)– taskPrioritySet (tid, priority)

Task Priorities

Page 41: Real Time Operating System Concepts

taskDelete (tid)• Deletes the specified task.• Deallocates the TCB and stack.

Task Delete

Page 42: Real Time Operating System Concepts

exit (code)• Analogous to a taskDelete( ) of self. of

Code parameter gets stored in the TCB field exitCode.

• TCB may be examined for post mortem debugging by: – Unsetting the VX_DELLOC_STACK option

or, – Using a delete hook. Using a delete hook.

Task Delete

Page 43: Real Time Operating System Concepts

• Contrary to philosophy of system resources sharable by all tasks.

• User must attend to. Can be expensive.• TCB and stack are the only resources

automatically reclaimed..

Resource reclamation

Page 44: Real Time Operating System Concepts

• Tasks are responsible for cleaning up after themselves. – Deallocating memory. – Releasing locks to system resources.– Closing files which are open.– Deleting child/client tasks when parent/server

exists.

Resource reclamation

Page 45: Real Time Operating System Concepts

taskRestart (tid)• Task is terminated and respawned with

original arguments and tid. • Usually used for error recovery.

Task Control

Page 46: Real Time Operating System Concepts

taskSuspend (tid)• Makes task ineligible to execute. • Can be added to pended or delayed state.

taskResume (tid)• Removes suspension. • Usually used for debugging and

development

Task Suspend and Resume

Page 47: Real Time Operating System Concepts

Intertask Communications

• Shared Memory• Semaphores: Timeout and queues

mechanism can be specified– Binary Semaphore– Mutual Exclusion Semaphores– Counting Semaphores

Page 48: Real Time Operating System Concepts

Shared Memory

foo.hextern char buffer[512];extern int fooSet();extern char *fooGet();

foo.c#include foo.hchar buffer[512];fooSet{}fooGet(){}

taskA(){

…fooSet();….

}

taskB(){

…fooGet()…

}

Page 49: Real Time Operating System Concepts

Semaphores

Int semTake (SEMID)if a task calls this function

– this function will return if the semaphore is not taken already– this function will block if the semaphore is taken already

Int semGive (SEMID)if a task call this function and

– there is a task which blocked that task will continue– if there is no task blocked the semaphore is free

Page 50: Real Time Operating System Concepts

SemaphorestaskBtaskA

Semaphore

Time

Page 51: Real Time Operating System Concepts

Intertask Communications

• Message Queues– Any task including the interrupt handler can

send message to message queues.– Any task can get message from message

queues(excl. interrupt context).– Full duplex communications between 2 tasks

requires two message queues– Timeout can be specified for reading writing

and urgency of message is selectable

Page 52: Real Time Operating System Concepts

Intertask Communications

• Message Queues– MSG_Q_ID msgQCreate (maxMsgs,

maxMsgLength, Options )– maxMsgs

• max number of messages in the queue.

– maxMsgLength• max size of messages

– options• MSG_Q_FIFO or MSG_Q_PRIORITY

Page 53: Real Time Operating System Concepts

Intertask Communications

• STATUS msgQSend (msgQId, buffer, nBytes, timeout, priority)

• int msqQReceive (msgQId, buffer, nBytes, timeout )

• STATUS msgQDelete (msgQId );

Page 54: Real Time Operating System Concepts

Intertask Communications

• Pipes– Named I/O device– Any task can read from/write to a PIPE– an ISR can write to a PIPE– select () can used on a pipe

• N/W Intertask Communication– Sockets (BSD 4.3)– RPC

Page 55: Real Time Operating System Concepts

Features VxWorks supports

• Interrupt handling Capabilities• Watchdog Timer• Memory management

Page 56: Real Time Operating System Concepts

Interrupt Handling

• Interrupt Service routines– They can bound to user C code through

intConnect.– intConnect takes I_VEC, reference to ISR and

1 argument to ISR

Page 57: Real Time Operating System Concepts

Don’t’s of ISR• All ISR use a common stack verify through

checkStack()• Interrupts have no task control block and

they do not run in a regular task context.

Page 58: Real Time Operating System Concepts

Donts of ISR(cont)• ISR must not invoke functions that might

cause blocking of caller like– semTake(), malloc(), free(), msgQRecv()– No I/O through drivers

• floating point co-processors are also discouraged as the floating point registers are not saved or restored.

Page 59: Real Time Operating System Concepts

Exceptions at Interrupt level

• Stores the discriptions of the exception in a special location in memory.

• System is restarted• In boot ROM the presence of the exception

description is tested, if present it prints it out.

• For re displaying ‘e’ command in the boot ROM can be used.

Page 60: Real Time Operating System Concepts

Errors and Exceptions

• ‘errno’ is a global int defined as a macro in “errno.h”

• This return the last error status• Default expection handler of the OS merely

suspends the task that caused it and displays the saved state of the task in stdout

Page 61: Real Time Operating System Concepts

Errors and Exceptions (cont)

• ti and tt can be used to probe into the status• Unix compatible signals() facility is used to

tackle exception other than that of the OS

Page 62: Real Time Operating System Concepts

Watchdog Timers

• Mechanism that allows arbitary C functions to be executed after specified delay

• function is executed as an ISR at the inrruptlevel of the system clock

• All restrictions of ISR applies

Page 63: Real Time Operating System Concepts

Watchdog Timers

• Creation of a WD timer is through wdCreate()

• Deletion of a WD timer through wdDelete()• Start a WD timer through wdStart()• Cancel a WD timer through wdCancel()

Page 64: Real Time Operating System Concepts

Network Capablities in vxWorks

• Normally uses Internet protocol over standard ethernet connections

• Transperency in access to other vxWorks/Unix systems thro’ Unix compatible sockets

• Remote command execution• Remote Login

Page 65: Real Time Operating System Concepts

Network Capabilities in vxWorks

• Remote Procedure calls• Remote debugging• Remote File access• Proxy ARP

Page 66: Real Time Operating System Concepts

Software development Environment

Development Host Target Platform

Ethernet LAN

RS-232

Page 67: Real Time Operating System Concepts

Libraries

• vxWorks routines are grouped into libraries• Each library has corresponding include files• Library

– taskLib– memPartLib– semLib– lstLib– sockLib

• Routine– taskSpawn– malloc– semTake– lstGet– send

• Include files– taskLib.h– stdlib.h– semLib.h– lstLib.h– types.h,

sockets.h, sockLib.h

Page 68: Real Time Operating System Concepts

Host Machine

• Any workstation (could run Unix)• OS should support networking• Cross/Remote Development Software• Unix platform

– Edit, Compile, Link programmes– Debug using vxWorks Shell or gdb

Page 69: Real Time Operating System Concepts

Target Machine

• Any H/W which is supported by vxWorks.• Could be Custom Hardware also.• Individual object code (.o files)

downloaded dynamically.• Finished application could be burnt into

ROM or PROM or EPROM.

Page 70: Real Time Operating System Concepts

Loader and System Symbol Table

• Global system symbol table• Dynamic loading and unloading of object

modules• Runtime relocation and linking.

Page 71: Real Time Operating System Concepts

Shared Code and reentrancy

• A single copy of code executed by multiple tasks is called shared code

• Dynamic linking provides a direct advantage

• Dynamic stack variables provide inherent reentrancy. Each task has ints own task. Eglinked list in lstLib

Page 72: Real Time Operating System Concepts

Shared Code and reentrancy

• Global and static variables that are inherently non-reentrant, mutual exclusion is provided through use of semaphores eg. semLib and memLib

• Task variables are context specific to the calling task. 4 byte task vars are added to the task’s context as required by the task.

Page 73: Real Time Operating System Concepts

The Shell

• Command Line interpreter allows execution of C language expressions and vxWorksfunctions and already loaded functions

• Symbolic evaluations of variables

Page 74: Real Time Operating System Concepts

The Shell

• -> x=(6+8)/4– x=0x20ff378: value=12=0xc

• -> nelson = “Nelson”• new symbol “name” added to symbol table• -> x

– x=0x20ff378: value=12=0xc

Page 75: Real Time Operating System Concepts

The Shell

• Commands

-> lkup ( “stuff”)stuff 0x023ebfffe bssvalue = 0 = 0x0-> lkup(“Help”)_netHelp 0x02021a90 text_objHelp 0x02042fa0 textvalue = 0 = 0x0

Page 76: Real Time Operating System Concepts

The Shell

• Commands

* sp creates a task with default options* td deletes a task* ts/tr Suspend/resume a task* b set/display break points* s single step a task* c continue a task* tt Trace a tasks stack* i/ti give (detailed) task information* ld load a module* unld unload a module

Page 77: Real Time Operating System Concepts

The Shell

• Commands

* period* repeat* cd (“/u/team3”); the quotes are required* ll shows directory contents* ls() same as ll

Page 78: Real Time Operating System Concepts

The Shell

• Commands

Shell redirection-> < scriptshell will use the input as from the file-> testfunc() > testOutputshell will execute the function and the output will be stored in a file “testOutput”

Page 79: Real Time Operating System Concepts

Debugging in vxWorks

• vxWorks provides Source level debugging• Symbolic disassembler• Symbolic C subroutine traceback• Task specific break points• Single Stepping• System Status displays

Page 80: Real Time Operating System Concepts

Debugging in vxWorks

• Exception handlers in hardware• User routine invocations• Create and examine variable symbolically

Page 81: Real Time Operating System Concepts

The Shell based Debugging

• Shell based debugging commands* b funcName() will set a break point in the beginning of the function funcName()* b 0xb08909f will set a break point at the address 0xb08909f* bd funcName() will delete the breakpoint at the beginning of the function funcName()* l will disassemble the code

Page 82: Real Time Operating System Concepts

System Tasks

• tUsrRoot– 1st task to be executed by the kernel

• File: usrConfig.c• Spawns tShell, tLogTask, tExecTask, tNetTask and

tRlogind

• tShell– The Application development support task

Page 83: Real Time Operating System Concepts

System Tasks

• tLogTask– Log message hander task

• tNetTask– Network support task

• tTelnetd– Telenet Support task

Page 84: Real Time Operating System Concepts

System Tasks

• tRlogind– Rlogin support for vxWorks. Supports remote

user tty interface through terminal driver• tPortmapd

– RPC Server support• rRdbTask

– RPC server support for remote source level debugging.

Page 85: Real Time Operating System Concepts

Why use RTOS?

• Unix– except QNX, most unices don’t live up to the

expectation of Real Time situations– Present day unix kernel scheduler do support

Realtime requirements– So Unix kernel can prioritize realtime processes– a flag to indicate a RT process is often provided

to this effect

Page 86: Real Time Operating System Concepts

vxWorks Vs Unix

• vxWorks does not provide resource reclamation– Deviation: user must write their own routine

when need.• vxWorks has a smaller context switch and

restore– Hence time taken to change context is much

smaller.

Page 87: Real Time Operating System Concepts

vxWorks Vs Unix(contd)

• vxWorks requires special care to be taken when writing multitasking code– Semaphore are used to achieve reentrancy

• vxWorks has a minimal interrupt latency

Page 88: Real Time Operating System Concepts

vxWorks Vs Unix(contd)

• vxWorks execute threads in a flat memory architechure as part of OS

• vxWorks has no processes. The so-called tasks are actually threads

• vxWorks scheduling could be on round-robin time slicing or pre-emptive scheduling with priorities.

Page 89: Real Time Operating System Concepts

vxWorks Vs Unix• vxWorks networking is completely Unix

compatible. Portable to BSD4.2/4.3 Unix supporting TCP/IP

• vxWorks support BSD sockets• vxWorks does not distinguish between kernal

mode and user mode execution– Hence minimal mode change overhead on a give

hardware• vxWorks has Virtual memory model

Page 90: Real Time Operating System Concepts

vxWorks Vs Unix• vxWorks is not “Realtime Unix” OS or even

a variant of Unix• vxWorks and Unix enjoy a symbiotic

relationship• vxWorks can use Unix as application

development platform