12 OperatingSystem Scheduling
12 OperatingSystem Scheduling
Embedded OS
Thread 1
Thread 2
Threads have own CPU register values,
but cohabit same memory space, so they Thread 3
could affect data of another thread.
•a process may have multiple threads
•threads may run on separate CPU cores
Typical process/task activation
records (task control blocks)
Task ID
Task state (running, ready, blocked)
Task priority
Task starting address
Task stack
Task CPU registers
Task data pointer
Task time (ticks)
Process state
P3 ready t=18
P2 ready t=0 P1 ready t=15
P2 P1 P2 P3
0 10 20 30 40 50 60
time
Process initiation disciplines
Periodic process: executes on (almost) every period.
Aperiodic process: executes on demand.
Tresponse = ∑N T
i<n
i Ti + max[Tn , Tn −1 ,...] + TTDn + Tcir + ∑T
int, srv
∑T + ∑T
{Task2(); }
Ttime − slice > Ti int, srv else if (T3_Ready)
i<n {Task3(); }
Can make all execution times k*Tslice }
Ttime − slice ≤ gcd(TP1 , TP 2 ,..., TPn )
greatest common divisor
P2 period
P1 period
P1 P2 P1 P1
0 5 10
time
RMS example (Ex. 6-3)
time
0 2 4 6 8 10 12
RMS example 2 (Ex. 6-4)
Case 1
Case 2
Rate-monotonic analysis
Response time: time required to finish process.
Critical instant: scheduling state that gives worst response
time.
Critical instant for any process occurs when it is ready and all
higher-priority processes are also ready to execute.
Consider whether the low-priority process can meet its
deadline
Critical instant
interfering processes
P1 P1 P1 P1 P1
P2 P2 P2
P3 P3
critical
instant
P4
CPU utilization for RMS
Task period τi
Process Pi
Task computation time Ti
CPU utilization = 1/3 + 1/4 + 2/5 = .98333333 (too high for RMS)
Time Running Deadlines Time Running Deadlines
0 P1 10 P2
1 P2 11 P3 P1,P2
2 P3 P1 12 P3
3 P3 P2 13 P1
4 P1 P3 14 P2 P1,P3
5 P2 P1 15 P1 P2
6 P1 16 P2
7 P3 P2 17 P3 P1
8 P3 P1 18 P3
9 P1 P3 19 P1 P2,P3
EDF analysis
EDF can use 100% of CPU.
But EDF may miss a deadline.
EDF implementation
More complex than RMS.
On each timer interrupt:
compute time to deadline;
choose process closest to deadline.
Generally considered too expensive to use in practice
due to changing priorities.
code PC
process 1
data
registers
process 2
activation
record
... CPU
memory
Context-switching time
Non-zero context switch time can push limits of a tight
schedule.
Hard to calculate effects---depends on order of context
switches.
In practice, OS context switch overhead is small.
Copy all registers to activation record, keeping proper return
value for PC.
Copy new activation record into CPU state.
How does the program that copies the context keep its own
context?
Context switching in ARM