Hardware Reference
In-Depth Information
τ
W
τ
D
shared
resource
wait(s)
wait(s)
critical
section
critical
section
R
signal(s)
signal(s)
Figure 2.11
Structure of two tasks that share a mutually exclusive resource protected by
a semaphore.
blocked
plot(x,y)
τ
R
D
x=4
y=8
τ
W
R
R
t
(1,2)
(4,2)
(4,8)
Figure 2.12
Example of schedule when the resource is protected by a semaphore.
To avoid the problem illustrated in Figure 2.10, both tasks have to encapsulate the
instructions that manipulate the shared variables into a critical section. If a binary
semaphore
s
is used for this purpose, then each critical section must begin with a
wait(s)
primitive and must end with a
signal(s)
primitive, as shown in Figure 2.11.
If the resource is free, the
wait(s)
primitive executed by
τ
W
notifies that a task is using
the resource, which becomes locked until the task executes the
signal(s)
. Hence, if
τ
D
preempts
τ
W
inside the critical section, it is blocked as soon as it executes
wait(s)
,
and the processor is given back to
τ
W
. When
τ
W
exits its critical section by executing
signal(s)
, then
τ
D
is resumed and the processor is given to the ready task with the
highest priority. The resulting schedule is shown in Figure 2.12.
Search WWH ::
Custom Search