Professional Documents
Culture Documents
Real Time Operating Systems: - RTOS
Real Time Operating Systems: - RTOS
Outline
Basic concepts Real Time Operating Systems RTOS Design considerations Tasks and scheduler in RTOS Tasks communication and synchronization Example POSIX Standard Conclusions and references
28.06.06
Outline
Basic concepts Real Time Operating Systems RTOS Design considerations Tasks and scheduler in RTOS Tasks communication and synchronization Example POSIX Standard Conclusions and references
28.06.06
Basic concepts
28.06.06
Provide a set of basic services to interact with the hardware Typically a library or set of libraries Tends to be small and highly optimized Might be (or not) other high level services on top
28.06.06
Resource allocation Task management, scheduling and interaction Hardware abstraction I/O interface Time control Error handling
6
28.06.06
OS classification
Depending on...
response time: batch / interactive / real-time users: multi / single user execution mode: multi / single task others: embedded, portable, standard compliant (i.e. POSIX).
28.06.06
Outline
Basic concepts Real Time Operating Systems RTOS Design considerations Tasks and scheduler in RTOS Tasks communication and synchronization Example POSIX Standard Conclusions and references
28.06.06
28.06.06
Same basic structure than a regular OS, but... In addition it provides mechanisms to allow real time scheduling of tasks:
No memory swap (virtual memory) is typically allowed Special hw interaction. Ad-hoc drivers (!)
28.06.06
10
Task priority policy Can be dynamic or static Inter-task communication and synchronization
semaphores, mailboxes, message queues, buffers, monitors, ... Hardware access is done at a lower level
28.06.06
11
Outline
Basic concepts Real Time Operating Systems RTOS Design considerations Tasks and scheduler in RTOS Tasks communication and synchronization Example POSIX Standard Conclusions and references
28.06.06
12
28.06.06
13
Memory Management
fragmentation
swap / virtual memory (depends on HD) 1) No memory handling at all 2) Limited memory management
Solution
28.06.06
14
Only certain blocs of fixed size of dynamic memory can be required by tasks. Blocks are stored in a free buffer queue
28.06.06
15
Advantages:
No fragmentation
No need of garbage collection (!) Easily implemented as constant functions (deterministic behavior!)
Disadvantages:
28.06.06
I/O Management
Unpredictable hw behavior:
ie: disc segmentation, hw failure, network collision, random delays (CSMA/CD), ... ie: printf() is not appropriated for RT applications New reentrant functions provided, ie:
void safe_printf(char *txt) { get_semaphore(s); printf(txt); release_semaphore(s); }
28.06.06
17
CPU & RAM are controllable Other devices are commonly unpredictable
Depends on header position, latency, spinning speed,... Development of ad-hoc drivers (!) Assembler and low level code = platform dependent
28.06.06
18
Time handling
Task scheduling
Time resolution
28.06.06
Limited resources
micro controllers low power CPUs ... Size restrictions Limited power consumption ...
RTOS - R.Serna Oliver - TU Kaiserslautern 20
28.06.06
Outline
Basic concepts Real Time Operating Systems RTOS Design considerations Tasks and scheduler in RTOS Tasks communication and synchronization Example POSIX Standard Conclusions and references
28.06.06
21
What is a task for us (users/programmers)? What is a task for the OS? Task states Scheduler
28.06.06
22
28.06.06
TCB
Task ID Task Priority Program Counter (PC) @ Task Memory @ Registers Task Status Waiting resource Next TCB (...)
28.06.06
24
Task States
: actually running : Ready to be dispatched : blocked by another task or resource : blocked by time
EXECUTING
Dispatch
Scheduler
Scheduler
select next task to be executed maintain queues (order by?) maintain priorities (static? dynamic?) Kernel executes periodically Kernel executes only when something happens
RTOS - R.Serna Oliver - TU Kaiserslautern 26
Time triggered
Event triggered
28.06.06
Outline
Basic concepts Real Time Operating Systems RTOS Design considerations Tasks and scheduler in RTOS Tasks communication and synchronization Example POSIX Standard Conclusions and references
28.06.06
27
28.06.06
28
sharing data
meeting points
i.e. wait for an intermediate result i.e. wait for the end of another task
RTOS - R.Serna Oliver - TU Kaiserslautern 29
28.06.06
Communication
Mailboxes:
message_send(id_mailbox, message);
m= message_read(id_mailbox);
28.06.06
30
Communication (II)
Message queues:
Same concept as Mailboxes but more than one message can be stored (fixed capacity)
Sender can send up to N messages before being blocked Reader gets messages in FIFO order
28.06.06
31
Communication
Mutex
mutex_lock()
blocks execution until mutex is unlocked then locks mutex unlock mutex
mutex_unlock()
Only one task can enter a section protected by mutex. optional protocols to avoid priority inversion
RTOS - R.Serna Oliver - TU Kaiserslautern 32
28.06.06
Communication
Example:
Task A
void taskA() { while (1) { mutex_lock(m) id_a = global_id; global_id ++; mutex_unlock(m); ... } }
28.06.06
void taskB() { while (1) { mutex_lock(m) id_b = global_id; global_id ++; mutex_unlock(m); ... } }
RTOS - R.Serna Oliver - TU Kaiserslautern 33
Synchronization
Semaphores
wait_semaphore()
decrement resource counter block execution if counter = 0 increment resource counter if value<=0 one of the blocked tasks continues execution
signal_semaphore()
resource counter
RTOS - R.Serna Oliver - TU Kaiserslautern 34
28.06.06
Synchronization
Example:
Task A
semaphore s = init_semaphore(0); Task B void taskB() { while (1) { ... wait_semaphore(s) get_result(); ... } }
RTOS - R.Serna Oliver - TU Kaiserslautern 35
Outline
Basic concepts Real Time Operating Systems RTOS Design considerations Tasks and scheduler in RTOS Tasks communication and synchronization Example POSIX Standard Conclusions and references
28.06.06
36
Example
28.06.06
37
Example (I)
Task 1 (T1)
void task1(void *param) { while (1) { ... echo(task 1); sleep_until(time+10); } } Priority = 1 Period = 10 Execution time = 5
28.06.06
Task 2 (T2)
void task2(void *param) { while (1) { ... echo(task 2); sleep_until(time+50); } } Priority = 2 Period = 50 Execution time = 10
38
Example (II)
void main() {
Task Creation (main)
char *mem1[100], *mem2[100]; int t1, t2; /* create_task(@code, @param, @mem, priority */ t1 = create_task(*task1, NULL, mem1, 1); t2 = create_task(*task2, NULL, mem2, 2); /* OS starts executing */ os_start(); } /* end */
28.06.06
39
Example (III)
Blocked
Waiting
T2 T1
0 5 10 15 20 25 30 35 40 45 50
Example (IV)
Blocked
Waiting
PC, stack, memory and CPU registers are updated from the TCB of T1
RTOS - R.Serna Oliver - TU Kaiserslautern 41
28.06.06
Example (V)
Blocked
T1 reaches the end of the loop. Sleeps until time+10 T2 becomes the Running task
Waiting T1 T2 Ready T1
0 5 10
15
20
25
30
35
40
45
50
TCB of T1 is updated with PC, stack, memory and CPU registers. PC, stack, memory and CPU registers are updated from the TCB of T2
RTOS - R.Serna Oliver - TU Kaiserslautern 42
28.06.06
Example (VI)
Blocked
Waiting
T1
0 5 10 15 20 25 30 35 40 45 50
28.06.06
43
Example (VII)
Blocked
T1 preempts T2 since it has higher priority Execution of T2 is preempted by a higher priority task
T2
Waiting
T1
0 5 10 15 20 25 30 35 40 45 50
TCB of T2 is updated with PC, stack, memory and CPU registers. PC, stack, memory and CPU registers are updated from the TCB of T1
RTOS - R.Serna Oliver - TU Kaiserslautern 44
28.06.06
Example (VIII)
Blocked
T1 reaches again the end of the loop and finishes its execution T2 can now continue its execution
T2
Waiting T1 Ready T1
0 5 10 15
20
25
30
35
40
45
50
TCB of T2 is updated with PC, stack, memory and CPU registers. PC, stack, memory and CPU registers are updated from the TCB of T1
RTOS - R.Serna Oliver - TU Kaiserslautern 45
28.06.06
Example (IX)
Blocked
T2 reaches the end of its loop and finishes its execution T1 executes again (after moving from Waiting to Ready)
Waiting T2 T2 Ready T1
0 5 10 15 20 25 30 35 40 45 50
TCB of T2 is updated with PC, stack, memory and CPU registers. PC, stack, memory and CPU registers are updated from the TCB of T1
RTOS - R.Serna Oliver - TU Kaiserslautern 46
28.06.06
Example (X)
Blocked
T1 reaches the end of its loop and finishes its execution No task is ready to be executed!!
Waiting T2 T1 T2 Ready T1
0 5 10 15 20 25 30 35 40 45 50
?
RTOS - R.Serna Oliver - TU Kaiserslautern 47
Example (XI)
Blocked
A special task IDLE is scheduled with the lowest priority in order to get the CPU when no other task is ready to execute
Waiting T2 T1 T2 Ready
Idle
Task IDLE
0 5 10 15 20 25 30 35 40 45 50
T1
Example (XII)
Blocked
The Idle task executes until any other task is available in the Ready queue
Running
Idle
Again PC, stack and memory are replaced like with any other task
RTOS - R.Serna Oliver - TU Kaiserslautern 49
28.06.06
Example (end)
Blocked
Execution continues...
Waiting Idle ... T2 Ready ... Running ... PC = ... Stack = ... Mem = ...
RTOS - R.Serna Oliver - TU Kaiserslautern 50
...
0 5 10 15 20 25 30 35 40 45 50
T1
28.06.06
Idle task is an infinite loop that gains CPU access whenever there is nothing to do. RTOS are preemptive since they kick out the current running task if there is a higher priority task ready to execute. Scheduling is based on selecting the appropriated task from (ordered) queues. Task context is saved on its own TCB and restored in every new execution.
RTOS - R.Serna Oliver - TU Kaiserslautern 51
28.06.06
RTOS
T2
T1
This is where the TCB and CPU registers are updated and. queues reordered (!)
RTOS - R.Serna Oliver - TU Kaiserslautern 52
28.06.06
Blocked X
resource
MUTEX, mailbox, etc...
BLOCKING RESOURCE
resource is free
28.06.06
53
Outline
Basic concepts Real Time Operating Systems RTOS Design considerations Tasks and scheduler in RTOS Tasks communication and synchronization Example Real RTOS POSIX Standard Conclusions and references
RTOS - R.Serna Oliver - TU Kaiserslautern 54
28.06.06
POSIX Standard
Overview RT Extensions
28.06.06
55
POSIX: Overview
Set of standards
POSIX 4
28.06.06
56
POSIX: RT Extensions
3 policies
Virtual Memory
28.06.06
57
POSIX: RT Extensions
Process Synchronization
Semaphores
Shared Memory
Signals
28.06.06
POSIX: RT Extensions
Message queues
Time
resolution = nanoseconds programmed to a certain interval expiration sends a signal to the owner process
RTOS - R.Serna Oliver - TU Kaiserslautern 59
Timers
28.06.06
POSIX: RT Extensions
Asynchronous I/O
No wait for I/O operation A signal is sent when done Threads Timeouts
Other extensions:
28.06.06
60
Outline
Basic concepts Real Time Operating Systems RTOS Design considerations Tasks and scheduler in RTOS Tasks communication and synchronization Example Real RTOS POSIX Standard Conclusions and references
RTOS - R.Serna Oliver - TU Kaiserslautern 61
28.06.06
Conclusions
Limited by:
Tasks
Scheduler
28.06.06
References
Operating Systems, William Stallings, ed. Prentince Hall Diseno de Sistemas en Tiempo Real, Guillem Bernat, Albert Llamosi, Ramon Puijaner, UIB Missconceptions about Real-Time Computing, John A. Stankovic POSIX in Real-Time, Kevin M. Obenland
http://www.embedded.com/story/OEG20010312S0073
http://www.onesmartclick.com/rtos/rtos.html
28.06.06
63
The End
Thank you!
28.06.06
64