Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 28

Real-Time Operating Systems

Read Chapter 6
(David E. Simon, An Embedded Software Primer)
An Operating System:
• What: It is a program (software) that acts as
an intermediary between a user of a computer
and the computer hardware.
• Why: Make the use of a computer
CONVENIENT and EFFICIENT.
RTOS Architecture Revision
• The real time operating system can suspend one
task code subroutine in the middle of its processing
in order to run another.

• The necessary signaling between the interrupt


routines and the task code is handled by the
operating system.

(02/24/2022) 3
What Is an Operating System?
For an Embedded System

• Provides software tools for a convenient


and prioritized control of tasks.
Also called as Kernel, real-time kernel

• Provides tools for task (process)


synchronization.

• Provides a simple memory management


system
Convenient and Efficient?
General Purpose

• Convenient :
– Simplicity of use
– Messy or complicated details are hidden
– User is unaware that he is using an OS

• Efficient
– Resources are optimally used
Desktop OS Vs RTOS
• In desktop computer, the • Here, application gets control
OS takes control of first and it then starts RTOS.
machine as soon as it is
turned on and lets start
your applications. • Application and RTOS are much
• Compile and link more tightly tied to one another.
applications separately • Passing a bad pointer into the
from the OS. system, make the whole system
• Desktop protect rebooted.
themselves as carefully
from your application. • To save memory RTOS typically
• Use more memory include just the services that you
need for your embedded system
and no more.
Commercially Available RTOS:

VxWorks,VRTX,pSOS,Nucleus,C
Executive,LynxOS,QNX,Multi-Task!,AMX, and
dozens more.
The commercial RTOS come already debugged with a
good collection of features and tools.
Process/Task Concept

• An operating system executes a variety of programs:

– Batch system – jobs


– Time-shared systems – user programs or Tasks

• Similar terms job, process, task (ES) almost


interchangeably
Task- Basic building block of software written
under an RTOS
Task is simply a sub routine
Task States
• There are 3 states a task can have:

1. Running – Task instructions are being


executed, only one task at a time.

2. Ready – Task is waiting to do some


operations if uP is available, many tasks at a
time can be in this state.

3. Blocked – Usually in the sleep state,


waiting for an external event to trigger,
many tasks at a time can be in this state. Eg:
task waiting for data from a network
Scheduler
Part of RTOS which keeps track of the state of each task
and decides which one task should go into running state.

• Among the non-blocked tasks the one with the highest


priority will be chosen for running.
Other tasks will wait in ready state
CPU Scheduler
• A task will decide for itself to move to the blocked
state.

• Blocked task never gets uP. So an interrupt or


some
other task must be able to signal that whatever the
task was waiting for has happened. Otherwise the
task is blocked forever.

• Scheduler shuffles the tasks between the ready


and
running states.
How does the scheduler know when the task has become blocked or unblocked?

• A task will decide for itself to move to the blocked


state. It informs the scheduler by calling some
functions and provides the information about the event
on which it isWhat
going towhen
happens wait.
all the tasks are blocked?

If all tasks are blocked, the scheduler will wait until


something happens to make atleast one task running. If
nothing happens, then it is design engineer’s fault.
What if there are two tasks ready at the same time with the same priority?

• If two tasks with same priority are in the ready state,


RTOS will take them one by one (differs with
• If one task is running and another high priority
task unblocks, will the running task get
stopped and moved to the ready state
immediately?

• That depends on the RTOS being preemptive


or non-preemptive
• Non preemptive RTOS

-- take the mp away from the lower priority task


when that task blocks.

• Preemptive RTOS:

..will stop a lower priority task as soon as the


higher priority task unblocks.
A simple Example..,
/* “Button Task*/
Void vbuttontask (void) /*High Priority*/
{
While (TRUE)
{
!! Block until user pushes a button
!! Quick: respond to the user
}
}
/* “Levels Task” */
Void vlevelstask (void ) /*Low Priority*/
{
While (TRUE)
{
!!Read levels of floats in tank
!!Calculate average float level
!!Do some interminable calculation
!!Do more interminable calculation
!!Do yet more interminable calculation

!!Figure out which tank to do next


}
}
User presses button:
Vbuttontask
Vleveltask is busy RTOS switches
finishes its work
calculating while microprocessor to
and blocks
vbuttontask is vbuttontask;
again;RTOS
blocked vleveltask is ready
switches mp back
to vleveltask.

vbuttontask

Vbuttontask does
everything it needs to do
to respond to the
button.

Time
Task and Data

Each task has its own private context, which


includes the register values,a program
counter and a stack.

All other data– global, static, initialized,


uninitialized and everything else- is shared
among all of the tasks in the system.
The RTOS has its own private data structure
which is not available to any of the tasks
DATA IN AN RTOS-BASED REAL TIME SYSTEM Task1 registers Task1 stack

RTOS data
structures RTOS

Task 1

All other data

Task 3

Task 2

Task3 registers
Task2 registers
Task3 stack
Task2 stack
>Shared Data Problem…again……!!!
Void task1 (void)
{

Task ……….
……..
Vcounterrors(9);

can ……..
……..
Void task2 (void)

Share {
…………….
…………….

Data Vcounterrors(11)
……….
……..
}
Static int cerrors;
Void vcounterrors (int cnewerrors)
{
cerrors+=cnewerrors;
}
Y this code fails???
;Assembly code for vcounterrors

;void vcounterrors (int cnewerrors)

;{
;cerrors+=cnewerrors;
Move R1,(cerrors)
ADD R1,(cnewerrors)
Move (cerrors),R1
RETURN
;}
Y this code fails???
R1 for cerrors
TASK1 R1 for
TASK1 calls vcounterrors(9) TASK2 5
Move R1,(cerrors) 5
ADD R1,(cnewerrors) 14

RTOS switches to TASK2

TASK2 calls vcounterrors(11)


Move R1,(cerrors) 5
ADD R1,(cnewerrors) 16
Move (cerrors),R1 16

RTOS switches to Task1 14


Move (cerrors),R1 14
Reentrancy
• A function is said to be reentrant if there is a provision to interrupt the
function in the course of execution, service the interrupt service routine
and then resume the earlier going on function, without hampering its
earlier course of action. 
– Functions that rely on a local variable are considered re-entrant due to
the fact that their variables are safely encapsulated between threads.
• Do not use static or global variables in your function since those may
be changed by time your function resumes
• Function must not modify its own code (e.g. some low level graphic
routines may have "habit" to generate itself)
• Do not call any function that does not comply with the rules above
REENTRANCY IN RTOS
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 a
function.
RULES TO BE REENTRANT
1. 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.

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

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


way.
Use of local variables in reentrant
function
// A non-reentrant example
// [The function depends on global variable i]
//Reentrant example
// Both fun1() and fun2() are
int i;
reentrant
// Both fun1() and fun2() are not reentrant
int fun1(int i)
// fun1() is NOT reentrant because it uses global {
variable i
int fun1() return i * 5;
{
return i * 5; }
}

// fun2() is NOT reentrant because it calls a non- int fun2(int i)


reentrant
// function {
int fun2()
{ return fun1(i) * 5;
return fun1() * 5;
}
}
RTOS
• Pros
– Worst case response time for highest priority function is
zero.
– System’s high priority response time relatively stable
when extra functionality added.
– Useful functionality pre-written.
– Generally come with vendor tools.
• Cons
– RTOS is complex and costly.
– Added processing time.
– Code out of your control, may contain bugs.

You might also like