Rtos
Rtos
Scheduling
Hardware abstraction layer (device drivers) Filesystems Communication We will focus on concurrent, real-time issues
Do I Need One?
Not always Simplest approach: cyclic executive
loop
do part of task 1 do part of task 2
do part of task 3
end loop
Copyright 2001 Stephen A. Edwards All rights reserved
Cyclic Executive
Advantages
Disadvantages
Cant handle sporadic events Everything must operate in lockstep Code must be scheduled manually
Interrupts
Some events cant wait for next loop iteration
A solution: Cyclic executive plus interrupt routines Interrupt: environmental event that demands attention
Handling an Interrupt
1. Normal program execution 2. Interrupt occurs
When interrupt occurs, execute a single user-specified instruction This typically copies peripheral data into a circular buffer No context switch, no environment save, no delay
Drawbacks of CE + Interrupts
Main loop still running in lockstep Programmer responsible for scheduling Scheduling static Sporadic events handled slowly
Cooperative Multitasking
A cheap alternative Non-preemptive Processes responsible for relinquishing control Examples: Original Windows, Macintosh
Programmer had to ensure this was called frequently An errant program would lock up the whole system
Concurrency Provided by OS
Basic philosophy:
Let the operating system handle scheduling, and let the programmer handle function
Submit job & its input Job runs to completion Collect output Submit next job
Processor cycles very expensive at the time Jobs involved reading, writing data to/from tapes
Store multiple batch jobs in memory at once When one is waiting for the tape, run the other one
Let no one process consume all the resources Make sure every process gets equal running time
Priority-based Scheduling
Typical RTOS based on fixed-priority preemptive scheduler Assign each process a priority At any time, scheduler runs highest priority process ready to run Process runs to completion unless preempted
Initiation
Execution time
Deadline
Time Period
GPS
Sensor Stick
Aileron
Elevator Rudder
Joystick 500 Hz
Rudder 1 kHz
1 2
Simply prohibit: Each process has unique priority Time-slice processes at the same priority
Extra context-switch overhead No starvation dangers at that level
Rate-Monotonic Scheduling
Common way to assign priorities Result from Liu & Layland, 1973 (JACM) Simple to understand and implement: Processes with shorter period given higher priority E.g., Period 10 12 15 20 Priority 1 (highest) 2 3 4 (lowest)
2
Would have met the deadline if p2 = (10,30,30), utilization reduced 83% P2 misses first deadline
Copyright 2001 Stephen A. Edwards All rights reserved
Fundamental result:
RMS schedule always exists if U < n (2 1/n 1) Proof based on case analysis (P1 finishes before P2)
76%
69%
Asymptotic bound
If the required processor utilization is under 69%, RMS will give a valid schedule Converse is not true. Instead:
If the required processor utilization is over 69%, RMS might still give a valid schedule, but there is no guarantee
EDF Scheduling
RMS assumes fixed priorities Can you do better with dynamically-chosen priorities? Earliest deadline first: Processes with soonest deadline given highest priority
Earliest deadline first scheduling is efficient: A dynamic priority schedule exists if and only if utilization is no greater than 100%
Priority Inversion
RMS and EDF assume no process interaction Often a gross oversimplification Consider the following scenario:
1
2
Process 1 tries to acquire lock for resource Process 1 preempts Process 2 Process 2 acquires lock on resource Process 2 begins running
Copyright 2001 Stephen A. Edwards All rights reserved
Priority Inversion
Lower-priority process effectively blocks a higherpriority one Lower-priority processs ownership of lock prevents higher-priority process from running Nasty: makes high-priority process runtime unpredictable
Nastier Example
Higher priority process blocked indefinitely
Process 2 delays process 3s release of lock
1 2 3
Process 1 tries to acquire lock and is blocked Process 1 preempts Process 2 Process 2 preempts Process 3 Process 3 acquires lock on resource Process 3 begins running
Copyright 2001 Stephen A. Edwards All rights reserved
Priority Inheritance
Solution to priority inversion Temporarily increase processs priority when it acquires a lock Level to increase: highest priority of any process that might want to acquire same lock
Danger: Low-priority process acquires lock, gets high priority and hogs the processor
Priority Inheritance
Basic rule: low-priority processes should acquire high-priority locks only briefly An example of why concurrent systems are so hard to analyze RMS gives a strong result No equivalent result when locks and priority inheritance is used
Summary
Cyclic executive
Interrupt handlers
Cooperative multitasking
Summary
Preemptive Priority-Based Multitasking
Rate-monotonic analysis
Shorter periods get higher priorities Guaranteed at 69% utilization, may work higher
Summary
Priority Inversion
Low-priority process acquires lock, blocks higherpriority process Priority inheritance temporarily raises process priority Difficult to analyze