Rtos PDF
Rtos PDF
Rtos PDF
OS kernel
Hardware
1 2
Processor cycles very expensive at the time Fairness, primary goal of timesharing schedulers
Jobs involved reading, writing data to/from tapes Let no one process consume all the resources
Cycles were being spent waiting for the tape! Make sure every process gets equal running time
1
Real-Time Is Not Fair Do We Need OS for RTS?
Main goal of an RTOS scheduler: Not always
meeting timing constraints e.g. deadlines Simplest approach: cyclic executive
If you have five homework assignments and only one
is due in an hour, you work on that one loop
Fairness does not help you meet deadlines do part of task 1
do part of task 2
do part of task 3
end loop
Interrupt handling
Fast process/thread switch
Exception handling
Fast interrupt response
Process Synchronization (IPC)
User control over OS policies
Process schedulling
Mainly scheduling, many priority levels
Memory support (especially embedded)
Reliability
12
2
Process, Thread and Task Basic functions of RTOS kernel
13 14
Task = thread (lightweight process) Each task a triplet: (execution time, period, deadline)
Usually, deadline = period
A sequential program in execution
Can be initiated any time during the period
It may communicate with other tasks
It may use system resources such as memory blocks Execution
Initiation Deadline
We may have timing constraints for tasks time
Time
Period
17
3
Example: Fly-by-wire Avionics:
Task Classification (1) Hard real-time system with multi-rate tasks
19
21 22
Idling
timeout delay
preemption preemption
Activate Terminate Activate Terminate
Ready Dispatch
Runnig Ready Dispatch
Runnig
Blocked Blocked
23 24
4
Task states (Ada95) TCB (Task Control Block)
Id
Task state (e.g. Idling)
Task type (hard, soft, background ...)
25 26
27 28
Creating an RT task, it has to get the memory without delay: this is Interrupt handling
difficult because memory has to be allocated and a lot of data Memory management
structures, code seqment must be copied/initialized
Exception handling
The memory blocks for RT tasks must be locked in main memoery Task synchronization
to avoid access latencies due to swapping Task scheduling
Time management
Changing run-time priorities is dangerous: it may change the run-
time behaviour and predictability of the whole system
29 30
5
Interrupts Handling an Interrupt
1. Normal program
Interrupt: environmental event that demands attention execution
Example: byte arrived interrupt on serial channel 3. Processor state
saved 4. Interrupt routine
2. Interrupt runs
Interrupt routine: piece of code executed in response to an
occurs
interrupt
6. Processor state
restored
5. Interrupt routine
terminates
7. Normal
program
execution
resumes
Asynchronous (or hardware interrupt) by hardware event (timer, network card ) the
interrupt handler as a separated task in a different context.
Synchronous (or software interrupt, or a trap) by software instruction (swi in ARM, int
Copy peripheral data into a buffer in Intel 80x86), a divide by zero, a memory segmentation fault, etc. The interrupt
handler runs in the context of the interrupting task
Indicate to other code that data has arrived Interrupt latency
Acknowledge the interrupt (tell hardware) The time delay between the arrival of interrupt and the start of corresponding ISR.
Modern processors with multiple levels of caches and instruction pipelines that need to
be reset before ISR can start might result in longer latency.
The ISR of a lower-priority interrupt may be blocked by the ISR of a high-priority
Longer reaction to interrupt performed outside interrupt routine
34
35 36
6
Basic functions of RT OS Exception handling
Standard techniques:
Task synchronization System calls with error code
Task scheduling Watch dog
Time management Fault-tolerance (later)
However, difficult to know all senarios
Missing one possible case may result in disaster
This is one reason why we need Modelling and Verification
37 38
Watch-dog Example
A task, that runs (with high priority) in parallel with all others Watch-dog (to monitor whether the application task is alive)
If some condition becomes true, it should react ... Loop
Loop if flag==1 then
begin {
.... next :=system_time;
end flag :=0
until condition }
The condition can be an external event, or some flags else if system_time> next+20s then WARNING;
sleep(100ms)
Normally it is a timeout
end loop
Application-task
flag:=1 ... ... computing something ... ... flag:=1 ..... flag:=1 ....
39 40
a semaphore can be waited for and signaled by any task,
while only the task that has taken a mutex is allowed to release it.
Time management Spinlock: lock mechanism for multi-processor systems,
A task wanting to get spinlock has to get a lock shared by all processors.
CPU scheduling Read/write locks: protect from concurrent write, while allow concurrent
read
Many tasks can get a read lock; but only one task can get a write lock.
Before a task gets the write lock, all read locks have to be released.
Barrier: to synchronize a lot of tasks,
they should wait until all of them have reached a certain barrier.
41 42
7
Task Synchronization IPC: Data exchanging
43 44
45 46
P(scb): V(scb):
Disable-interrupt; Disable-interrupt;
If scb.counter>0 then If not-empty(scb.queue) then
scb.counter - -1; tcb := get-first(scb.queue);
end then tcb.state := ready;
else insert(tcb, ready-queue);
save-context(); save-context();
current-tcb.state := blocked; schedule(); /* dispatch invoked*/
insert(current-tcb, scb.queue); load-context();
dispatch(); end then
load-context(); else scb.counter ++1;
end else end else
Enable-interrupt Enable-interrupt
47 48
8
Advantages with semaphores Exercise/Questions
Get-msg(mbox,receiver,msg)
It can be used to implement other How to implement hand-shaking communication?
synchronization tools V(S1)P(S2)
Monitors, protected data type, bounded buffers, V(S2)P(S1)
mailbox etc Solve the read-write problem
(e.g max 10 readers, and at most 1 writer at a time)
49 50
51 52
Task A with low priority holds S that task C with highest Highest Priority Inheritance
priority is waiting. Non preemption protocol (NPP)
Tast A can not be forced to give up S, but A can be Basic Priority Inheritance Protocol (BIP)
preempted by B because B has higher priority and can run POSIX (RT OS standard) mutexes
without S
Priority Ceiling Protocols (PCP)
So the problem is that A can be preempted by B Immedate Priority Inheritance
Highest Lockers priority Protocol (HLP)
Ada95 (protected object) and POSIX mutexes
Solution 1: no preemption (an easy fix) within CS sections
Solution 2: high As priority when it gets a semaphore shared
with a task with higher priority! So that A can run until it
release S and then gets back its own priority
53 54
9
Basic functions of RT OS Task states
Task mangement
Interrupt handling
Memory management
Idling
Exception handling timeout delay
Task synchronization preemption
Activate Terminate
Task scheduling Ready Dispatch
Runnig
Time management
signal wait
Blocked
55 56
58
Preemption
59 60
10
Priority-based scheduling in RTOS Basic functions of RT OS
61 62
A high resolution hardware timer is programmed to Save the context of the task in execution
interrupt the processor at fixed rate Time interrupt Increment the system time by 1, if current time > system
Each time interrupt is called a system tick (time lifetime, generate a timing error
resolution): Update timers (reduce each counter by 1)
A queue of timers
Activation of periodic tasks in idling state
Normally, the tick can vary in microseconds (depend on hardware)
The tick may (not necessarily) be selected by the user Schedule again - call the scheduler
All time parameters for tasks should be the multiple of the tick Other functions e.g.
Note: the tick may be chosen according to the given task parameters (Remove all tasks terminated -- deallocate data structures e.g TCBs)
System time = 32 bits (Check if any deadline misses for hard tasks, monitoring)
One tick = 1ms: your system can run 50 days
One tick = 20ms: your system can run 1000 days = 2.5 years
load context for the first task in ready queue
One tick = 50ms: your system can run 2500 days= 7 years
63 64
Priority-based scheduling
Memory management !
Application tasks should be programmed to suit ...
Exception handling !
Ability to quickly respond to external interrupts
Task synchronization !
Task scheduling ! Basic mechanisms for process communication and
Time management ! synchronization
Small kernal and fast context switch
Support of a real time clock as an internal time
reference
65 66
11
Existing RTOS: 4 categories RT Linux: an example
Priority based kernel for embbeded applications e.g. OSE, VxWorks, RT-Linux is an operating system, in which a small real-time
QNX, VRTX32, pSOS .... Many of them are commercial kernels kernel co-exists with standard Linux kernel:
Applications should be designed and programmed to suite priority-based The real-time kernel sits between standard Linux kernel and the
scheduling e.g deadlines as priority etc h/w. The standard Linux Kernel sees this RT layer as actual h/w.
The real-time kernel intercepts all hardware interrupts.
Real Time Extensions of existing time-sharing OS e.g. Real time Linux, Only for those RTLinux-related interrupts, the appropriate ISR is run.
Real time NT by e.g locking RT tasks in main memory, assigning All other interrupts are held and passed to the standard Linux kernel as
highest priorities etc software interrupts when the standard Linux kernel runs.
The real-time kernel assigns the lowest priority to the standard
Linux kernel. Thus the realtime tasks will be executed in real-time
Research RT Kernels e.g. SHARK, TinyOS user can create realtime tasks and achieve correct timing for
them by deciding on scheduling algorithms, priorities, execution
Run-time systems for RT programmingn languages e.g. Ada, Erlang, freq, etc.
Real-Time Java ... Realtime tasks are privileged (that is, they have direct access to
hardware), and they do NOT use virtual memory.
67 68
RT Linux Scheduling
Linux contains a dynamic scheduler
RT-Linux allows different schedulers
EDF (Earliest Deadline First)
Rate-monotonic scheduler
Fixed-prioritiy scheduler
69 70
Unpredictable delay
486 allows stable hard real-time operation: Uninterruptible system calls, the use of interrupt disabling, virtual
memory support (context switch may take hundreds of microsecond).
17 nanoseconds timer resolution. Linux Timer resolution is coarse, 10ms
Linux Kernel is Non-preemptible.
6 microseconds interrupt response time (measured RTLinux Real-time Features
on interrupts on the parallel port). Support real-time scheduling: guarantee hard deadlines
Predictable delay (by its small size and limited operations)
High resolution timing functions give
71 72
12