08 - Concurrency
08 - Concurrency
Parallelism
J J
K K
A B
r
ilo
he
so
op
ph
s
lo
e
i
r
Ph
1
chopstick 5 2 chopstick
Philo
r
sophe
s op he
Philo
4 3
r
ch
op
k
ic
st
st
ic
op
k
ch
Philosopher
r
ilo
he
so
op
ph
s
lo
e
i
r
Ph
1
chopstick 5 2 chopstick
Philo
r
sophe
s op he
Philo
4 3
r
ch
op
k
ic
st
st
ic
op
k
ch
Philosopher
time
Sequential execution
Parallel execution
T1 T 2 T3 T4 T 5 T 6 T 7
t0
Stage 1
t1
Stage 2
t2
time
Thread i
Thread j
Thread k
2 1 2 1 2 1
t1 t1 t1
3 3 3
p3 p3 p3
Petri Nets firing rules. (a) An unmarked net with one transition t1 with two input
places, p1 and p2 and one output place, p3. (b) The marked net, the net with
places populated by tokens; the net before firing the enabled transition t1.
(c) The marked net after firing transition t1 two tokens from place p1 and one
from place p2 are removed and transported to place p3
t3
p1 p1 p2 p1 p2 p3
t1 t2 t1 t2 t3 t1 t2
1 1
1 1
1
1
1 1
1
1
1
1
1
1
1
1 1
p 1
p 2
p
(c) Three processes interact by means 3
(c)
of communication events.
(m-1,n) (m,n-1)
(m,n)
p
1
p
1, 0 0 ,1
2 1 2
e e 2 2
1 2
e 1 e 1
2, 0 1,1 0, 2
p 1
p 2
1 2
e e
The lattice of the
3, 0 2 ,1 1, 2 2 2
(a) 1 2
e e
global states of two 1 1
p
1
3,1 2, 2
p
processes with the 2 1
e e
2
2
2
space-time showing
4 ,1
3, 2
2,3
e e
1
1
2
1
p
events per process.
5,1 4, 2 3, 3 2, 4 2 1 2
e e 2 2
1 2
e e
(b) The six possible
1 1
p
5, 2 4,3 3, 4 2,5
1
sequences of p
2 1 2
e 2 e 2
events leading to
53
4, 4
3, 5
e
1
1
2
e
1
p
the state Σ2,2 p
1
4,5
5, 4 2 1 2
e 2 e
2
(2 + 2)! / 2!2! = 6
5, 5
time
6,5
(a) (b)
m 1 m 2 m
5
1 2 6 7 8 9
p 2
m 3 m 4
1 2 3 10 11
p 3
deliver
Channel/ Channel/
Process receive Process
Interface Interface
Channel
Message receiving and message delivery are two distinct operations. The
channel-process interface implements the delivery rules, e.g., FIFO delivery.
m m 3
2
p 2
m 1
p 3
Violation of causal delivery when more than two processes are involved;
message m1 is delivered to process p2 after message m3, though message m1
was sent before m3. Indeed, message m3 was sent by process p1 after
receiving m1, which in turn was sent by process p3 after sending message m1.
p 1
m m C 1 C 2
m
5
1 2
1 2 3 5
e
2 e
2 e
2
4
e e
2 2
6
e
2
p 2
m 3 m 4
1 2 3 4 5
e 3 e
3 e
3 e 3 e
3
p 3
p 1
m
1 m 2 m
5
1 2 3 5
e
2 e
2 e
2
4
e e
2 2 e
6
2
p 2
m 3 m 4
1 2 3 4 5
e 3 e
3 e
3 e
3 e
3
p 3
The causal history of event e25 is the smallest consistent cut including e25.
Thread i
ACQUIRE Lock L
Lock L
Shared variable
Multi-step operation to be executed atomically
RELEASE Lock L
To enter a critical section a thread must acquire the lock of that section
and after finishing must release the lock. Only one thread should be
successful when multiple threads attempt to acquire the lock at the
same time; the other threads must wait until the lock is released.
Transaction T: Transaction U:
balance = b.getBalance() balance = b.getBalance()
b.setBalance(bal*1.1) b.setBalance(bal*1.1)
a.withdraw(bal/10) c.withdraw(bal/10)
Operations Locks Operations Locks
openTransaction
100 bal = b.getBalance() lock B
b.setBalance(bal*1.1) openTransaction
110
a.withdraw(bal/10) lock A bal = b.getBalance() waits for T’s lock on B
10
closeTransaction unlock A, B
lock B
121 b.setBalance(bal*1.1)
c.withdraw(bal/10) lock C
11
REF1 Fig. 16.14 closeTransaction unlock B, C
49
Example of Exclusive Locks
Transaction T: Transaction U:
balance = b.getBalance() balance = b.getBalance()
b.setBalance(bal*1.1) b.setBalance(bal*1.1)
a.withdraw(bal/10) c.withdraw(bal/10)
Operations Locks Operations Locks
openTransaction
100 bal = b.getBalance() lock B
b.setBalance(bal*1.1) openTransaction
110
a.withdraw(bal/10) lock A 100 bal = b.getBalance() waits for T’s lock on B
closeTransaction unlock A, B
lock B
110 b.setBalance(bal*1.1)
c.withdraw(bal/10) lock C
50
Two-Phase Locking
◼ Two-phase locking:
Growing phase: lock → lock → … → lock
Shrinking phase: unlock → unlock → … → unlock
52
Lock Compatibility
We can see that a write lock is more exclusive than a read lock.
53
Lock Promotion
◼ Inside a single transaction, it is possible to first read an object and
then write the same object.
So it sets a read lock on the object, and then sets a write lock on the
object.
Instead of using two locks on the same object at the same time, we can
simply “promote” the original read lock to the more exclusive write lock.
However, if the read lock is shared with other transaction(s), it cannot
be promoted because the write operation conflicts with the read
operation of other transaction(s). The transaction must request a write
lock and wait for other read locks to be released.
54
Strict Two-Phase Locking
55
Deadlocks
◼ The use of locks can lead to deadlock
Transaction T Transaction U
56
Wait-for Graph
◼ A wait-for graph can be used to represent the waiting relationships
between current transactions.
Nodes represent transactions.
Edges represent wait-for relationships.
Waits for B
Held by
57
Conditions for Deadlock Occurs
◼ Mutual Exclusion
Some resources are locked
◼ Hold and Wait
Some process is holds some resource and waits for some other
resources held by other processes.
◼ No Preemption
Once resource is allocated, it cannot be preempted/taken back.
◼ Circular Wait
The waiting is in a circular manner.
58
Deadlock Prevention - Breaking the Conditions
◼ Mutual Exclusion
Resources are many enough. No sharing is needed
◼ Hold and Wait
A process request all resources at a time (inefficient)
Take one resource at a time (not realistic, non serial equivalent)
◼ No Preemption
Process with higher priority can overtake resource
May need to rollback transaction.
◼ Circular Wait
Label each resource with an integer. Lock resources in an ascending
order. (may cause starvation)
59
Deadlock Avoidance
◼ Every process is to declare its maximum need of locks at the
beginning.
◼ Coordinator keeps the record and decides to entertain a new
request only if it will not transit to an unsafe state.
N: number of processes
M: number of resources
Av[]: array of each resource availabile
Dcl[][]: 2D array of resources needed by each process
Allo[][]: 2D array of resources currently allocated to each process
Need[][]: Av[][] – Allo[][], the resource shorts of.
60
Banker’s Safety Algorithm
61
Banker’s Safety Algorithm
62
Deadlock Detection and Timeouts
◼ Deadlock detection:
Find cycles in the wait-for graph
Select a transaction for abortion to break the cycle
◼ Timeouts:
Breaks “Hold and Wait” condition
Each lock is given a limited period in which it is invulnerable.
After this time, a lock becomes vulnerable.
◼ If no other transaction is competing for the locked object, it remains
locked.
◼ However, if any other transactions is waiting to access this object,
the lock is broken and the waiting transaction resumes. The
transaction whose lock has been broken is aborted.
63
Timeout
Premature Writes:
; Restore the old value of a
commit transaction
65 abort transaction; Restore the old value of a
Runs, cuts and atomic actions.
◼ I don't cover these concepts in the course;
◼ If you are interested, you can refer to the hidden slides.
Dan C. Marinescu 66
Atomic actions
◼ Either the entire atomic action is carried out, or the system is left in the
same state it was before the atomic action was attempted;
a transaction is either carried out successfully, or the record targeted by
the transaction is returned to its original state.
◼ Two phases:
Pre-commit → during this phase it should be possible to back up from it
without leaving any trace. Commit point - the transition from the first to the
second phase. During the pre-commit phase all steps necessary to prepare
the post-commit phase, e.g., check permissions, swap in main memory all
pages that may be needed, mount removable media, and allocate stack
space must be carried out; during this phase no results should be exposed
and no actions that are irreversible should be carried out.
Post-commit phase → should be able to run to completion. Shared
resources allocated during the pre-commit cannot be released until after
the commit point.
Abort Aborted