Professional Documents
Culture Documents
Unit 5: Real Time Operating System
Unit 5: Real Time Operating System
• Function-queue-scheduling
Advantages
• Very simple
• No shared variables
Disadvantages
• Possible long response time:
• might not meet requirements
• might feel sluggish
• No priority mechanism
• Fragile: hard to extend
Round-Robin with Interrupts
• Short Interrupt routines to handle the urgent needs of the
hardware and set flags
• The main loop checks the flags and performs the actual
needed processing
• Different devices can be assigned to different levels of
priority interrupts
.
_oi _ ma1 (voi _
{
while (TR - )
{
if (f eviceA)
(
f eviceA FALs ~:
!! andle data to or From 110 Dev·ce A
)
1f ( f - e - 1 c eB )
{ voi = interr . t vHan : le .eviceA (voi _- )
High priority
Everything Device A ISR Device A ISR
Device B ISR Device B ISR
Device C ISR Device C ISR
Device D ISR Device D ISR
Device E ISR Task Code 1
Device F ISR Task Code 2
Low priority Device G ISR Task Code 3
All task Code Task Code 4
Summary
Worst Response Time for Stability of response when the
Priority available task code code changes Simplicity
Round robin None Sum of all task code Poor Very Simple
ISR-priority order All Total of execution time Must deal with data shared
Round robin Task codes-same Good for ISR between ISR and Task code
for all task code
With Interrupt Poor for task code
Function Queue ISR and Task code - Execution time for the Must deal with shared data and
scheduling priority order longest function must write function queue code
Relatively good
• Function-queue-scheduling
• Turn around time: The length of time it takes to run a process from
initialization to termination, including all the waiting time.
• Waiting time: The total amount of time that a process is in the ready
queue.
• Response time: The time between when a process is ready to run and
its next I/O request.
Function-Queue Scheduling
• No processing loop
High priority
Everything Device A ISR Device A ISR
Device B ISR Device B ISR
Device C ISR Device C ISR
Device D ISR Device D ISR
Device E ISR Task Code 1
Device F ISR Task Code 2
Low priority Device G ISR Task Code 3
All task Code Task Code 4
Summary
Worst Response Time Stability of response when
Priority for task code the code changes Simplicity
available
.
lrntertas.k
• Real-time systems
are those systems in
which
the correctness of
D,namic the system
l/Tummy Manage- depends not only on
the logical result of
[im ·rs
Allio. ment
computation, but
also on the time at
which the results are
produced.
Snper,mo:r • Real Time Operating Systems .
I
Time embedded system (RTOS)
• TASK SCHEDULING
• DETERMINISM
• PREEMPTIVE KERNEL
• PRIORITY INVERSION
• USAGE
Rlli\,~Z: llllliJ!l15MifflWJIH&&fftWJ!Ait1iSCT7JSMII
• The scheduler
should be able
to preempt any
task in the
system and
allocate the
resource to the
thread that
needs it most
even at peak
load.
I
Thread
priority
PRIORITY
60 TH1
--•'=T=~_; · TH1
20
•• TH3 .
• • H3
emption.The
highest priority
I
• I
TIME
task that is
ready to run
will be the task
REQUEST - EXIT PROCESSOR
I t
that will be
ACQUIRE PROMOTE
I ♦
running.
RELEASE DEMOTE
• Inter Task Communication &
Synchronization - Multiple tasks
pass information among each other
in a timely fashion and ensuring
data integrity
• Priority Inheritance - RTOS should
have large number of priority levels
& should prevent priority inversion
using priority inheritance.
I
• Interrupt dispatch latency:
Short Latencies - The The time from the last
latencies are short and instruction in the interrupt
predefined. handler to the next task
scheduled to run.
I
Vulnerabilites
I
Unit-5
f1
r om one task to a nothe1r.
• l f m,e ssages ,a re sent more quickly t han they can be handled, t he OS provides
message queues fo,r holldi ng the messages until they c,an be processed.
Messag Message
Producer Task Receiver Task
It is, s.ometime,s ne,ce,ssary for a task or an SR to communicate ·ntormat''on to another
task. This information transfer is calle,d intertask communicatio.n. Information 1
m ay be
communicat,e d between tasks ·n two, ways::through global data or by sending mes,sages,.
Wh,en using gllolba l variables, e,ach task or ISR. rm ust ensU1 re that it has exclus ive, a1ccess t ,o
t he va r iables.
If an ISR is involved, the on ly way to ,ens11.1 n;2 excl usiive access t o t he ,c ommon variable,s is to
disable interrupts.
If t wo tasks are shar ing data each can gaiin excllusi'v,e access to t h e vari'abl,es by usi ng e'i ther
disabling/enablii ng inter rupts or th rough a semaphore (as we have s,een). Note that a task
earn only commun icate information to an ISIR by using global va riablles.
A ta sk is n,ot aware when a global varia b le is changed by an, ISR un less t he ISR sign,als t he
task by usi ng a semaphore or by having, the task regu la rly polll t he contents of t he va ri ab le.
PENO ►
1
Through a service provided by the kernel, a task or an ISR can deposit a message (the
pointe·r) ·nto this mailbox. Simila1rly, one or moire tasks can l"leceive mess.a ges through a
se·rvice p1rovided by the kernel.
Both the sending task and receiving task will agree as to what the po·nter is actually
pointing to.
Kerne services are typically provided to:
a) Initialize the contents of a m1ailbox . The ma 'illbox may or may not initially contain a
me,ssag e.
1
A retu rn code is used t o notify the callller about the outcome of t he ca ll.
message, queues and mailboxes are soiftware-engine,ering components used for inter
process comm un ication, or for inter-thread com 1
m1uniication within t he sam,e process.
They use a queue for m,essaging - the passing of contro,I or of content. Group
communkatio,n systems provide similar kinds of functiona lity.
Through a service provided by·the ker·n el, a task or an ISR can deposit a me,s.saig e (the
pointer) into a message queue ..
one or 1
m ,o re tasks can re,c eive me·s.sages thr,o ugh a service provide,d by the kernel.
~m rru ~ s PEND
►
ll :' II e
If the queue contained a message, the message is extracted from t he queue. A return code is
use,d to notify the cal ler about the outcome of the call.
Both the sending·task and receiv·"ng task wil agri!e as to what t he pointer is actua ly
pointing to .
G1ene1rally, the first mes.s age inse·rted in the queue will be· the first mie s.s age extracted
fro the ,q1ueue ,(IFIFO).
lln addition t,o e·x tract me·s.sages in a FIFO fas ion, µC/OS -111allows a task to get messages.
Last-In-first-Out f--FO).
As w'ith the mailbox,. a1·w aiting ist is as.sociated with e·a ch mes.s age queue i case mor'le
than one task is to receive messages through the ,q ueue .
OSQ,Po,s ·t Fron·t
OSQ,:F 1 us.h 1(::,
1( )
l
'Task
OSQ,Po,s 't 1( )
OSQ PO,S't P,r on't 1( )
1
e·ssage
osg,·, l US,h ()
osg,Acc,ep I- ( )
5. Pipes
Write Data
Read Data
to Pipe
)
Read Data
Write Data
from Pipe
7
/4teData
"'
Read Data
from Pipe
to Pipe
Pipes
• A pip,e is an RTOS object t hat provide simplle
communication channel used fo r unstructured data
exchange among tasks. A
• Pipes can be opened, closed, written to and read
from.
• Traditionally, a pipe is a unidirectional data exchange
faci lity.
• Data is written into the pipe as an unstructured byte
stream. Unlike m,essage queue, a pipe does not store
multiple messages but strea1 m of bytes.
• lln addition, data flow from a pipe cannot b,e
• prioritized.
Pipes
• A task can write into pipe and another task
can read the data from Pipe.
• The output of one task is passed on as in11p ut
the other task.
• Task to Task or ISR to task data transfer can
take place using pipes.
Timer Management
• Timers are used to measure the elapsed time of ev,e nts.
• The kernel has to keep track of different times.
- A particular task may need to be ,e xecuted
p,eri,o,dically, say, every lOms. A timer has to, keep
track this periodicity.
- A task may be waiting in a queue for an event to
occur. If the event does not occur for a specified time
, it has t ,o, take appropriate action.
- A task may be waiting in a queue for a shared
resource. If the resource . If the resource is not
available for a specified time, an appropriate action
has to be taken.
Timer Management
• In embedded systems, system and user tasks
are often scheduled to perform after a
specified duration.
• To providle such sclhedulling, there is a need for
a periodical interrupt to keep track of time
delays and timeout.
• Most RTOSs today offer both "relative timers"
1
✓ When one task changes the memory, all the other tasks see the
mod1f·cat1on.
Solution:
• Disabling interrupts
• Semaphores
• Disabling task switches
/* "Button Task"*/
void vRespondToButton (void)
{
/* High priority*/ SRM
1.·
......
__,m'IF.o
inti;
while ( TRUE)
{
11 Block until user pushes a button
i •!!ID of button pressed;
printf ("\nTIME: %08ld LEVEL: %08ld",
tankdata[1].lT1meUpdated,
tankdata[1].1TankLevel); /* "Levels
Task''*/
}
} void vCalculateTankLevels (void) /* Low priority*/
{
inti - O;
wh1le (TRUE)
{
!! Read 1eve1s of floats 1n tank i
11 Do more interminable calculation
!! Do yet more interminable ca1cu1ation
Re-entrancy
u....,J1,a.,~ 11J./E.1(.("An.r11SU
-v oi
0 C io (. r i
' n oc 1 ;
oc·l ;
SRM
__,m......
1.· 'IF.o
/* "Button Task"*/
void vRespondToButto~ (void) /* High priority*/
{
int i ;
while (TRUE) .
{
!! Block until user pushes ·~ button
i =!!Get ID of button pressed
TakeSemaphore (); ·
pri ntf ( "\nT.IME: %081 d LEVEL: %081 d",
tankdata[i].lTimeUpdated,
tankdata[i J. lTankLevel);
Re l easeSemaphore ();
}
}
/* "Levels Task"*/
. void vCalculateTanklevels (void) /* Low priority*/
{
.
inti= O;
. . .. .
while (TRUE)
{
TakeSemaphore (); .
! ! Set tankdata[j], 1TimeUpdated
!! Set tankdata[;J, lTankLevel
ReleaseSemaphore ( )': .·
..
}
}
SRM -.....
Code in the vc al cul a teTa n kLeve ls task. Code in the vResporidToBu tton t a sk. ·
TakeSemaphor e ()
l
! ! Set tinkdat~[i].lT imeUpdated
I
The semaphore i"is not available; th e .
button tas k blocks; the R..TOS
J
('This does· not return yet}
.
switch e s back.
~
The buuo n ,as k: blocks; ,he RTOS . ·)
res u r n es the leve ls t a sk.
SRM
Multiple semaphore
I. :ffm'IF.<il' SClf~& TKIC'10Ulm
u....,J1,a.,~ 11J./E.1(.("An.r11SU
while ( TRUE)
{
/* Wait for a m~ssage lling wha t repor t to ormat. * /
wMsg - (int) OSQPend (QP rinterTas . WAJT_ FOREVER. &by Error);
els,e
I* P·rint · he n xt 1 i e. •'/
1
Har d " r , ,Pr1 nte r Outp u L1 n, C. _ c:h Pri n [IL n sPr i t d · · l);
SRM
Semaphore problems
I. :ffm'IF.<il' SClf~& TKIC'10Ulm
u....,J1,a.,~ 11J./E.1(.("An.r11SU
m ls (h 0 );
ajsrnrls (hSernapho r eB) ;
}
void ~ sk2 ()
{
aJsmrsv (hSemap oreB , C, C} ;
a sm_ V (h or I C, C} ;
b = ,•
a smr s (hSema 0 eB) ;
ajsmrls (hSemap oreA) ;
SRM
I, . m'IF.Ci
~ .. .., .
1- .
1
~
~1"'
'riilt:'iOUICl
AA-rillU
M,ethods to P1
rotect: S,hared Data
■1 The 2 basic m1ethods are disabl"ng interrupts and using s,emaphores.
■1 A thiird 1
m,ethod is disablli g task swiitches but this has no effect on
interrupt routines .
■ Note - interrupts are not .allowed to take semaphores so they
cannot be used if the, da a is shared between the lask code and the
ISR .