Download - RTOS Concepts
![Page 1: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/1.jpg)
Real-Time Operating Systems
![Page 2: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/2.jpg)
What is RTOS?• A RTOS (Real-Time Operating System)
– Is an Operating Systems with the necessary features to support a Real-Time System
– What is a Real-Time System?• A system where correctness depends not only on the
correctness of the logical result of the computation, but also on the result delivery time
• A system that responds in a timely, predictable way to unpredictable external stimuli arrivals
![Page 3: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/3.jpg)
Real Time System
• A system is said to be Real Time if it is required to complete it’s work & deliver it’s services on time.
• Example – Flight Control System– All tasks in that system must execute on time.
• Non Example – PC system
![Page 4: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/4.jpg)
GPOS
(GENERAL PURPOSE OPERATING SYSTEMS) RTOS
Time No time bound processing, task has not got any time limit to finish work.
time bound processing, task has to complete work within given time
frame. Deterministic behavior
Memory Architecture
Full virtual memory /Monolithic architecture
Flat memory architecture
Size/latency GPOS are non scalable and has larger footprint and higher context switch latency.
RTOS are scalable and has smaller footprint and low context switch latency and interrupt latency.
Scheduling round robin way of scheduling. priority preemptive scheduling preemption is a must feature
Memory Tight memory constraints are not considered. it is not light-weight.
should not take up too much memory since embedded systems come with tight memory constraints
priority Priority consideration for each task is not as strict as RTOS
Each task must have a priority
![Page 5: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/5.jpg)
RTS• Tasks or processes attempt to control or react to
events that take place in the outside world
• These events occur in “real time” and process must be able to keep up with them
![Page 6: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/6.jpg)
Examples of RTS• ATM machine updating database• Control of laboratory experiments• Process control plants• Robotics• Air traffic control• Telecommunications• Military command and control systems
![Page 7: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/7.jpg)
Engine control system-A real time system
![Page 8: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/8.jpg)
Engine control system-A real time system
![Page 9: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/9.jpg)
Why an RTOS?
![Page 10: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/10.jpg)
Why an RTOS?
![Page 11: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/11.jpg)
Role of an OS in Real Time Systems
• Standalone Applications– Often no OS involved– Micro controller based Embedded Systems
• Some Real Time Applications are huge & complex– Multiple threads– Complicated Synchronization Requirements– Filesystem / Network / Windowing support– OS primitives reduce the software design time
![Page 12: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/12.jpg)
Characteristics of RTOS• Deterministic
– Operations are performed at fixed, predetermined times or within predetermined time intervals
– Concerned with how long the operating system delays before acknowledging an interrupt
![Page 13: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/13.jpg)
Characteristics of RTOS• Responsiveness
– How long, after acknowledgment, it takes the operating system to service the interrupt
– Includes amount of time to begin execution of the interrupt
– Includes the amount of time to service the interrupt
![Page 14: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/14.jpg)
Characteristics of RTOS• User control
– User specifies priority
– Specify paging
– What processes must always reside in main memory
– Disks algorithms to use
– Rights of processes
![Page 15: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/15.jpg)
Characteristics of RTOS• Reliability
– Degradation of performance may have catastrophic consequences
– Attempt either to correct the problem or minimize its effects while continuing to run
– Most critical, high priority tasks execute
![Page 16: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/16.jpg)
Features of RTOS• Fast context switch• Small size(kernel should fit within ROM)• Ability to respond to external interrupts quickly• Multitasking with interprocess communication tools such as
semaphores, signals, and events• Use of special sequential files that can accumulate data at a
fast rate• Preemptive scheduling based on priority (scheduling)• Delay tasks for fixed amount of time • Special alarms and timeouts.• Resource allocation
![Page 17: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/17.jpg)
Types of Real-Time Systems• Hard Real-Time
– Missing a deadline has catastrophic results for the system
• Soft Real-Time– Reduction in system quality is acceptable– Deadlines may be missed and can be recovered from
• Non Real-Time– No deadlines have to be met
![Page 18: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/18.jpg)
Examples• Soft real time e.g.
– “this flight is on time 98 times out of 100”
• Hard real time e.g.– “The emergency valve opens within 20
microsecond after this event occurs, irrespective of system load.”
– “this parachute automatically opens without fail when you reach 500 feet above ground.”
![Page 19: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/19.jpg)
Soft Real Time• Good average case performance
• Low deviation from average –case performance
• Temporally speaking: soft real time systems are statistically predictable, but a single event is neither predictable nor guaranteed.
• soft real time systems are not suited for handling mission critical events.
• Soft real-time systems are typically used where there is some issue of concurrent access and the need to keep a number of connected systems up to date with changing situation.
• software that maintains and updates the flight plans for commercial airliners. The flight plans must be kept reasonably current but can operate to a latency of seconds.
• Live audio-video systems are also usually soft real-time; violation of constraints results in degraded quality, but the system can continue to operate.
![Page 20: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/20.jpg)
Hard Real Time• Predictable performance at each moment of time, not as
“average”
• Low Latency response to events
• Precise scheduling of periodic tasks
• No real time event is ever missed
• System response is load-independent
• e.g. RTLinux, QNX, ECOS, VxWORKS....
![Page 21: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/21.jpg)
RTOS Concepts
![Page 22: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/22.jpg)
Critical Section of Code
• A critical section of code, also called a critical region, is code that needs to be treated indivisibly. Once the section of code starts executing, it must not be interrupted. To ensure this, interrupts are typically disabled before the critical code is executed and enabled when the critical code is finished.
![Page 23: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/23.jpg)
Critical Sections
![Page 24: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/24.jpg)
Resource
• A resource is any entity used by a task. • A resource can be an I/O device such as a
printer, a keyboard, a display, or a variable, a structure, an array, etc.
![Page 25: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/25.jpg)
Shared Resource
• A shared resource is a resource that can be used by more than one task. Each task should gain exclusive access to the shared resource to prevent data corruption. This is called Mutual Exclusion
![Page 26: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/26.jpg)
Multitasking
• Multitasking is the process of scheduling and switching the CPU (Central Processing Unit) between several tasks; a single CPU switches its attention between several sequential tasks.
![Page 27: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/27.jpg)
Task
![Page 28: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/28.jpg)
Task
• A task, also called a thread, is a simple program that thinks it has the CPU all to itself.
• The design process for a real-time application involves splitting the work to be done into tasks which are responsible for a portion of the problem.
• Each task is assigned a priority, its own set of CPU registers, and its own stack area
![Page 29: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/29.jpg)
![Page 30: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/30.jpg)
State Diagram• Task states
RunningReadyDormant ISR
Waiting
task create
task delete
task start
task is preempted
interrupt
intExit
task resume
tasksuspend
task delete
task delete
Ready
Running
Blocked
![Page 31: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/31.jpg)
Priority
![Page 32: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/32.jpg)
Task Priority
• Static Priorities:• Task priorities are said to be static when the
priority of each task does not change during the application's execution.
• Each task is thus given a fixed priority at compile time. All the tasks and their timing constraints are known at compile time in a system where priorities are static.
![Page 33: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/33.jpg)
• Dynamic Priorities• Task priorities are said to be dynamic if the
priority of tasks can be changed during the application's execution; each task can change its priority at run-time. This is a desirable feature to have in a real-time kernel to avoid priority inversion problem.
![Page 34: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/34.jpg)
Priority Inversion Problem• Priority inversion is an undesirable situation in
which a higher priority task gets blocked (waits for CPU) for more time than that it is supposed to, by lower priority tasks.
• Example:• Let T1 , T2 , and T3 be the three periodic tasks with
decreasing order of priorities. • Let T1 and T3 share a resource “S”.
![Page 35: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/35.jpg)
![Page 36: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/36.jpg)
• T3-lock on semaphore S enters critical section• T1 ready-preempts T3-T1 tries to lock semaphore S-
locked by T3-T1 is blocked• T2 ready-preempts T3 while T3 is in critical section• T1(high priority) is blocked for longer duration of
time as T2 got executed in between
![Page 37: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/37.jpg)
Priority Inversion -- Example• T3 obtains a lock on the semaphore S and enters its critical
section to use a shared resource.
• T1 becomes ready to run and preempts T3. Then, T1 tries to enter its critical section by first trying to lock S. But, S is already locked by T3 and hence T1 is blocked.
• T2 becomes ready to run. Since only T2 and T3 are ready to run, T2 preempts T3 while T3 is in its critical section.
• Ideally, one would prefer that the highest priority task (T1) be blocked no longer than the time for T3 to complete its critical section. However, the duration of blocking is, in fact, unpredictable because task T2 got executed in between.
![Page 38: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/38.jpg)
• Dynamic Priorities• Task priorities are said to be dynamic if the
priority of tasks can be changed during the application's execution; each task can change its priority at run-time. This is a desirable feature to have in a real-time kernel to avoid priority inversion problem.
![Page 39: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/39.jpg)
Priority Inheritance Protocol• Priority inheritance protocol solves the problem of priority
inversion.
• Under this protocol, if a higher priority task TH is blocked by a lower priority task TL, because TL is currently executing critical section needed by TH, TL temporarily inherits the priority of TH.
• When blocking ceases (i.e., TL exits the critical section), TL resumes its original priority.
• Unfortunately, priority inheritance may lead to deadlock.
![Page 40: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/40.jpg)
![Page 41: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/41.jpg)
Priority Inheritance Protocol – Deadlock• Assume T2 > T1 (i.e., T2 has high priority)
![Page 42: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/42.jpg)
Deadlock
• If task T1 has exclusive access to resource R1 and task T2 has exclusive access to resource R2, then
• if T1 needs exclusive access to R2 and T2 needs exclusive access to R1, neither task can continue. They are deadlocked. The simplest way to avoid a deadlock is for tasks to:
• a) acquire all resources before proceeding,• b) acquire the resources in the same order, and• c) release the resources in the reverse order.
![Page 43: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/43.jpg)
Deadlock
• Avoiding deadlock conditions requires careful attention to the way in which multiple tasks share semaphores and other RTOS resources.
• Deadlock conditions don't always show up easily during software testing.
![Page 44: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/44.jpg)
Memory• The fundamental requirement for memory in a
real-time system is that its access time should be bound (or in other words predictable).
• As a direct consequence, the use of demand paging (swapping pages to disk) is prohibited for real-time processes.
• This is why systems providing a virtual memory mechanism should have the ability to “lock” the process into the main memory so swapping will not occur (Swapping is a mechanism that cannot be made predictable.)
![Page 45: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/45.jpg)
Memory• Virtual memory is another technique that cannot be
made predictable, and therefore should notbe used in real-time systems.
• A simple solution is to allocate all memory for all objects you need during the life of the system and never de-allocate them.
• Another solution is always to allocate and de-allocate blocks of memory with a fixed size.
(Introducing internal fragmentation = never using some parts of memory internal to the blocks).
![Page 46: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/46.jpg)
Memory Management
![Page 47: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/47.jpg)
Memory• Static memory allocation
– All memory allocated to each process at system startup
• Expensive• Desirable solution for Hard-RT systems
• Dynamic memory allocation– Memory requests are made at runtime
• Should know what to do upon allocation failure• Some RTOSs support a timeout function
![Page 48: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/48.jpg)
Memory
• In HRT static memory allocation is used.
• In SRT you have the option of dynamic memory allocation, no virtual memory, and no compaction.
• In non-RT you may want virtual memory and compaction.
![Page 49: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/49.jpg)
Memory Requirements
• A minimal kernel for an 8-bit CPU that provides only scheduling, context switching, semaphore management, delays, and timeouts should require about 1 to 3 Kbytes of code space.
• Code space needed when a kernel is used.• Application code size + Kernel code size
![Page 50: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/50.jpg)
Context Switch (or Task Switch)
• When a multitasking kernel decides to run a different task, it simply saves the current task's context (CPU registers) in the current task's context storage area – it’s stack Once this operation is performed, the new task's
• context is restored from its storage area and then resumes execution of the new task's code. This process is called a context switch or a task switch.
![Page 51: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/51.jpg)
Context Switches
![Page 52: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/52.jpg)
Example PIC24F
![Page 53: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/53.jpg)
Kernel
• The kernel is the part of a multitasking system responsible for the management of tasks (that is, for managing the CPU's time) and communication between tasks. The fundamental service provided by the kernel is context switching.
• The use of a real-time kernel will generally simplify the design of systems by allowing the application to be divided into multiple tasks managed by the kernel.
• A kernel can allow you to make better use of your CPU by providing you with indispensible services such as
• semaphore management, queues, time delays etc.
![Page 54: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/54.jpg)
Scheduling
![Page 55: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/55.jpg)
Scheduling Tasks
![Page 56: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/56.jpg)
Scheduler
• The scheduler, also called the dispatcher, is the part of the kernel responsible for determining which task will run next.
• Most real-time kernels are priority based. Each task is assigned a priority based on its importance. The priority for each task is application specific. In a priority-based kernel, control of the CPU will always be given to the highest priority task ready-to-run. When the highest-priority task gets the CPU, however, is determined by the type of kernel used.
• There are two types of priority-based kernels: • non-preemptive and preemptive.
![Page 57: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/57.jpg)
Non-Preemptive Kernel
• Non-preemptive kernels require that each task does something to explicitly give up control of the CPU.
• Non-preemptive scheduling is also called• cooperative multitasking, tasks cooperate
with each other to share the CPU. This is done to maintain illusion of concurrency.
![Page 58: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/58.jpg)
Co-Operative MultitaskingTasks execute until they pause or yield Tasks execute until they pause or yield- Must be written to explicitly yield.- System performance can be optimized Tasks can be given priorities
![Page 59: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/59.jpg)
Pre-emptive Multitasking
![Page 60: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/60.jpg)
Priority
![Page 61: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/61.jpg)
Round Round-Robin Scheduling
![Page 62: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/62.jpg)
Round Robin Scheduling
• When two or more tasks have the same priority, the kernel will allow one task to run for a predetermined amount of time, called a quantum, and then selects another task. This is also called time slicing. The kernel gives control to the next task in line if:
• a) the current task doesn't have any work to do during its time slice or
• b) the current task completes before the end of its time slice.
![Page 63: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/63.jpg)
Priority-based Pre-emptive emptive Multi-tasking
![Page 64: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/64.jpg)
Non-Preemptive Kernel
• Asynchronous events are still handled by• ISRs• An ISR can make a higher priority task ready
to run, but the ISR always returns to the interrupted task. The new higher priority task will gain control of the CPU only when the current task gives up the CPU.
![Page 65: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/65.jpg)
![Page 66: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/66.jpg)
advantages of a non-preemptive kernel
• interrupt latency is typically low• lesser need to guard shared data through the
use of semaphores.• Each task owns the CPU and you don't have to
fear that a task will be preempted.• semaphores should still be used. Shared I/O
devices may still require the use of mutual exclusion semaphores; for example, a task might still need exclusive access to a printer.
![Page 67: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/67.jpg)
drawback of a non-preemptive kernel
• responsiveness• A higher priority task that has been made• ready to run may have to wait a long time to
run, because the current task must give up the CPU when it is ready.
![Page 68: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/68.jpg)
Preemptive Kernel
• A preemptive kernel is used when system responsiveness is important
• The highest priority task ready to run is always given control of the CPU.
• If an ISR makes a higher priority task ready, when the ISR completes, the interrupted task is suspended and the new higher priority task is resumed.
• With a preemptive kernel, execution of the highest priority task is deterministic
![Page 69: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/69.jpg)
![Page 70: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/70.jpg)
Reentrancy
• A reentrant function is a function that can be used by more than one task without fear of data corruption. A reentrant
• function can be interrupted at any time and resumed at a later time without loss of data. Reentrant functions either use
• local variables (i.e., CPU registers or variables on the stack) or protect data when global variables are used.
![Page 71: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/71.jpg)
copies of the arguments to strcpy() are placed on the task's stack, strcpy() can be invoked by multiple tasks without fear that the tasks will corrupt each other's pointers.
![Page 72: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/72.jpg)
Non-Reentrant function
![Page 73: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/73.jpg)
![Page 74: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/74.jpg)
• The programmer intended to make swap() usable by any task. Figure 2-6 shows what could happen if a low priority task is interrupted while swap() F2-6(1) is executing. Note that at this point Temp contains 1. The ISR makes the higher priority task ready to run, and thus, at the completion of the ISR F2-6(2), the kernel (assuming µC/OS-II) is invoked to switch to this task F2-6(3). The high priority task sets Temp to 3 and swaps the contents of its variables correctly (that is, z is 4 and t is 3). The high priority task eventually relinquishes control to the low priority task
• F2-6(4) by calling a kernel service to delay itself for 1 clock tick (described later). The lower priority task is thus
• resumed F2-6(5). Note that at this point, Temp is still set to 3! When the low-priority task resumes execution, it sets y
• to 3 instead of 1.
![Page 75: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/75.jpg)
Assigning Task Priorities
• An interesting technique called Rate Monotonic Scheduling (RMS) has been established to assign task priorities based on how often tasks execute. Simply put, tasks with the highest rate of execution are given the highest priority.
![Page 76: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/76.jpg)
RMS makes a number of assumptions
• 1. All tasks are periodic (they occur at regular intervals).
• 2. Tasks do not synchronize with one another, share resources, or exchange data.
• 3. The CPU must always execute the highest priority task that is ready to run. In other words, preemptive scheduling must be used.
![Page 77: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/77.jpg)
Rate Monotonic Scheduling• Promising method of scheduling for
periodic tasks
• Assign priorities based on their periods
• Task with the shortest period has the highest priority
![Page 78: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/78.jpg)
Periodic Task Timing
![Page 79: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/79.jpg)
RMS • Suppose we have n tasks with a fixed
period and computation time, so to meet all possible deadlines following expression must hold:
• Sum of the processor utilization cannot exceed 1 that is the maximum utilization of processor, this is infact for a perfect scheduling algorithm.
• For practical reasons this bound is less than 1, for RMS it is:--- + .... + --- < U(n) = n(2 - 1)
C1 Cn 1/ n
T1 Tn
![Page 80: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/80.jpg)
RMS
Theorem (RMA Bound) Any set of n periodic tasks is RM-schedulable if the processor utilization, U, is no greater
than 1
2 1nn
.
This means that whenever U is at or below the given utilization bound, a schedule can be constructed with RM. In the limit when the number of tasks n , the maximum utilization limit is
1
lim 2 1 ln 2 0.69n
nn
![Page 81: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/81.jpg)
RMS Example• Example: • Task P1: C1 = 20, T1 = 100,
U1 = 20/100 = 0.2• Task P2: C2 = 40, T2 = 150,
U2 = 40/150 = 0.267• Task P3: C3 = 100, T3 = 350,
U3 = 100/350 = 0.286• Total Utilization: =0.753
![Page 82: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/82.jpg)
Deadline Scheduling• Real time is not just about sheer speed
• It is the completion of a task at a specified time, not early neither late.
![Page 83: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/83.jpg)
Types of RT Tasks
• An aperiodic real time task is the one which has a dead line by that it must finish or start, or may have constraints on both start and finish.
• A periodic real time task is the one which has a dead line once every period ”T” or exactly ”T” units apart
![Page 84: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/84.jpg)
Example Periodic Tasks• Consider an example of scheduling
periodic tasks with completion deadlines, system collects and processes data from two sensors A and B.
• Deadline for A to collect data is 20 ms and 50 ms for B.
• A takes 10 ms to process the sample including operating system overhead and B takes 25 ms.
![Page 85: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/85.jpg)
Example Periodic Tasks
![Page 86: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/86.jpg)
Example Periodic Tasks
![Page 87: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/87.jpg)
Example Aperiodic tasks
![Page 88: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/88.jpg)
Example Aperiodic tasks
![Page 89: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/89.jpg)
Mutual Exclusion
• The most common methods to obtain exclusive access to shared resources are:
• a) Disabling interrupts• b) Test-And-Set• c) Disabling scheduling• d) Using semaphores
![Page 90: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/90.jpg)
Synchronization
• A task can be synchronized with an ISR, or another task when no data is being exchanged, by using a semaphore
• A task initiates an I/O operation and then waits for the semaphore.
• When the I/O operation is complete, an ISR (or another task) signals the semaphore and the task is resumed.
• the semaphore is drawn as a flag, to indicate that it is used to signal the occurrence of an event
![Page 91: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/91.jpg)
![Page 92: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/92.jpg)
![Page 93: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/93.jpg)
• When the first task reaches a certain point, it signals the second task and then waits for a signal from the second task.
• Similarly, when the second task reaches a certain point, it signals the first task and then waits for a signal from the first task At this point, both tasks are synchronized with each other.
![Page 94: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/94.jpg)
![Page 95: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/95.jpg)
Events
![Page 96: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/96.jpg)
Event Flags
• Event flags are used when a task needs to synchronize with the occurrence of multiple events.
• The task can be synchronized when any of the events have occurred. This is called disjunctive synchronization (logical OR).
• A task can also be synchronized when all events have occurred. This is called conjunctive synchronization (logical AND).
![Page 97: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/97.jpg)
![Page 98: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/98.jpg)
Intertask Communication
• Message queues• Pipes• Fifos• Mailboxes• Semaphore• Shared memory
![Page 99: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/99.jpg)
Inter-task Communication
![Page 100: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/100.jpg)
Interrupts
• An interrupt is a hardware mechanism used to inform the CPU that an asynchronous event has occurred. When an interrupt is recognized, the CPU saves part (or all) of its context (i.e. registers) and jumps to a special subroutine called an Interrupt Service Routine, or ISR.
![Page 101: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/101.jpg)
Interrupts• RT systems respond to external events
– External events are translated by the hardware and interrupts are introduced to the system
• Interrupt Service Routines (ISRs) handle system interrupts– May be stand alone or part of a device driver
• RTOSs should allow lower level ISRs to be preempted by higher lever ISRs
– ISRs must be completed as quickly as possible
![Page 102: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/102.jpg)
Interrupts
![Page 103: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/103.jpg)
Interrupts– Interrupt Dispatch Time
• Time the hardware needs to bring the interrupt to the processor
– Interrupt Routine• ISR execution time
– Other Interrupt• Time needed for managing each simultaneous pending
interrupt
– Pre-emption• Time needed to execute critical code during which no pre-
emption may happen
![Page 104: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/104.jpg)
Interrupts– Scheduling
• Time needed to make the decision on which thread to run
– Context Switch• Time to switch from one context to another
– Return from System Call• Extra time needed when the interrupt occurred while a system
call was being executed
• System calls cause software interrupts (SWIs)– Portable Operating System Interface (POSIX) defines the
syntax of many of the library calls that execute the SWIs.
![Page 105: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/105.jpg)
Interrupt Latency
• the most important specification of a real-time kernel is the amount of time interrupts are disabled.
• All real-time systems disable interrupts to manipulate critical sections of code and re-enable interrupts when the critical
• section has executed. The longer interrupts are disabled, the higher the interrupt latency
![Page 106: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/106.jpg)
Interrupt latency.
• Maximum amount of time interrupts are disabled
• +• Time to start executing the first instruction in
the ISR
![Page 107: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/107.jpg)
Interrupt Response
• Interrupt response is defined as the time between the reception of the interrupt and the start of the user code which will handle the interrupt. The interrupt response time accounts for all the overhead involved in handling an interrupt.
• Typically, the processor's context (CPU registers) is saved on the stack before the user code is executed.
![Page 108: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/108.jpg)
Interrupt Response
• Interrupt latency• +• Time to save the CPU's context
![Page 109: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/109.jpg)
Interrupt response, Non-preemptive kernel.
• For a non-preemptive kernel, the user ISR code is executed immediately after the processor's context is saved. The
• response time to an interrupt for a non-preemptive kernel is
• Interrupt latency • +• Time to save the CPU's context
![Page 110: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/110.jpg)
Interrupt response, Preemptive kernel.
• For a preemptive kernel, a special function provided by the kernel needs to be called. This function notifies the kernel that an ISR is in progress and allows the kernel to keep track of interrupt nesting.
![Page 111: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/111.jpg)
Interrupt response, Preemptive kernel
• Interrupt latency• +• Time to save the CPU's context • +• Execution time of the kernel ISR entry
function
![Page 112: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/112.jpg)
Interrupt Recovery
• Interrupt recovery is defined as the time required for the processor to return to the interrupted code.
![Page 113: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/113.jpg)
Interrupt recovery, Non-preemptive kernel.
• Time to restore the CPU's context • +• Time to execute the return from interrupt
instruction
![Page 114: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/114.jpg)
Interrupt recovery, preemptive kernel.
• Time to determine if a higher priority task is ready
• +• Time to restore the CPU's context of the
highest priority task • +• Time to execute the return from interrupt
instruction
![Page 115: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/115.jpg)
Interrupt Latency, Response, and Recovery
![Page 116: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/116.jpg)
Interrupt latency, response, and recovery(Preemptive kernel)
![Page 117: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/117.jpg)
Non-Maskable Interrupts (NMIs)
• Interrupt latency for an NMI.– Time to execute longest instruction +– Time to start executing the NMI ISR
• Interrupt response for an NMI.– Interrupt latency +– Time to save the CPU's context
• Interrupt recovery of an NMI.– Time to restore the CPU's context +– Time to execute the return from interrupt instruction
![Page 118: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/118.jpg)
use this feature to pass parameters (i.e. larger variables) to and from the ISR and a task.
![Page 119: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/119.jpg)
The total RAM required if the kernel does not support a separate interrupt stack
• Data space needed when a kernel is used.• Application code requirements • +• Data space (i.e. RAM) needed by the kernel • +• SUM(task stacks + MAX(ISR nesting))
![Page 120: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/120.jpg)
the kernel supports a separate stack for interrupts
• Data space needed when a kernel is used.• Application code requirements• +• Data space (i.e. RAM) needed by the kernel • +• SUM(task stacks) • +• MAX(ISR nesting)
![Page 121: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/121.jpg)
To reduce the amount of RAM needed in an application, carefulness about how you
use each task's stack for• a) large arrays and structures declared locally• to functions and ISRs• b) function (i.e., subroutine) nesting• c) interrupt nesting• d) library functions stack usage• e) function calls with many arguments
![Page 122: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/122.jpg)
• This especially the case when running an RTOS and wanting to share relatively small amounts of RAM amongst two or three or four tasks' stacks.
![Page 123: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/123.jpg)
• a multitasking system will require more code space (ROM) and data space (RAM)
• The amount of extra ROM depends only on the size of the kernel, and the amount of
• RAM depends on the number of tasks in your system.
![Page 124: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/124.jpg)
Advantages and Disadvantages of Real-Time Kernels
• An RTOS allow you to make better use of• your resources by providing you with precious
services such as semaphores, mailboxes, queues, time delays, timeouts
• With a preemptive RTOS, all time-critical events are handled as quickly and as efficiently as possible.
• There are currently about 80+ RTOS vendors. Products are available for 8-, 16-, and 32-bit microprocessors.
• Disdvantage: cost
![Page 125: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/125.jpg)
Real-Time Systems Summary
![Page 126: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/126.jpg)
When is an RTOS appropriate?
![Page 127: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/127.jpg)
Why an RTOS?
![Page 128: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/128.jpg)
Why an RTOS?
![Page 129: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/129.jpg)
Concepts and Terminology
![Page 130: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/130.jpg)
Memory Management
![Page 131: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/131.jpg)
RTOS Configuration
![Page 132: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/132.jpg)
Example Configuration
![Page 133: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/133.jpg)
How to Choose/Factors influencing
![Page 134: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/134.jpg)
RTOS Vendors
![Page 135: RTOS Concepts](https://reader033.vdocuments.us/reader033/viewer/2022061123/5476a6abb4af9f02148b4656/html5/thumbnails/135.jpg)
RTOS Vendor Websites