RTOS Based Embedded Design (Unit 4 Btech)
RTOS Based Embedded Design (Unit 4 Btech)
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
Difference Between RTOS & GPOS
• RTOS is light weight and small in size compared to
a GPOS.
• A GPOS is made for high end, general purpose
systems like a personal computer, a work station, a
server system etc.
• The basic difference between a low end system and
high end system is in it's hardware configuration
Contd..
• A General Purpose Operating System (GPOS) is a
complete OS that supports process management,
memory management, I/O devices, file systems and
user interface.
1. Program :
When we execute a program that was just compiled, the OS will generate a process
to execute the program. Execution of the program starts via GUI mouse clicks,
command line entry of its name, etc. A program is a passive entity as it resides in the
secondary memory, such as the contents of a file stored on disk. One program can
have several processes.
2. Process :
The term process (Job) refers to program code that has been loaded into a
computer’s memory so that it can be executed by the central processing unit (CPU).
A process can be described as an instance of a program running on a computer or
as an entity that can be assigned to and executed on a processor. A program
becomes a process when loaded into memory and thus is an active entity.
Sr.No Program Process
▪ Hard real-time
• Tasks have to be performed correctly and on time
• Deadline scheduling
• Aircraft controller, Nuclear reactor controller
4.1 Structure of a RTOS
Applications
RTOS-kernel
BSP
Custom-Hardware
4.1 Components of RTOS
Task structure:
▪ An infinite loop
▪ An self-delete function
Waiting
Task Delete
4.1 Task Priority
States
Stack Pointer
Priority
Misc …
Link Pointer
4.1 Task Control Block(cont.)
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.
new admitted
exit terminated
interrupt
ready running
Scheduler
I/O or I/O or
dispatch
event event wait
completi
on
waitin
g
4.1 Process States
• New - The process is being created.
▪ Interrupt Handler
A routine executed whenever an interrupt occurs. It
determines the interrupt source and calls corresponding
ISR. Usually provided by OS.
bus
Peripheral A
Interrupt ARM
Controller Processor
Peripheral B
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
4.1 Interrupt
▪ When an interrupt occurs, no further interrupt accepted
ISR
High-priority Task
high-priority task
Relinquishes the CPU
Time
4.3 Task Scheduling
▪ Non-preemptive
Low-priority Task
ISR
High-priority Task
low-priority task
Relinquishes the CPU
Time
4.3 Non-Preemptive Scheduling
• Why non-preemptive?
Time
• Basic terms
– nonblocking - Operation does not wait for
completion
– synchronous - Completion of send requires initiation
(but not completion) of receive
– ready - Correct send requires a matching receive
– asynchronous - communication and computation
take place simultaneously, not an MPI concept
(implementations may use asynchronous methods)
4.5 Basic Send/Receive modes
• MPI_Send
– Sends data. May wait for matching receive. Depends on
implementation, message size, and possibly history of
computation
• MPI_Recv
– Receives data
• MPI_Ssend
– Waits for matching receive to start
• MPI_Rsend
– Expects matching receive to be posted
4.5 Nonblocking Modes
• MPI_Isend
– Does not complete until send buffer available
• MPI_Irsend
– Expects matching receive to be posted when called
• MPI_Issend
– Does not complete until buffer available and matching
receive posted
• MPI_Irecv
– Does not complete until receive buffer available (e.g.,
message received)
4.5 Completion
• MPI_Test
– Nonblocking test for the completion of a nonblocking
operation
• MPI_Wait
– Blocking test
• MPI_Testall, MPI_Waitall
– For all in a collection of requests
• MPI_Testany, MPI_Waitany
• MPI_Testsome, MPI_Waitsome
• MPI_Cancel (MPI_Test_cancelled)
4.5 Message Passing
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
• Requires dynamic memory allocation, so
fragmentation could occur.
4.5 Basic Communication Primitives
• 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
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.
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.
timeout = 0;
Wait(s, timeout, &error_code);
Access shared data;
Signal(s);
4.7 Applications of Binary Semaphores
Request/Release
Semaphore RS-232
Request/Release
Task 2
Send data via RS232
4.7 Semaphore
Task Post
Mail Box
Pend
Task
ISR Post
“message”
4.7 Message Queues
▪ Array of MailBox
▪ FIFO & FILO configuration
Task Post
Mail Queues
Pend
Task
ISR Post
“message”
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()
4.7 Memory Management
dispatch dynamically
allocate statically
OS Initializing OS running
4.7 Memory Management
(4) (12)
Task 1 (H)
(8)
Task 2 (M)
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.
AFS support
uHAL routines
routines
Development board
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
4.9 RTLinux
I/O, interrupts
Hardware