Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 33

Real Time Operating Systems

Agenda
Hard and soft RTOS
Scheduling
Priority inversion
Deadlock and starvation
Task synchronization
Hard and Soft Real Time Systems
Hard Real Time System
 Failure to meet deadlines is fatal
 example : Flight Control System
Soft Real Time System
 Late completion of jobs is undesirable but not
fatal.
 System performance degrades as more & more
jobs miss deadlines
 Online Databases
Task Management

 Tasks are implemented as threads in RTOS


 Have timing constraints for tasks
 Each task a triplet: (execution time, period, deadline)
 Can be initiated any time during the period
Task memory allocation
Task States
Task state queues
There is a queue for each state in the
system.
The scheduler moves the tasks from
one queue to another depending on the
execution flow.
For example, if a task has gone to
sleep, the scheduler moves it from
Running queue to Wait queue.
Scheduling in RTOS
More information about the tasks are known
 No of tasks
 Resource Requirements
 Release Time
 Execution time
 Deadlines
Being a more deterministic system better
scheduling algorithms can be devised.
Scheduling Algorithms in RTOS

Clock Driven Scheduling

Weighted Round Robin Scheduling

Priority Scheduling
(Greedy / List / Event Driven)
Context switch
Scheduling Algorithms in RTOS (contd)

Clock Driven
 All parameters about jobs (release time/
execution time/deadline) known in
advance.
 Schedule can be computed offline or at
some regular time instances.
 Minimal runtime overhead.
 Not suitable for many applications.
Scheduling Algorithms in RTOS (contd)

Weighted Round Robin


 Jobs scheduled in FIFO manner
 Time quantum given to jobs is proportional to it’s
weight
 Example use : High speed switching network
 QOS guarantee.
 Not suitable for precedence constrained jobs.
 Job A can run only after Job B. No point in giving time
quantum to Job B before Job A.
FIFO Scheduling
First-in, First-out scheduling

The first enqueued task of highest priority executes to completion


A task will only relinquish a processor when it completes, yields, or blocks

Task 1 Task 2 Task 3

Time
Only a higher priority SCHED_FIFO or SCHED_RR task can preempt a
SCHED_FIFO task – all others will be starved as it runs

13
Round Robin Scheduling
Round-robin scheduling
Same as SCHED_FIFO but with timeslices

Among tasks of equal priority:


Rotate through all tasks
Each task gets a fixed time slice
Task Task Task Task Task Task Task Task Task
1 2 3 1 2 3 1 2 3

Time
Only a higher priority SCHED_FIFO or SCHED_RR task can
preempt a SCHED_FIFO
Tasks of equal priority preempt each other after timeslice
expiration

14
Scheduling Algorithms in RTOS (contd)

Priority Scheduling
(Greedy/List/Event Driven)
 Processor never left idle when there are
ready tasks
 Processor allocated to processes according
to priorities
 Priorities
 static - at design time
 Dynamic - at runtime
Preemptive Fixed Priority
Scheduling
High & low priority jobs
arrived together
High Priority Task

Time
Low Priority Task

16
Preemptive Fixed Priority
Scheduling
High priority job is
executed first
High Priority Task

Time
Low Priority Task

High Priority Task

Low priority job is


executed later

Time
Low Priority Task
17
Preemptive Fixed Priority
Scheduling

High Priority Task High priority job arrived

Time
Low Priority Task

18
Preemptive Fixed Priority
Scheduling
Preempts low priority
High Priority Task job

Time
Low Priority Task

High Priority Task

Lower priority job


resumes later

Time
Low Priority Task

19
Priority Scheduling

Earliest Deadline First (EDF)


 Process with earliest deadline given highest
priority
Least Slack Time First (LSF)
 slack = relative deadline – execution left
Rate Monotonic Scheduling (RMS)
 For periodic tasks
 Tasks priority inversely proportional to it’s period
Bounded Priority Inversion
Unbounded priority inversion
Solutions to Priority Inversion
Non Blocking Critical Section
 Since the low priority task blocks the high
priority task, avoid blocking by using non-
blocking algorithms.
Priority Ceiling
 All of the resources are assigned a priority that
is equal to the highest priority of any task that
may attempt to claim them.
Priority Inheritance
 The thread holding a resource inherits the
priority of the thread blocked on that resource
Random boosting
 The priority of the ready tasks can be
randomly boosted until they exit the critical
section.
Disabling Interrupts
 There are only two priorities in this case i.e.
interrupts disabled and preemptible. So
priority inversion is impossible as there is no
third option.
Other RTOS issues
Interrupt Latency should be very small
 Kernel has to respond to real time events
 Interrupts should be disabled for minimum
possible time
For embedded applications Kernel Size should
be small
 Should fit in ROM
Sophisticated features can be removed
 No Virtual Memory
 No Protection
Deadlock and starvation
Deadlock is a situation where a group of processes
are permanently blocked waiting for the resources
held by each other in the group.

Typical application where deadlock is a serious


problem: Operating system, data base accesses,
and distributed processing.

Starvation is the problem that occurs when high


priority processes keep executing and low priority
processes get blocked for indefinite time.
Deadlock Characterization
Deadlock can arise if four conditions hold simultaneously.

Mutual exclusion: only one process at a time can use a


resource.
Hold and wait: a process holding at least one resource is
waiting to acquire additional resources held by other processes.
No preemption: a resource can be released only voluntarily
by the process holding it, after that process has completed its
task.
Circular wait: there exists a set {P0, P1, …, P0} of waiting
processes such that P0 is waiting for a resource that is held by
P1, P1 is waiting for a resource that is held by P2, …, Pn–1 is
waiting for a resource that is held by Pn, and P0 is waiting for a
resource that is held by P0.
Dealing with Deadlock
Three general approaches exist for dealing with deadlock:
Dealing with starvation
There are a few ways to combat starvation. The first is to
simply make sure that your high priority tasks always yield
some time to the processor (e.g. by waiting for a
mutex/semaphore or through a delay function to put itself
to sleep).
The second method can be built into the scheduler or rely
on a higher priority task to monitor how long other tasks
have been asleep. If a lower-priority task has been asleep
(blocked) for some time, its priority level is gradually
raised until it gets a chance to run. Once it has run for
some time, its priority level is dropped back to its original
level. This is known as “aging.”
Task synchronization
Mutex lock
Semaphore signaling
Further reading
https://www.cs.unc.edu/~anderson/tea
ch/comp790/powerpoints/freertos-proj.
pdf
https://www.youtube.com/playlist?list=
PLEBQazB0HUyQ4hAPU1cJED6t3DU0h3
4bz

You might also like