Explain the disadvantages of using Test & Set. Outline the behaviour of semaphores ... if sem 0 decrement value, otherwise suspend current thread until value 0 ... – PowerPoint PPT presentation
val val I nS nW 9 Mutual Exclusion with Semaphores Mutual exclusion Ensuring no more than one thread in a critical region Critical region Sections of code that could suffer concurrent problems
P1
wait( sem )
critical region
signal( sem )
rest of actions
P2
wait( sem )
critical region
signal( sem )
other stuff
What should be the initial value of the semaphore? 10 Proof of Mutual Exclusion
sem is initially 1
nW(sem) number of completed waits
Providing access to critical region is
Wait before, Signal afterwards
Number in region is N nW(sem)-sS(sem)
From the invariant
nW(sem) ? nS(sem) 1
nW(sem)-nS(sem) ? 1
N nW(sem)-nS(sem) ? 1
At most 1 thread can be in the critical region
11 Simple Synchronisation
One thread held until another does something
Semaphore proceed is initially 0
P1
Do something
Wait(proceed)
Do action relying on P2
P2 Do preparation Signal(proceed) Do something else 12 Semaphore Weaknesses
Cannot conditionally enter or leave a WAIT
E.g. cannot wait with a timeout
Cannot examine the value of the semaphore
without executing a WAIT and becoming vulnerable to being blocked
Consumer Repeat extract item from buffer consume item forever Producer Repeat produce item deposit item in buffer forever 15 P/C Mutual Exclusion
How to ensure mutual exclusion?
Initialise mutex to ?
Initialise mutex to 1
Use semaphore avail to record number of items stored
Use semaphore free to record number of free buffer slots
Producer Repeat produce item deposit item in buffer forever Consumer Repeat extract item from buffer consume item forever Wait for new item Wait for free slot Signal availability of item Signal availability of slot 16 P/C Controlling Resources
How to ensure mutual exclusion?
Initialise mutex to 1
Initialise avail to ?
Initialise avail to 0
Initialise free to ?
Initialise free to N
Producer Repeat produce item put item in buffer forever Consumer Repeat take item from buffer consume item forever Wait(avail) Wait(free) Wait(mutex) Wait(mutex) Signal(mutex) Signal(avail) Signal(free) Signal(mutex) 17 What can we prove?
At most one thread in the critical region?
The same proof as before will work here
18 What can we prove?
Contraints on number of items in buffer
0 ? Number of items in buffer (B) ? N
B insertions extractions I - E
From producer
nS(avail) ? insertions ? nW(free) (Why)
From consumer
nS(free) ? extraction ? nW(avail)
-nW(avail) ? -extraction ? -nS(free)
nS(avail)-nW(avail) ? I - E ? nW(free)-nS(free)
Now use invariants on the semaphores
19 Number of items in buffer
Invariants
nW(free) ? nS(free)N
nW(free) - nS(free) ? N
nW(avail) ? nS(avail)
0 ? nW(avail) - nS(avail)
nS(avail)-nW(avail) ? I - E ? nW(free)-nS(free)
0 ? I - E ? N
20 Evaluating the proof
Was it easy to create?
Was it easy to understand?
Was it convincing?
Can we check the logic?
Can you show the result more simply?
Are you sure of your argument
Remember these are concurrent programs
Many possible paths
Argument must hold for every possible path
Proof holds for all paths
21 P/C Mutual Exclusion
Does this work?
Initialise mutex to 1
Initialise avail to 0
Initialise free to N
Producer Repeat produce item put item in buffer forever Consumer Repeat take item from buffer consume item forever Wait(mutex) Wait(mutex) Wait(avail) Wait(free) Signal(mutex) Signal(avail) Signal(free) Signal(mutex) 22 Evaluating the proof
PowerShow.com is a leading presentation sharing website. It has millions of presentations already uploaded and available with 1,000s more being uploaded by its users every day. Whatever your area of interest, here you’ll be able to find and view presentations you’ll love and possibly download. And, best of all, it is completely free and easy to use.
You might even have a presentation you’d like to share with others. If so, just upload it to PowerShow.com. We’ll convert it to an HTML5 slideshow that includes all the media types you’ve already added: audio, video, music, pictures, animations and transition effects. Then you can share it with your target audience as well as PowerShow.com’s millions of monthly visitors. And, again, it’s all free.
About the Developers
PowerShow.com is brought to you by CrystalGraphics, the award-winning developer and market-leading publisher of rich-media enhancement products for presentations. Our product offerings include millions of PowerPoint templates, diagrams, animated 3D characters and more.