Download as pdf or txt
Download as pdf or txt
You are on page 1of 33

Embedded Systems

UNIT-4
Introduction to RTOS

Dr.Rekha.K.S.
Associate Professor,Dept of CS&E
NIE,Mysuru
Introduction to RTOS
Tasks and Task States
A basic building block of software written under
RTOS is called TASK. Task can be in one of the
following states
1. Running : Microprocessor is executing this task
2. Ready : It has things that it could do if the
microprocessor is available(other task is
executed in the microprocessor)
3. Blocked: It is waiting for some external event.
Task can not be executed even if it gets
microprocessor
Task states in RTOS environment
The Scheduler
• Part of RTOS that keeps track of the state of
each task and decides which task should go
into running state
• It looks at priority of each task and among the
tasks that are not in blocked state and runs
the task with highest priority
Task states in RTOS environment

S
c
h
e
d
u
l
e
r
Common questions about scheduler
• How does a scheduler know when a task has
become blocked/unblocked?
• What happens if all the tasks are blocked?
• What if two tasks with the same priority are
ready?
• If one task is running and another task with
high priority unblocks, does the task that is
running get stopped and moved to the ready
state right away?
Example on uses for Tasks
Microprocessor Responds to a button
under RTOS
RTOS Initialization code
Tasks and Data
• Private context: register values, program
counter and stack
• Shared data: global, static data
Data in RTOS based Real time System
Example for sharing data among tasks
Tasks sharing code(function)
Re-entrant Functions
• 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
(the private variables of that task).
• A reentrant function may not call any other
functions that are not themselves reentrant.
• A reentrant function may not use the
hardware in a nonatomic way
Semaphores
Semaphores and Shared data
• Semaphore – a variable/lock/flag used to control
access to shared resource (to avoid shared-data
problems in RTOS)
• TakeSemaphore()
• ReleaseSemaphore()
If one Task has called TakeSemaphore and has not
called ReleaseSemaphore, then any other task
that calls TakeSemaphore will block until first
task calls ReleaseSemaphore
Semaphores protect data
Semaphores protect data
µC/OS functions
• OSInit
• OSStart
• OSTaskCreate
• OSSemInit
• OSSemPend
• OSSemPost
• OSTimeDly
Semaphores protect data in the
nuclear Reactor
Initializing Semaphore
• Highest Priority Task is used for Initializing the
semaphore
Reentrancy and Semaphores
• Multiple Semaphores:
OS_Event Sem1,sem2....semn

• Semaphore as a signalling device


eprinter
Semaphore problems
• Forgetting to take semaphore
• Forgetting to release semaphore
• Taking the wrong semaphore
• Holding the semaphore too long
-priority inversion
• Causing deadly embrace
Priority Inversion
Deadly embrace
Semaphore Variants
• Counting semaphore: Semaphores that can be
taken multiple times
• Resource semaphore :Semaphores that can be
released by only the task that took the
semaphore
• Mutex semaphore: Semaphore that will
automatically deal with priority inversion
problem
Ways to protect Shared data
• Disabling Interrupt: response time of IR and
tasks are affected (context switching between
the task is also disabled?)
• Taking Semaphore: response time of IR and
tasks that do not need semaphore are not
affected
• Disabling task switches: : response time of IR
are not affected but tasks are affected

You might also like