Title: INFT1331073310 Section 8 Concurrency II: Mutual Exclusion
1INFT13-310/73-310Section 8Concurrency
IIMutual Exclusion
http//www-dse.doc.ic.ac.uk/concurrency/
Using material from Magee and Kramer
2Modelling Concurrency
- Issues
- How to model execution speed?
- arbitrary
- abstract away time
- How to model concurrent execution?
- actions in each process ordered
- arbitrary relative ordering between processes
(interleaving) - Result
- general (asynchronous) model independent of
scheduling
- Parallel Composition
- If P and Q are processes, (PQ) represents the
concurrent execution of P and Q
E.g., PONDER (think -gt STOP). DINNER (talk
-gt eat -gt STOP). PONDINNER (PONDER
DINNER). Traces think -gt talk -gt eat. talk -gt
think -gt eat. talk -gt eat -gt think.
Laws of composition (P Q) (Q P) (P (Q
R)) ((P Q) R) (P Q R)
3Modelling Interaction
- Shared actions
- Processes in an interaction with actions in
common share the actions - unshared actions can be arbitrarily interleaved
- shared actions are executed at the same time by
all sharing processes - E.g.,
- MAKER (make -gt ready -gt MAKER).
- USER (ready -gt use -gt USER).
- MAKER_USER (MAKER USER).
- Handshake
- An action acknowledged by another
- E.g.,
- MAKER2 (make-gtready-gtused-gtMAKER2).
- USER2 (ready-gtuse-gtused-gtUSER2).
- MAKER_USER2 (MAKER2 USER2).
How many states? How many traces?
4Modelling Multi-party Synchronisation
- Handshake An action acknowledged by another
- E.g.,
- MAKER_A (makeA-gtready-gtused-gtMAKER_A).
- MAKER_B (makeB-gtready-gtused-gtMAKER_B).
- ASSEMBLE (ready-gtassemble-gtused-gtASSEMBLE).
- FACTORY (MAKER_A MAKER_B ASSEMBLE).
How many states? How many traces?
makeA
makeB
makeA
ready
assemble
5
0
1
2
3
4
makeB
used
5Guten Jour!
public static void displayMessage(
RepeatedMessage rm ) throws InterruptedException
for (int i 0 i lt rm.message.length()
i) System.out.print(rm.message.charAt(i)
) sleep(50) System.out.println()
Sample output BGount ejno uTra!g !B on
jour! BoGnu tjeonu rT!a Bgo!n jour! BonG
ujtoeunr !T Baogn! jour! BGount ejno
uTra!g B!o n jouGru!t en Tag!
- HOMME (B-gto-gtn-gtj-gto-gtu-gtr-gtHOMME).
- DEUTSCHER (G-gtu-gtt-gte-gtn-gtT-gta-gtg-gtDEUTSCHER).
- MESS (HOMME DEUTSCHER).
6Bon Tag!
public synchronized static void displayMessage(
RepeatedMessage rm ) throws InterruptedException
for (int i 0 i lt rm.message.length()
i) System.out.print(rm.message.charAt(i)
) sleep(50) System.out.println()
Sample output Bon jour! Guten Tag! Bon
jour! Guten Tag! Bon jour! Bon jour! Guten Tag!
- HOMME (f_go-gtB-gto-gtn-gtj-gto-gtu-gtr-gtf_end-gtHOMME).
- DEUTSCHER (g_go-gtG-gtu-gtt-gte-gtn-gtT-gta-gtg-gtg_end
-gtDEUTSCHER). - SYSTEM (f_go -gtf_end -gtSYSTEM g_go -gtg_end
-gtSYSTEM). - NO_MESS (HOMME DEUTSCHER SYSTEM).
7Interference
- Interference destructive update caused by
arbitrary interleaving of read and write actions
- ETURN
- (ein-gtereadCntec-gtewriteCntec1-gtETURN
- eout-gtereadCntec-gtewriteCntec-1-gtETURN).
- WTURN
- (win-gtwreadCntwc-gtwwriteCntwc1-gtWTURN
- wout-gtwreadCntwc-gtwwriteCntwc-1-gtWTURN)
. - COUNTERcount
- (readCntcount-gtCOUNTERcount
- writeCntnewCount-gtCOUNTERnewCount).
- PARK (ETURN WTURN COUNTER).
- E.g., Theme park
- two gates counting entrants
- central record of number of people in park
Park
Does it work?
East
people
West
Turnstile
Turnstile
8Interference
- Possible element of trace
- Suppose count 10
- ...-gtereadec (ec 10)
- -gtwreadwc (wc 10)
- -gtwwritewc1 (wc1 11 count)
- -gtewriteec1 (ec1 11 count)
- -gt
- count 11, but 2 more people in park
How?
Does it work? means Does it work for all
traces?
9Atomic Actions
- An atomic action is an action that executes
uninterrupted from start to finish on a processor - (In our CSP/FSP notation, events represent atomic
actions)
- Note! Very few statements in a high-level
programming language are atomic - E.g., assignment is not atomic
- Java x 10
- Assembler
- LOAD R1 10
- LOAD R2 ADDR_X
- STORE _at_R2 R1
10Interference and Mutual Exclusion
- Interference bugs are extremely difficult to
track - General solution
- Identify the critical section of the code
- Ensure only one process is allowed to execute
during its critical section - The critical sections are the accesses to the
shared memory/resource
- Mutually exclusive access to critical section
through locking - LOCK (acquire-gtrelease-gtLOCK).
- ETURN (ein-gtINC-gtETURN
- eout-gtDEC-gtETURN).
- INC (acquire-gtreadx-gtwritex1-gtrelease).
- DEC (acquire-gtreadx-gtwritex-1-gtrelease).
- COUNTERcount
- (readcount-gtCOUNTERcount
- writenewCount-gtCOUNTERnewCount).
11Locking in Java
- Keyword synchronized
- Attached to method system ensures method
executes to completion with no interleaving - Can also be attached to an object
- synchronized (object)
- // code
-
- Object object is locked for duration of code
- code cannot be executed if another process has
the lock on object
- class Counter
-
- public synchronized void increment( )
count -
or
class Turnstile Counter counter public
void enter( ) synchronized (counter)
counter.increment() // Remember!
Locks object
12Buffering
- Locking (synchronisation) limits the number of
possible traces to the safe ones - If not used carefully, locking will force the
system into a sequential execution - E.g., the theme park
- ein-gtINC-gtwin-gtINC-gtein-gtINC-gt
- One technique to improve concurrency in many
systems is buffering - Dont lock the central object
- Have each thread communicate with central control
through its own buffer - buffer is a list (e.g., a Vector)
- buffers are locked for adding and removing, but
while doing other work, processes are concurrent
13Monitors
- Synchronisation (and locking)
- someone must be controlling the locks
- processes will be waiting to access locked
objects - Who is controlling?
- Where do processes wait?
- A Monitor is a method of managing synchronisation
- A Monitor provides
- encapsulation of data and methods
- mutually exclusive execution of monitored methods
- condition synchronisation
- a wait queue of processes waiting to use the
monitored resources
14Monitors
- Condition synchronisation
- Java provides a wait queue per object
- final void wait() throws InterruptedException
- puts thread on wait queue
- thread releases synchronisation lock on monitor
- thread waits until notified by another thread
- when awoken, thread must still wait to reacquire
synchronisation lock on monitor - final void notify()
- wake up a single thread on the wait queue
- final void notifyAll()
- wake up all threads on the wait queue
- Java provides support for treating any object as
a monitor (through keyword synchronized) - Typical usage scenario
- Active entities (initiating actions) implemented
as threads - Passive entities (responding to actions)
controlling shared resources implemented as
monitors
15Monitor and Condition Synchronisation Example
- Carpark
- cars can enter carpark when its not full
- cars can leave carpark when its not empty
- Processes
- arrivals
- departures
- controller
- CONTROLspaces
- ( when (spaces gt 0) arrive -gt CONTROLspaces-1
- when (spaces lt MAX) depart -gt
CONTROLspaces1 ). - ARRIVALS (arrive -gt ARRIVALS).
- DEPARTURES (depart -gt DEPARTURES).
- CARPARK ( CONTROL ARRIVALS DEPARTURES).
- Implementation
- ARRIVALS and DEPARTURES threads
- CONTROL monitor
16Typical Implementation of Condition
Synchronisation
- CSP/FSP
- when cond act -gt NEWSTATE
Java public synchronized void act( ) throws
InterruptedException while (!cond) wait(
) // modify monitor state notifyAll( )
Notification necessary to awaken threads that may
be waiting to enter the monitor now that its
state has changed
17Carpark in Java
- class CarParkControl
- protected int spaces
- protected int capacity
-
- public synchronized void arrive()
- throws InterruptedException
-
- while (spaces0) wait()
- spaces - -
- notify()
-
- public synchronized void depart()
- throws InterruptedException
-
- while (spacescapacity) wait()
- spaces
- notify()
- class Arrivals implements Runnable
- protected CarParkControl carpark
-
- public Arrivals(CarParkControl c)
- carpark c
-
- public void run()
- try
- while (true)
- carpark.arrive()
- // delay
-
- catch (InterruptedException e)