2019 Lecture 08-Concurrency (Part2)
2019 Lecture 08-Concurrency (Part2)
INFORMATION
TECHNOLOGY
2
What we have understood about the
problem of concurrency?
Edsger W. Dijkstra
4
The Problem of Concurrency
5
Concurrency: control mechanisms
6
What are semaphores?
How can semaphores be used to control
concurrency problems?
The Concept of Flags
8
The Concept of Semaphores
9
Semaphores: Operations
10
Semaphores: Consequences
There is no way
to know which You do not
There is no way
process will know whether
to know before a
continue another process
process
immediately on a is waiting so the
decrements a
uniprocessor number of
semaphore
system when unblocked
whether it will two processes processes may
block or not are running be zero or one
concurrently
11
Counting Semaphores: Definition
12
Binary Semaphores: Definition
Continue
execution
13
WHAT IS A MUTEX?
Mutex: Mutual Exclusion Lock
15
Mutual Exclusion: Using Semaphores
The semaphore s is
initialised to 1.
The first process that
executes a semWait()
will be able to enter the
critical section immediately,
setting the value of s to 0.
16
Mutual Exclusion: Shared Data Protected by a Semaphore
17
What is a deadlock?
(Another concurrency problem)
The Concept of Deadlock
19
Potential Deadlock
I need quad
I need quad A and B
D and A
20
Actual Deadlock
HALT until
B is free
HALT until
A is free
Deadlock: Joint Program Diagram
22
Deadlock: Joint Program Diagram
23
No Deadlock
P acquires one
resource, uses it and
releases it before
acquiring another
one.
24
What resources do processes
compete for?
Resource Categories
Reusable
• can be safely used by only one process
at a time and is not depleted by that use
• processors, I/O channels, main and
secondary memory, devices, and data
structures such as files, databases, and
semaphores
Consumable
• one that can be created (produced) and
destroyed (consumed)
• interrupts, signals, messages, and
information in I/O buffers
26
Reusable Resources: Two Processes Competing
D – Disk resource
T –Tape resource
27
Consumable Resources: Two Processes Communicating
28
Resource Allocation Graph
Within a resource
node, a dot is
shown for each
instance of that
resource
29
Resource Allocation Graph
Multiple
copies of the
same resource
30
Deadlock: Example
Circular chain of
processes and
resources that
results in deadlock
31
What are conditions for a deadlock
to occur?
Deadlock: The Four Conditions
33
Deadlock: The Four Conditions Direct condition for
deadlock exists
34
What are three common approaches
to dealing with deadlock?
Dealing with Deadlock
36
Deadlock: Prevention, Avoidance, Detection
37
Deadlock: Prevention Strategy
38
Deadlock: Condition Prevention
Might not know all
Should not be the resources
disallowed needed in advance
Mutual
Hold and Wait
Exclusion
39
Deadlock: Condition Prevention
Only practical when states
of resources can be easily
saved and restored later
❑ No Preemption
▪ If a process holding certain resources is denied a
further request, that process must release its
original resources and request them again
▪ OS may preempt the second process (which holds
the requested resources) and require it to release
its resources to the first process
Slow down processes and
denying resource access
❑ Circular Wait
▪ Define a linear ordering of resource types
40
Deadlock: Avoidance Strategy
41
Deadlock: Avoidance Approaches
Deadlock Avoidance
42
Avoidance Strategy: Resource Allocation Denial
43
Avoidance Strategy: Determination of a Safe State
Need Matrix
Amount of Resources
existing available
resources after
Cij – Aij <= Vj for all j allocation
44
Avoidance Strategy: Determination of a Safe State
P2 runs to
completion
and releases
its resources
45
Avoidance Strategy: Determination of a Safe State
P1 makes the
request for
the remaining
resources
46
Avoidance Strategy: Determination of a Safe State
P3 runs to
completion
47
Avoidance Strategy: Determination of an Unsafe State
SAFE-STATE
P2 →1-R1, 2-R3
UNSAFE-STATE
P1 →1-R1, 1-R3
48
Deadlock Avoidance: Implementation Logic
49
Deadlock Avoidance: Implementation Logic
50
Deadlock Avoidance: Restrictions
51
Deadlock: Prevention Strategy vs Detection Strategy
52
Deadlock: Detection Algorithms
❑ Disadvantage:
▪ frequent checks consume considerable processor
time
53
Deadlock: Detection Algorithms
54
Deadlock Detection: Example
Available
vector
55
Deadlock Detection: Recovery Strategies
56
The Dining Philosophers problem
58
First Solution: Five Seated Philosophers
59
Second Solution: Four Seated Philosophers
60
Summary of Lecture 8
61
SUPPLEMENTARY SLIDES (non-examinable)
Semaphores: Strong vs. Weak
❑ Strong semaphores:
▪ The process that has been blocked the longest
is released from the queue first (FIFO)
❑ Weak semaphores:
▪ The order in which processed are removed
from the queue is not specified
63
Strong Semaphore Mechanism: Example
A, B and C rely on
the result from D
One of the
D results is
available
64
What is the producer/consumer problem?
(The classical concurrency problem)
[non-examinable]
The Producer/Consumer Problem
66
The Producer/Consumer Problem: Infinite Buffer
n = (in-out) = number of
items in the buffer
67
Solving with Binary Semaphores: Incorrect Solution
n = number of items
in the buffer
68
Solving with Binary Semaphores: Incorrect Solution
A semaphore is
initialised to 1.
semWait operation
decrements the
semaphore value.
semSignal operation
increments the
semaphore value.
69
Solving with Binary Semaphores: Incorrect Solution
In line 14, the consumer
fails to execute the
semWaitB operation. The
consumer exhausts the
buffer and set n to 0 (line
8), but the producer has
incremented n before the
consumer can test it in line
14. The result is a
semSignalB not matched by
a prior semWaitB . The
value of –1 for n in line 20
means that the consumer
has consumed an item from
the buffer that does not
exist. It would not do
simply to move the
conditional statement
inside the critical section of
the consumer because this
could lead to deadlock (e.g.,
after line 8 of the Table).
70
Solving with Binary Semaphores: Incorrect Solution
Deadlock (the
producer will be
waiting for the buffer
— s to be released by
the consumer, but
consumer is blocked
on delay)
71
Solving with Binary Semaphores: Correct Solution
72
Solving with Counting Semaphores
73
The Producer/Consumer Problem: Finite Buffer
Circular storage
74
Solving with Counting Semaphores: Finite Buffer
75