Docsity

Real Time Operating Systems part 2, Summaries of Embedded Systems

Toggle sidebar
bg1
Real-Time Operating Systems · Lecture 1 · Concept notes
— 1 —
Real-Time Operating Systems
Lecture 1 (RTOS 1) — concept-first exam notes
Clock synchronization · What an RTOS needs · Kernel structures · Resource sharing & semaphores · Scheduling basics
The lecture in one paragraph. A real-time operating system (RTOS) is judged by whether its timing is
predictable, not just whether it is fast. This lecture sets the ground: (1) how several computers keep their clocks
in agreement, (2) what an RTOS must provide, (3) how RT kernels are structured so that real-time tasks are not
dragged down by a normal OS, (4) how sharing resources (semaphores) can make a task wait, and (5) a revision
of scheduling vocabulary — preemptive, priority, round robin — that the next lectures build on.
1Clock
synchronization
keep all clocks
counting together
2What an RTOS
must provide
predictable,
scheduling, fast
3RT kernel
structures
proprietary vs
RT-extension
4Resource
sharing
critical section,
semaphores
5Scheduling
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.
1Clock Synchronization
Why many computers must “count time together”
1.1 Why do we need it?
Modern embedded systems rarely have one computer. A car, for example, has separate dedicated processors for
engine control, brakes, airbags, infotainment and so on, all working for one overall goal. Each processor has its own
clock, and clocks drift apart. If they disagree, then “event A happened before B” or “finish within 5 ms” means
different things on different processors.
Rule: whenever there is more than one clock, we need synchronization so that they count together.
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.
Reminder from the last class: time is represented in two ways, TAI (International Atomic Time) and UTC
(Coordinated Universal Time). GPS can supply both.
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
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Real Time Operating Systems part 2 and more Summaries Embedded Systems in PDF only on Docsity!

Real-Time Operating Systems

Lecture 1 (RTOS 1) — concept-first exam notes

Clock synchronization · What an RTOS needs · Kernel structures · Resource sharing & semaphores · Scheduling basics

The lecture in one paragraph. A real-time operating system (RTOS) is judged by whether its timing is

predictable, not just whether it is fast. This lecture sets the ground: (1) how several computers keep their clocks

in agreement, (2) what an RTOS must provide, (3) how RT kernels are structured so that real-time tasks are not

dragged down by a normal OS, (4) how sharing resources (semaphores) can make a task wait, and (5) a revision

of scheduling vocabulary — preemptive, priority, round robin — that the next lectures build on.

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.

Clock Synchronization

Why many computers must “count time together”

1.1 Why do we need it?

Modern embedded systems rarely have one computer. A car, for example, has separate dedicated processors for

engine control, brakes, airbags, infotainment and so on, all working for one overall goal. Each processor has its own

clock, and clocks drift apart. If they disagree, then “event A happened before B” or “finish within 5 ms” means

different things on different processors.

Rule: whenever there is more than one clock, we need synchronization so that they count together.

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.

Reminder from the last class: time is represented in two ways, TAI (International Atomic Time) and UTC

(Coordinated Universal Time). GPS can supply both.

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

INTERNAL synchronization

no outside reference — nodes agree among themselves

Process A (my clock)

Neighbour B (its clock)

  1. what's your time?
    1. here is my time
  2. apply the correction and set the corrected value

Two flavours:

  • one master clock, all follow it
  • distributed: ask neighbours

EXTERNAL synchronization

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

“Precision” here means: how large a mismatch between two clocks is acceptable, and therefore how tight the

synchronization exchange must be. The closer to the wire, the tighter it can be.

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

  • Predictability is still an issue: it was tuned for particular situations and has not been tested for all of them.
    • RT tasks cannot use the facilities/functions of the standard OS.
    • Heavier and not as fast as proprietary kernels (not always the case).

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)

REAL-TIME SIDE

keeps running even if the other side crashes

STANDARD-OS SIDE (e.g. Linux)

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

REAL-TIME KERNEL + REAL-TIME SCHEDULER

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.

Walk through the diagram:

  • A real-time kernel with its own real-time scheduler is the shared base, sitting directly on the hardware.
  • Left (real-time side): RT tasks plus dedicated device drivers. RT tasks use these drivers directly.
  • Right (standard-OS side): the standard OS (e.g. Linux) with its own scheduler, its own drivers, and ordinary

programs such as init, bash, Chrome. These programs reach the kernel only through the standard OS.

  • Hardware is reached by I/O operations and interrupts via the kernel.

Why build it with two separate sides?

  • Crash isolation: a crash of the standard OS (like a Windows hang) does not touch the real-time tasks.
  • No waiting at drivers: any request to a driver owned by the standard OS goes through the standard OS, which

may impose mutual exclusion and make the task wait. That is undesirable and unnecessary for dedicated

real-time devices, so we keep those devices outside its control.

  • Different scheduling policy: standard scheduling policies are often not applicable to real-time work (we see

why in the next lectures), so each side has its own scheduler.

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

One major impediment to predictability is resource sharing: you want a resource and you may not get it

immediately. The time you spend waiting is part of your total time, and it is hard to predict. To understand it, revise

critical sections and semaphores from your OS course.

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

A semaphore S is an integer variable on which only two operations are allowed, and each is atomic (while one task

is executing it, no other task can start wait(S) or signal(S) on the same S):

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

S is initialized to 1 and gates a driver. T1 starts first, T2 starts a little later.

5

Scheduling Basics (Revision) Vocabulary we will need for real-time scheduling

5.1 What is scheduling?

When we “schedule a task”, we allocate the processor (CPU) to that task. Only the task holding the CPU is

running; the others wait.

5.2 Preemptive vs non-preemptive

  • Non-preemptive: once a task has the CPU it keeps it until it finishes — even if a more important task arrives.
  • Preemptive: if a higher-priority task arrives, the running task is stopped, the new one runs, and the stopped

task continues later from where it stopped.

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

PREEMPTIVE

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?

  • Assigned by the designer/tasks: we create a hierarchy of priorities (this task is more important than that

one).

  • Decided by the scheduling policy: e.g. shortest job first gives priority to the shortest job; shortest

remaining time first keeps re-deciding dynamically.

  • First-come-first-served (FCFS) is also a priority — priority by time of arrival.

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

Three tasks T1, T2, T3 arrive together at t = 0. Because they arrive together, FCFS cannot separate them, so we

choose by job length and start with the shortest, T2. Then a still shorter task T4 arrives at t = 2.

NON-PREEMPTIVE shortest-job-first

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.

PREEMPTIVE shortest-remaining-time-first

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

Tasks wait in a ready queue. The first task gets the CPU for one time quantum (e.g. 5 ms). If it finishes within the

quantum it leaves; otherwise it goes to the back of the queue and the next task runs. Round robin is preemptive

— but the preemption is caused by the quantum expiring, not by a higher-priority arrival.

READY QUEUE

T3 T2 T

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

Standard-OS scheduling policies are not always applicable to real-time systems. From the next lecture we study

preemptive scheduling algorithms only, first the basic ones and then dedicated real-time algorithms.

Q. Why is resource sharing a threat to real-time behaviour?

A. A task may have to wait for a resource held by another task, so its execution time includes an unpredictable blocking time.

Q. Preemptive vs non-preemptive scheduling?

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.

Q. Is round robin preemptive?

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

  • Writing “RTOS = fast OS”. The key word is predictable.
  • Mixing up the precision order. Hardware is the tightest of the three internal levels; application is the loosest.
  • Saying wait() increments S. wait decrements, signal increments, and wait proceeds only if S > 0.
  • Saying RT tasks use the standard OS’s drivers. In the RT-extension design they use dedicated drivers directly.
  • Calling round robin non-preemptive. It is preemptive — by the timer.
  • Forgetting that FCFS is also a priority scheme (priority = arrival time).