Unit IV Rtos Based Embedded System Design
Unit IV Rtos Based Embedded System Design
Unit IV Rtos Based Embedded System Design
SYSTEM DESIGN
4.1 Introduction to basic concepts of RTOS- Task, process
& threads, Interrupt routines in RTOS
4.2 Multiprocessing and Multitasking
4.3 Preemptive and non-preemptive scheduling
4.4 Task communication shared memory
4.5 Message passing
4.6 Inter process Communication
4.7 Synchronization between processes-semaphores,
Mailbox, pipes
4.8 Priority inversion, priority inheritance
4.9 Comparison of Real time Operating systems:
VxWorks, C/OS-II, RT Linux
4.1 Introduction to basic concepts of
RTOS
Device ManagementConfigure,
Initiate, register with OS, read, listen,
write, accept, deregister
3
4.1 Operating System
A system software, which
6
4.1 Soft and Hard Real
Time OS
Soft real-time
Tasks are performed by the system as fast
as possible, but tasks dont have to finish by
specific times
Priority scheduling
Multimedia streaming
Hard real-time
Tasks have to be performed correctly and on
time
Deadline scheduling
Aircraft controller, Nuclear reactor controller 7
4.1 Structure of a RTOS
Applications
RTOS-kernel
BSP
Custom-Hardware
4.1 Components of RTOS
14
4.1 Task Structure
Task structure:
An infinite loop
An self-delete function
Waiting
Waiting
Task Delete
16
4.1 Task Priority
17
4.1 Task Control Block
States
Stack Pointer
Priority
Misc
Link Pointer
18
4.1 Task Control Block(cont.)
19
4.1 PROCESS
A process is a program in execution ...
Starting a new process is a heavy job for OS:
memory has to be allocated, and lots of data
structures and code must be copied.
ready running
Scheduler
I/O or I/O or
dispatch
event event wait
complet
ion
waiti
ng
4.1 Process States
New - The process is being created.
bus
Peripheral
PeripheralAA
Interrupt
Interrupt ARM
Controller ARM
Controller Processor
Processor
Peripheral
PeripheralBB
35
4.1 Interrupt
Default interrupt handler: uHALr_TrapIRQ()
1. Save all registers in APCS-compliant manner
2. Call StartIRQ(), if defined
3. Determine interrupt source, call the corresponding
interrupt service routine (ISR)
4. Call FinishIRQ(), if defined
5. Return from interrupt
36
4.1 Interrupt
When an interrupt occurs, no further interrupt
accepted
37
4.2 Multiprocessing Operating System
ISR
High-priority Task
high-priority task
Relinquishes the CPU
Time
56
4.3 Task Scheduling
Non-preemptive
Low-priority Task
ISR
High-priority Task
low-priority task
Relinquishes the CPU
Time
57
4.3 Non-Preemptive
Scheduling
Why non-preemptive?
Time
CPU 1 CPU 2
message message
message
4.5 Message Passing
Communication
Synchronization and Communication.
Fixed Length
Easy to implement
Minimizes processing and storage overhead.
Variable Length
Multiple clients
might request services
from one of
multiple servers. We use mail boxes.
Abstraction of a
finite size FIFO queue maintained by
kernel.
4.5 Synchronization and Buffering
These are the three typical combinations.
78
Fixed-length messages:
simple to implement - can have pool of standard-sized
buffers
low overheads and efficient for small lengths
copying overheads if fixed length too long
can be inconvenient for user processes with variable
amount of data to pass
may need a sequence of messages to pass all the data
long messages may be better passed another way e.g. FTP
copying probably involved, sometimes multiple copying into kernel and out
Variable-length messages:
more difficult to implement - may need a heap with
garbage collection
more overheads and less efficient, memory fragmentation
more convenient for user processes
79
4.6 IPC unicast and
multicast
In distributed computing, two or more processes
engage in IPC using a protocol agreed upon by
the processes. A process may be a sender at
some points during a protocol, a receiver at other
points.
P 2 P 2 P 3 ... P 4
m
m m m
P 1 P 1
P ro c ess 1 P ro c ess 2
d a ta
sender r e c e iv e r
82
4.6 INTERPROCESS
COMMUNICATION
Processes executing concurrently in the operating system may be either
independent or cooperating processes.
Message Passing:
Message passing:
processes send messages along a
communication channel---no common
address space.
4.6 Blocking, deadlock, and
timeouts
Blocking operations issued in the wrong sequence
can cause deadlocks.
Deadlocks should be avoided. Alternatively, timeout
can be used to detect deadlocks.
P ro c ess 1 P ro c ess 2
r e c e iv e f r o m p r o c e s s 2 is s u e d
p r o c e s s 1 b lo c k e d p e n d in g d a t a
fro m p ro c ess 2 .
r e c e iv e d f r o m p r o c e s s 1 is s u e d
p r o c e s s 2 b lo c k e d p e n d in g d a t a
fro m p ro c ess 1 .
new t h r e a d is s u e s a b lo c k in g I P C o p e r a t io n
t h r e a d is b lo c k e d
m a in t h r e a d c o n t in u e s w it h
o t h e r p r o c e s s in g
t h r e a d is u n b lo c k e d a f t e r t h e o p e r a t io n is f u lf ille d
89
4.6 Deadlocks and Timeouts
Connect and receive operations can result in
indefinite blocking
For example, a blocking connect request can
result in the requesting process to be suspended
indefinitely if the connection is unfulfilled or
cannot be fulfilled, perhaps as a result of a
breakdown in the network .
It is generally unacceptable for a requesting
process to hang indefinitely. Indefinite blocking
can be avoided by using timeout.
Indefinite blocking may also be caused by a
deadlock
90
4.7 Semaphores
A semaphore is a key that your code
acquires in order to continue execution
If the key is already in use, the requesting
task is suspended until the key is released
There are two types
Binary semaphores
0 or 1
Counting semaphores
>= 0
4.7 Semaphore Operations
Initialize (or create)
Value must be provided
Waiting list is initially empty
Wait (or pend)
Used for acquiring the semaphore
If the semaphore is available (the semaphore value is
positive), the value is decremented, and the task is not
blocked
Otherwise, the task is blocked and placed in the waiting
list
Most kernels allow you to specify a timeout
If the timeout occurs, the task will be unblocked and an
error code will be returned to the task
4.7 Semaphore Operations
Signal (or post)
Used for releasing the semaphore
If no task is waiting, the semaphore
value is incremented
Otherwise, make one of the waiting
tasks ready to run but the value is not
incremented
Which waiting task to receive the key?
Highest-priority waiting task
First waiting task
4.7 Semaphore Example
Semaphore *s;
Time timeout;
INT8U error_code;
timeout = 0;
Wait(s, timeout, &error_code);
Access shared data;
Signal(s);
4.7 Applications of Binary
Semaphores
Suppose task 1 prints I am Task 1!
Task 2 prints I am Task 2!
If they were allowed to print at the
same time, it could result in:
I Ia amm T Tasask k1!2!
Solution:
Binary semaphore
4.7 Semaphore
Semaphore serves as a key to the resource
A flag represent the status of the resource
Prevent re-entering Critical Region
Can extent to counting Semaphore
Request/Release
Semaphore RS-232
RS-232
Request/Release
Task
Task 22
Send data via RS232
96
4.7 Semaphore
97
4.7 Message Mailbox
Task
Task Post
Mail Box
Pend
Task
Task
ISR Post
ISR
message
98
4.7 Message Queues
Array of MailBox
FIFO & FILO configuration
Task
Task Post
Mail Queues
Pend
Task
Task
ISR Post
ISR
message
99
4.7 MailBox & Queues
Using MailBox in uC/OS-II Using Queue in uC/OS-II
OSMboxCreate() OSQCreate()
OSMboxPend() OSQPend()
OSMboxPost() OSQPost()
OSMboxQuery() OSQPostFront()
OSMboxAccept() OSQQuery()
OSQAccept()
OSQFlush()
100
4.7 Memory Management
dispatch dynamically
Task
Task11 Task
Task22 Task
Task33
allocate statically
OS Initializing OS running
101
4.7 Memory Management
102
4.8 Priority Inversion
Priority Inversion
(4) (12)
Task 1 (H)
(8)
Task 2 (M)
103
4.8 Priority Inversion
A long existing terminology, recently it
becomes a buzzword due to Pathfinder on
Mars (RTOS: VxWorks).
104
4.8 Priority Inversion
105
4.8 Priority Inversion
106
4.8 Priority Inversion
107
4.8 Priority Inversion
Advantage
. Transparent to scheduler
Disadvantage
. Deadlock possible in the case of bad use of semaphores
. Chained blocking: if P accesses n resources locked by
processes with lower priorities, P must wait for n CS
4.8 Priority Inheritance
4.8 Priority Inheritance
Deadlocks
4.8 Priority Inheritance :
Chained Blocking
A weakness of the priority inheritance protocol is
that it does not prevent chained blocking.
115
4.9 C/OS-II Features
116
4.9 C/OS-II vs. HAL
uHAL is shipped in ARM Firmware Suite
uHAL is a basic library that enables simple
application to run on a variety of ARM-based
development systems
uC/OS-II use uHAL to access ARM-based
hardware
C, C++ libraries
AFS support
uHAL routines
routines
Development board
117
4.9 RT-Linux
Init Bash Mozilla
RT-tasks
cannot use standard OS
calls (www.fsmlabs.com)
scheduler
Linux-Kernel
RT-Task RT-Task
driver
interrupts
RT-Linux
RT-Scheduler
interrupts
I/O
interrupts
Hardware
118
4.9 RTLinux
I/O, interrupts
Hardware
121