Today's Topic:: To Run A Single Program Is Easy What To Do When Several Programs Run in Parallel?
Today's Topic:: To Run A Single Program Is Easy What To Do When Several Programs Run in Parallel?
Why OS?
Memory areas
Program counters
Scheduling (e.g. one instruction each)
....
Communication/synchronization/semaphors
Device drivers
OS is a program
Application
Program
Application
Program
Application
Program
OS kernel
Hardware
4
Not always
Simplest approach: cyclic executive
loop
do part of task 1
do part of task 2
do part of task 3
end loop
Cyclic Executive
Advantages
Simple implementation
Low overhead
Very predictable
Disadvantages
We need an OS
For convenience
Multitasking and threads
Cheaper to develop large RT systems
Requirements on RTOS
Determinism
Multi-tasking
Real-time
synchronization
Time management
Task mangement
Interrupt handling
Memory management
Task scheduling
Micro-kernel architecture
External
interrupts
Immediate
Interrupt
services
System
calls
Hardware/software
exceptions
Case of
.
.
.
.
.
.
Kernel
Exception handling
Create task
Suspend task
Terminate task
Clock
interrupts
Time services
Create timer
Sleep-timer
Timer-notify
Other system calls
Scheduling
10
Basic functions of RT OS
Time management
Task mangement
Interrupt handling
Memory management
Exception handling
Task synchronization
Task scheduling
11
Time mangement
12
A queue of timers
13
Time management
Task mangement
Interrupt handling
Memory management
Exception handling
Task synchronization
Task scheduling
14
What is a Task?
15
16
17
Execution
time
Deadline
Time
Period
19
20
10
A single system may have both hard and soft real-time tasks. In reality many
systems will have a cost function associated with missing each deadline.
21
Classification of RTSs
value
value
soft deadline
Hard deadline
value
value
On-time deadline
no deadline
22
11
Ready
Running
Waiting/blocked/suspended ...
Idling
Terminated
23
Activate
preemption
Ready
Runnig
Dispatch
signal
Terminate
wait
Blocked
24
12
Idling
timeout
Activate
delay
preemption
Ready
Runnig
Dispatch
signal
Terminate
wait
Blocked
25
Idling
declared
created
timeout
Activate
Sleep
preemption
Ready
Runnig
Dispatch
signal
Terminate
wait
Blocked
26
13
Id
Task state (e.g. Idling)
Task type (hard, soft, background ...)
Priority
Other Task parameters
period
comuting time (if available)
Relative deadline
Absolute deadline
Context pointer
Pointer to program code, data area, stack
Pointer to resources (semaphors etc)
Pointer to other TCBs (preceding, next, waiting queues etc)
27
Basic functions of RT OS
Time management
Task mangement
Interrupt handling
Memory management
Exception handling
Task synchronization
Task scheduling
28
14
Task managment
29
30
15
Basic functions of RT OS
Time management
Task mangement
Interrupt handling
Memory management
Exception handling
Task synchronization
Task scheduling
31
Handling an Interrupt
1. Normal program
execution
2. Interrupt
occurs
3. Processor state
saved
6. Processor state
restored
4. Interrupt routine
runs
5. Interrupt routine
terminates
7. Normal
program
execution
resumes
16
Basic functions of RT OS
Task mangement
Interrupt handling
Memory management
Exception handling
Task synchronization
Task scheduling
Time management
34
17
Memory Management/Protection
Standard methods
Basic functions of RT OS
Time management
Task mangement
Interrupt handling
Memory management
Exception handling
Task synchronization
Task scheduling
36
18
Exception handling
Standard techniques:
Watch-dog
A task, that runs (with high priority) in parallel with all others
If some condition becomes true, it should react ...
Loop
begin
....
end
until condition
38
19
Example
flag:=1 ... ... computing something ... ... flag:=1 ..... flag:=1 ....
39
Basic functions of RT OS
Task mangement
Interrupt handling
Memory management
Exception handling
Task synchronization
Time management
CPU scheduling
40
20
Synchronization primitives
A task wanting to get spinlock has to get a lock shared by all processors.
they should wait until all of them have reached a certain barrier.
41
42
21
Semaphore
Shared variables
Bounded buffers
FIFO
Mailbox
Message passing
Signal
43
a counter
a queue
Tasks waiting
22
45
P(scb):
Disable-interrupt;
If scb.counter>0 then
scb.counter - -1;
end then
else
save-context();
current-tcb.state := blocked;
insert(current-tcb, scb.queue);
dispatch();
load-context();
end else
Enable-interrupt
46
23
V(scb):
Disable-interrupt;
If not-empty(scb.queue) then
tcb := get-first(scb.queue);
tcb.state := ready;
insert(tcb, ready-queue);
save-context();
schedule(); /* dispatch invoked*/
load-context();
end then
else scb.counter ++1;
end else
Enable-interrupt
47
48
24
Exercise/Questions
49
Deadlocks
Loss of mutual exclusion
Blocking tasks with higher priorities (e.g. FIFO)
Priority inversion !
50
25
A gets S by P(S)
C wants S by P(S) and blocked
B is released and preempts A
Now B can run for a long long period .....
A is blocked by B, and C is blocked by A
So C is blocked by B
51
Solution?
26
53
Basic functions of RT OS
Time management
Task mangement
Interrupt handling
Memory management
Exception handling
Task synchronization
Task scheduling
54
27
Task states
Idling
timeout
Activate
delay
preemption
Ready
Runnig
Dispatch
signal
Terminate
wait
Blocked
55
Priority-based Scheduling
28
Scheduling algorithms
Priorities (HPF)
Execution times (SCF)
Deadlines (EDF)
Arrival times (FIFO)
57
58
29
Schedulability
New tasks
scheduling
Dispatching
Running
Termination
Preemption
59
Basic functions of RT OS
Task mangement !
Interrupt handling !
Memory management !
Exception handling !
Task synchronization !
Task scheduling !
Time management !
60
30
61
RT Linux: an example
RT-Linux is an operating system, in which a small real-time
kernel co-exists with standard Linux kernel:
The real-time kernel sits between standard Linux kernel and the
h/w. The standard Linux Kernel sees this RT layer as actual h/w.
The real-time kernel intercepts all hardware interrupts.
62
31
RT Linux
63
Scheduling
64
32
Unpredictable delay
65
33