Title: Concurrent And Distributed Systems Lecture 4 BusyWaiting
1Concurrent And Distributed SystemsLecture
4Busy-Waiting
2General Strategy - Concurrency
- 1. Identify processes, global variables
- 2. Write down activities of each process and
identify shared (global variables) - 3. Specify synchronisation using coarse-grained
atomic actions - 4. Implement correct solution using fine-grained
atomic actions
3Busy-Waiting
- In a busy-waiting solution to a synchronisation
problem a process repeatedly checks a condition
until it is true, then continues execution. - For example, assume two sequential programs, P
and Q, are composed into a concurrent program,
PandQ. P calculates the value of an expression
and Q must wait until P has finished before using
the expression. - We can write code as follows.
4(No Transcript)
5When to Use Busy-Waiting
- Advantages of Busy Waiting
- 1. It can be implemented using only machine
instructions available on modern processors. - 2. It is efficient in multiprocessor hardware
where several CPUs share global memory. - Disadvantages of Busy Waiting?
6The Critical Section Problem
- This problem, like the bounded buffer problem, is
a standard problem in the theory of concurrent
systems. It is all about mutual exclusion. Here
we use it for illustrating the use of program
properties and invariants. - A collection of processes executes the following
repeatedly
7- process Pi 1 to n
- while true
- entry protocol
- critical section statements
- exit protocol
- non-critical section
-
-
- Required Properties
- 1. Only one process can be executing in its
critical section at any one time. - 2. Absence of deadlock.
- 3. Absence of unnecessary delay.
- 4. Eventual entry
8Critical Section Properties
- Assume that entry protocol is busy-waiting. Are
the above four requirements safety or liveness? - 1.
- 2.
- 3.
- 4.
9Global Invariant for CS Problem
- Want this to express safety property 1. We want
to make sure that something bad never happens.
Expressing the bad thing in English it is - Assume for the moment that there are two
processes, P1 and P2. - Define in1, boolean true if P1 is in CS, false
otherwise - Define in2, boolean true if P2 is in CS, false
otherwise. - Assume that we have the Boolean operators, and
or. - A formula using in1 and in2 which expresses
absence of the bad thing is
10The Naïve Solution
11Outline Solution for Two Processes
- program CS
- boolean in1 false // Initially both
- boolean in2 false // processes outside
- co P1
- while (true)
- Entry protocol for P1, EnP1
- crit sect (in1 is true)
- Exit protocol for P2
- non-crit (in1 is false)
-
-
- // P2
-
12Entry Protocols
- Entry protocol in P1, EnP1, is a coarse-grained
atomic action - lt await (not in2) in1 true gt
- Entry Protocol in P2 is
13Do coarse-grained atomic actions maintain the
Global Invariant, G, during concurrent execution?
- G is the good state that should be true before
and after each atomic action - G not (in1 and in2)
- Look at different points during execution
- Before P1 and P2 start executing, in1 and in2 are
both false. G is certainly true. - If P1 executes EnP1 then, G is true before EnP1,
and (not in2) is true just before the execution
of the assignment in EnP1 (because of the await
condition). When EnP1 has completed then - (not in2) and in1
- is true, and G is certainly still true.
- Similarly, if EnP2 runs then G is true after
EnP2.
14Global Invariant, G
- If we specify the exit protocol for P1 as ExP1
- ltin1 falsegt
- and the exit protocol for P2 as ExP2
- lt in2 falsegt
- Then, assuming that G is true before an exit
protocol , it will still be true after an exit
protocol.
15The False Path
program P2 while (true) while (in1)
skip in2 true crit sect code in2
false non-crit
- program CS
- boolean in1 false
- boolean in2 false
- co P1 //
- P2
- oc
- end of program
program P1 while (true) while (in2)
skip in1 true crit sect code in1
false non-crit
16Why is the False Path Program wrong?
- This looks OK, but we can end up with both P1 and
P2 in the CS at the same time. - How can it happen? Consider the scenario where
- 1. The Operating System gives P1 time slice 1
- P1 runs. P1 tests in2, finds it false, drops out
of the while loop. - 2. The time slice expires and P1 stops running.
- P2 starts running in time slice 2
- P2 tests in1, finds it false, drops out of the
while loop. - P2 sets in2 to true
- 3. The time slice expires and P2 stops running.
- P1 starts running in time slice 3.
- P1 sets in1 to true
- BINGO, we have found a scenario where both in1
and in2 are true at the same time. - THE GLOBAL INVARIANT, G, is False. THIS IS AN
INCORRECT SOLUTION.
17Spin Locks
- Consider the Critical Section Problem yet again.
Define the boolean variable, lock - lock (in1 or in2)
- When is lock true? When is lock false?
- Test-and-Set
- NOW, assume hardware or software provides the
Test-and-Set function, TSet - boolean TSet(lock)
- boolean value
- lt value lock
- lock true gt
- return value
TSet reads value lock and then sets lock to true
(atomically). Original value is returned
18Critical Section - Entry Code and Exit Code
- Keep lock as a global variable, initially false.
- Each process repeatedly attempts to get the lock
- by looping on the value of TSet(lock) until the
value returns false - EnP1
- while (Tset(lock))
- skip
- ExitP1
- lock false
19Lock-Based Solution Keeps G true
- Note that this is still a busy-waiting
implementation, but that it preserves the Global
Invariant. - Consider the Entry Protocol for P1. The entry
code only finishes if the while loop is exited.
The condition in the loop can only be false if
TSet has returned false at some point. But if
TSet returned false then this means that the lock
must have been false and both in1 and in2 must
have been false. Hence the guard in the await
statement was true. - If the operating system now gives P2 the
processor and P2 executes its TSet, P2 will find
the lock true and will have to keep looping. - In other words, P1 and P2 cannot read the lock as
false sequentially. - If P1 reads the lock as false, it sets the lock
to true before P2 gets a chance to read it.
(Similar argument applies with P1 and P2
swapped.) - When we use the TSet solution we cannot duplicate
the scenario in the False Path.
20TSet in Multiprocessors
- Each process is on a separate CPU, has a local
memory cache. The lock variable is in global
(shared) memory. When a process executes TSet it
prevents the other processes from doing TSet at
same time and therefore slows them down. - Multiprocessor Solution
- Read the lock before testing it (dont use Tset).
- If the lock is true then read it again.
- If the lock is false then try TSet.
Shared Memory boolean lock false Local
Processor memory CSEnter() boolean localc
while (lock) skip localc
TSet(lock) while (localc) while
(lock) skip localc
TSet(lock)
21Busy-Waiting, TSet and Four Properties?
- 1. Mutual exclusion?
- 2. Absence of deadlock?
- 3. Absence of delay?
- 4. Eventual entry?
22General Implementation ltawait (B) Sgt
Once you are inside the castle, you can collect
the treasures provided that the magic mushroom is
coloured red and the secret lake is coloured
silver. Unfortunately there are naughty pixies
who run around the forest, changing the colours
of all the objects. If you are inside the castle
this has the effect of "freezing" the current
colours of the mushroom and the lake, but you
can't change the colours yourself.
- Deep Dark Forest contains a castle full of
treasures. There is a single golden key to the
castle. A ticket dispenser controls access to the
key. You can pull out a ticket from the ticket
dispenser at any time. The ticket can come out
green or gold. If it comes out green you must
keep trying, pulling out tickets. If the ticket
is gold, you can get the key and get inside the
castle.
23The Solution
- Wait outside the castle, trying the ticket
dispenser until you get the key. When you have
got the key, enter the castle, look at the
mushroom and the lake. If they are red and silver
then collect the treasure, exit the castle and
leave the key back at the ticket dispenser.
Otherwise, exit the castle (leaving the key
behind again) and wander around outside for a
while before returning to try again in the hope
that the pixies have changed the colours in your
absence.
24General Solution
- ltawait (B) Sgt
- CSenter()
- while (not B)
- CSexit()
- delay (skip)
- CSenter()
-
- S
- CSexit()
- Exercise
- Match up the solution to the text