






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Clock synchronization · What an RTOS needs · Kernel structures · Resource sharing & semaphores · Scheduling basics
1 Clock
synchronization
keep all clocks counting together
2 What an RTOS
must provide
predictable, scheduling, fast
3 RT kernel
structures
proprietary vs RT-extension
4 Resource
sharing
critical section, semaphores
5 Scheduling
basics
preemptive, priority, round robin
Running theme: PREDICTABLE TIMING — everything here is a step towards knowing how long things take Fig. 1 — Roadmap of the lecture. Every box is a piece of the same puzzle: making timing predictable.
REMEMBER How to use these notes: read the INTUITION boxes first (they give the “why”), look at the diagram, then check the EXAM TIP boxes for what to write in an answer. The last section is a one-page cheat sheet with model answers.
Why many computers must “count time together”
1.1 Why do we need it?
INTUITION Think of a rowing team. Each rower has a private stopwatch. If the watches disagree, “pull on 3!” is pulled at different moments and the boat zig-zags. Synchronization = making everyone’s watch show the same time, within an allowed error.
EXTRA CONTEXT (not from the lecture) UTC is TAI adjusted with occasional leap seconds so it stays aligned with Earth’s rotation; TAI never gets such jumps.
1.2 Two ways to synchronize
no outside reference — nodes agree among themselves
Process A (my clock)
Neighbour B (its clock)
Two flavours:
sync to an outside, authoritative time source
GPS gives BOTH TAI and UTC
Clock 1 Clock 2 Clock 3
resolution ≈ 100 ns (nowadays the trend)
Fig. 2 — Internal synchronization (agree among ourselves) vs external synchronization (agree with an outside reference).
Internal synchronization External synchronization
Idea No outside authority. Clocks are brought into agreement with each other.
All clocks are tied to one outside, trusted time source.
How (a) One master clock, everyone adheres to it; or (b) distributed: collect time from neighbours, then apply corrections.
Use GPS (nowadays the trend). GPS offers both TAI and UTC.
Quality Depends on the layer where it is done (see 1.3). Resolution about 100 ns.
INTUITION Internal sync in daily life: you are unsure of your watch, so you ask a friend “what’s the time?” and adjust your own. External sync: you set your watch from the national time signal.
1.3 How precise? It depends on the layer
allowed time mismatch between clocks (bar length — not to scale)
Application layer 500 μs – 5 ms
Kernel level 10 μs – 100 μs
Communication hardware < 10 μs
External: GPS ≈ 100 ns
looser
tighter
Lower the layer, tighter the clock!
Fig. 3 — Allowed mismatch shrinks as we go down the stack, and is smallest with GPS.
Where the sync is done Precision Why (intuition)
Application layer 500 μs – 5 ms Lots of software (scheduling, buffers) sits between your code and the wire, so delays vary a lot.
Kernel level 10 μs – 100 μs Fewer layers in between, so less random delay.
Communication hardware
< 10 μs The network hardware itself stamps the time: almost no software delay.
External (GPS) ≈ 100 ns Direct from an atomic-time-based source.
Class A: fast & proprietary Class B: RT extension of a standard OS
Disadvantage s
INTUITION Class A = a race car tuned for one track: blindingly fast there, but nobody has proved how it behaves on every other road. Class B = a building with a VIP entrance and a public lobby: if the lobby floods, the VIP side keeps working; but VIPs cannot use the lobby’s shops.
3.3 Inside the RT-extension design (the diagram you must be able to draw)
keeps running even if the other side crashes
the usual PC-style world real-time tasks non-real-time tasks
RT Task 1 RT Task 2
Device driver 1 (dedicated)
Device driver 2 (dedicated)
direct path, no waiting in line
✗ cannot use std-OS services
init bash Chrome
Standard OS kernel part has its OWN scheduler
Std-OS device drivers (controlled by the std OS)
requests are routed through std OS: mutual exclusion, may have to wait
one shared base — both worlds sit on top of it
HARDWARE (CPU, timers, interrupt controller, I/O devices)
interrupts
I/O & interrupts
Fig. 5 — RT-extension architecture (e.g. RT Linux): one kernel, two worlds, two separate paths to hardware.
WATCH OUT The diagram looks as if both sides share the same hardware. For a real embedded system that is not necessary — you would not load the same device with real-time and non-real-time work. The point is only that there are two separate paths from software to hardware.
EXTRA CONTEXT (not from the lecture) In RTLinux-style systems the RT kernel treats the whole standard OS like a low-priority background task, so real-time work always wins the CPU first. (This is background knowledge, not stated in the lecture.)
EXAM TIP If asked to “explain the RT-extension of a standard OS”: draw Fig. 5, label the two compartments, state the 2 advantages (crash isolation, direct device access) and the 2 drawbacks (cannot use standard OS functions, heavier/slower than proprietary kernels).
4
Resource Sharing & Semaphores The big enemy of predictability
4.1 Why sharing hurts timing
4.2 Critical section
DEFINITION A critical section is the piece of code that accesses a shared resource (for example a driver). If two tasks run it at the same time — two writes, or one read and one write — the result can be unpredictable or wrong. So we need mutual exclusion: only one task inside at a time.
4.3 Semaphore: the lock that gives mutual exclusion
wait(S) (also written P(S)) while (S <= 0) { } // keep checking until S > 0 S = S - 1;
signal(S) (also written V(S)) S = S + 1;
Usage pattern in every task: P(S); /* enter / ... critical section (use the shared resource) ... V(S); / leave */
INTUITION A semaphore is the single key at a reception desk for a one-person room. P(S) = take the key (if it is on the hook, S = 1 → 0). If the hook is empty you wait. V(S) = hang the key back (S = 0 → 1) so the next person can enter.
4.4 Worked example: two tasks, one resource
5
Scheduling Basics (Revision) Vocabulary we will need for real-time scheduling
5.1 What is scheduling?
5.2 Preemptive vs non-preemptive
T1 keeps the CPU (6 units) T
0 1 2 3 4 5 6 7 8 9
time
T2 arrives — higher priority than T
T2 waits 3 units though it is more important
T1 T2 runs at once T1 resumes (rest)
0 1 2 3 4 5 6 7 8 9
time
T2 arrives — higher priority than T
T1 was pushed back, but T2 met its need
T1 needs 6 units, T2 needs 3 units
Fig. 8 — Same two tasks, two policies. Preemption lets the high-priority task start immediately.
INTUITION You are chatting with a friend (T1) when your boss calls (T2). Non-preemptive: the boss waits until you finish chatting. Preemptive: you pause the chat, take the call, and resume the chat afterwards.
5.3 Where do priorities come from?
Algorithm Priority is based on… Non-preemptive form Preemptive form
FCFS arrival time FCFS — (its timer-based cousin is round robin)
Shortest job length of the job Shortest job first (SJF) Shortest remaining time first (SRTF) — priority changes dynamically
Priority-based priority given to tasks run to completion, then pick highest priority
highest-priority ready task always runs
Round robin arrival order + time quantum
— always preemptive (preempted by the timer)
5.4 Worked example: shortest job first, with and without preemption
T2 T4 T3 T
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
T4 arrives (shortest job so far)
T4 must wait for T2 to finish, although it is the shortest. Then order = shortest first: T4, T3, T1.
T2 T4 T2 (rest) T3 T
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
T4 arrives (shortest job so far)
T4 takes the CPU the moment it arrives; T2 (2 units left) resumes after it.
Burst lengths: T1 = 7, T2 = 4, T3 = 5 (all arrive at t = 0). T4 = 1 (arrives at t = 2).
Fig. 9 — Non-preemptive vs preemptive shortest-job scheduling when a short job arrives mid-run.
EXAM TIP In a non-preemptive scheduler an arriving high-priority (or shorter) job cannot touch the running task; it can only be considered once the running task finishes. In a preemptive scheduler the running task is stopped immediately and resumed later.
5.5 Round robin
READY QUEUE
tail head
CPU quantum q = 5 ms
DONE (finished within its quantum)
short job
quantum expired → job is PREEMPTED by the timer → goes to the tail of the queue
Example: T1 = 8 ms, T2 = 3 ms, T3 = 6 ms, q = 5 ms
T1 T2 ✓ T3 T1 ✓ T3 ✓
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
ms ✓ = job completes. Each 5 ms slice that ends without ✓ means: preempted, back of the queue. Fig. 10 — Round robin with a 5 ms quantum.
INTUITION Everyone in a queue gets 5 minutes at the microphone; if you are not done you go to the back of the line.
5.6 What comes next
A. A task may have to wait for a resource held by another task, so its execution time includes an unpredictable blocking time.
A. Non-preemptive: the running task keeps the CPU until it finishes. Preemptive: a higher-priority arrival stops it; it resumes later. Example: Fig. 8.
A. Yes. Each task runs for a time quantum; when the quantum expires the task is preempted and joins the tail of the ready queue. The trigger is the timer, not a higher-priority arrival.
6.3 Common mistakes to avoid