You are on page 1of 21

Embedded System

Chapter 5

Lecture 6

Resource Sharing
Presented by:
Ahmed k. Daraj
Introduction
Many shared resources do not allow simultaneous access. When tasks share
resources, scheduling anomalies may occur due to potential priority inversions and
even deadlocks. This chapter discusses how resource sharing and resource
contention affect the execution behavior and schedulability of tasks and how various
resource access control protocols work to reduce the undesirable effect of resource
sharing. We focus on priority-driven and single-processor systems
Resource Sharing
Task may need some resources in addition to a processor in order to make
progress. For example, a computational task may share data with other
computational tasks, and the shared data may be guarded by semaphores each
semaphore is a resource for examples of common resources are:

❑ Data structures and variables,


❑ Main memory area,
❑ Files, registers,
❑ I/O units.
Resource Sharing
When a task wants to access the shared data guarded by a semaphore R, it must lock the
semaphore first and then enters the critical section of the code where it accesses the shared
data. In this case, we say that the task requires the resource R for the duration of the
critical section
Resource Requirement Specification
The duration of a critical section: defined as how long the task that locks the
corresponding semaphore (resource) needs to hold the resource.

The task needs units ( 𝜂 ) resource (R) execution time of critical section (c) denote
by [R, 𝜂; c], for one unit only, the simpler notation [R; c] is used.

The nested critical sections are denoted by nested brackets.

For example:- [R1; 10[R2; 3]] that mean:

one unit of R1 for 10 units of time & one unit of R2 for 3 units of time in the critical
section of R1.
• Example 5.1 Critical Sections • The execution time of T2 is 10 units of
time. It has five code segments:
• Figure 5.1 shows the critical
sections of two tasks, namely T1 • [0, 1]: Requires no resources.
and T2. The execution time of T1 is • [1, 4]: Requires R2.
12 units with seven code • [4, 6]: Requires no resources.
segments:
• [6, 8]: Requires R1.
• [0, 2]: Requires no resources.
• [8, 10]: Requires no resources.
• [2, 4]: Requires R1. • The resource requirements of the two
tasks are specified as follows:
• [4, 6]: Requires no resources.
• T1: [R1; 2], [R3; 5[R2; 2]].
• [6, 8]: Requires R3.
• T2: [R2; 3], [R1; 2].
• [8, 10]: Requires R3 and R2.
• [10, 11]: Requires R3.
• [11, 12]: Requires no resources.
Priority Inversion and Deadlocks
Assume that a higher priority task TH and a lower one TL share a resource R.
TH expects to run as soon as it is ready. However, if TL is using the shared resource R
when TH becomes ready to run, TH must wait for TL to finish with it. We say that TH is
pending on the resource.
priority inversion classify into two types as following:

1- Bounded priority inversion


2- Unbounded priority inversion
Priority Inversion and Deadlocks cont…
1- Bounded priority inversion
The bounded priority inversion occurs when a lower priority task is running while a
higher priority task is waiting, which violates the priority model that a task can only be
preempted by another task of higher priority, this is Bounded priority inversion.
When TL is finished using R and unlocks it, TH preempts TL and runs.

However: As long as the critical section of TL with regard to R is not sufficiently


long, TH can still meet its deadline.
Priority Inversion and Deadlocks cont…
2- Unbounded priority inversion
What is the much worse situation can occur?.
• After TH is blocked by TL on R and before TL is done with R, a task TM1
with a priority between TH and TL is released and preempts TL. Now TL has
to wait until TM1 is completed and then resumes its execution and so on if we
have TM2 between them. This situation called an unbounded priority
inversion.
Deadlock
Tasks that share resources can also enter a state that none of them can make progress. Such a
state is called a deadlock.

A deadlock occurs when all the following four conditions are met:
❑ Mutual exclusive: One or more resources must be held by a task in an exclusive mode.

❑ Hold and wait: A task holds a resource while waiting for another resource.
❑No preemption: Resources are not preemptable.
❑Circular wait: here is a set of waiting tasks T ={T1, T2, …, TN}, such that T1 is waiting for a
resource held by T2, T2 is waiting for a resource held by T3 and so on, until TN is waiting for a
resource held by T1.
Deadlock
Resource Access Control
Resource sharing can cause serious problems in real-time systems.
We need rules to regulate the access to shared resources as following:

❑ Several well-known resource access control protocols have been developed to


handle priority inversion and deadlocks caused by resource sharing.
❑ A resource access control protocol is a set of rules that govern when and under what
conditions each request for resource is granted and how tasks requiring resources are
scheduled.
❑ A well-designed access control protocol can prevent deadlocks from occurring.

However: there is no protocol that can eliminate priority inversion. A realistic goal
of access control is to keep the blocking time of higher priority tasks under control.
Non-preemptive Critical Section Protocol

To prevent unbounded priority inversion from occurring, a simple way is to make


all critical sections non-preemptable. In other words, when a task locks a resource,
it executes at a priority that is higher than the priorities of all other tasks, until it
unlocks the resource (or completes the execution of its critical section).This
protocol is called non-preemptive critical section (NPCS) protocol. Because in this
protocol, no task can be preempted when it holds a resource, circular waiting can
never occur. Therefore, a deadlock is impossible.
Non-preemptive Critical Section Protocol
Non-preemptive Critical Section Protocol
Example1: For the following tasks find (completion time, total time, waiting time and
response time based on Non-preemptive Critical Section Protocol
Task Priority Arrival time Task units Completion time Total time(T.T) Waiting time Response
s (A.T) (T.U) (C.T) C.T – A.T T.T- T.U Time

P1 3 0 8 8 8 0 0
P2 4 1 2 17 16 14 14
P3 4 3 4 21 18 14 14
P4 5 4 1 22 18 17 17
P5 2 5 6 14 9 3 3
P6 6 6 5 27 21 16 16
P7 1 10 1 15 5 4 4
P1 P5 P7 P2 P3 P4 P6

C.T 8 14 15 17 21 22 27 AVERAGE WAITTING= 9.7142


Example2: For the following tasks find (completion time, total time, waiting time and
response time based on Non-preemptive Critical Section Protocol
Task Priority Arrival time Task units Completion time Total time(T.T) Waiting time Response
s (A.T) (T.U) (C.T) C.T – A.T T.T- T.U Time

P1 3 0 8 8 8 0 0
P2 4 1 2 11 10 8 8
P3 4 3 4 21 18 14 14
P4 5 4 1 22 18 17 17
P5 2 5 6 15 10 4 4
P6 6 6 5 27 21 16 16
P7 1 7 1 9 2 1 1
P1 P7 P5 P2 P3 P4 P6

C.T 8 9 15 17 21 22 27 AVERAGE WAITTING = 8.5714


Lecture 6
Done
Best luck

You might also like