Title: Assignment 1 Interleaving
1Assignment 1 Interleaving Mutual Exclusion
Group Exercise 1. Consider the following program.
- If instructions 1a and 2a are treated as atomic
instructions, what are the possible final values
of c1 and c2? - On a load and store architecture the instruction,
ab op c, is implemented by the three atomic
instructions Rb RR op c aR, where R is a
local register and op is either or .
2Assignment 1 Interleaving Mutual Exclusion
GE1. (b) If instructions 1a and 2a are
implemented on a load and store architecture (i)
How many interleavings are possible? (ii) What
are the possible final values of c1 and c2?
3Assignment 1 Interleaving Mutual Exclusion
- GE1. Solution
- (a)
- 1a2a gives c120, c224.
- 2a1a gives c145, c29.
4Assignment 1 Interleaving Mutual Exclusion
- 20 interleavings.
- c145, c29
- c120, c29
- c120, c224
5Assignment 1 Interleaving Mutual Exclusion
GE2.
- A car park has 100 parking spaces. Because of
building work max cars must be accommodated in an
overflow area which is accessed via the main car
park. Entrance to the main and overflow areas is
controlled by two automatic barriers as follows. - When the car park is empty both barriers are
closed. - Normally, the main barrier is raised as a car
approaches and is lowered immediately the car has
entered. - An exception occurs immediately after the main
car park is full i.e. when it has 100-max cars in
it. As the next car approaches the overflow
barrier is raised first, then the main barrier is
raised. Once the car has entered the main car
park the overflow barrier remains raised and the
main barrier is lowered. The normal main barrier
action described above then resumes.
6Assignment 1 Interleaving Mutual Exclusion
GE2.
Consider the following program which is intended
to control the two barriers. All instructions,
o1, o2, o3, m1, m2, m3, m4, are atomic. You may
assume that 0ltmaxlt100.
int cars0
process Overflow int max o1
ltinput(max)gt while(true) o2 ltif
(cars100-max1) breakgt o3
ltopenOverflowBarriergt
process Main while (true) m1
ltcarsgt m2 ltopenMainBarriergt m3
ltcloseMainBarriergt m4 ltif (cars100)
breakgt
7Assignment 1 Interleaving Mutual Exclusion
GE2.
- The program terminates only if both processes
- terminate.
- (i) Explain why the program may not terminate.
- (ii) Explain why, even if the program does
terminate, it may not operate as specified. - (b) Suggest minimal changes which, while keeping
the - two process structure, ensure that program
does - terminate and operates as specified. You
must only use atomic instructions. You may
introduce new variables but you must not alter
the scope of cars and max.
8Assignment 1 Interleaving Mutual Exclusion
- GE2. Solution
- (a)
- The following interleaving that shows the program
may not terminate - m1 m2 m3 m4 ... (this sequence is
repeated until cars equals 100 and process Main
terminates) - o1 o2 o2 o2 (process Overflow never gets
past o2). -
9Assignment 1 Interleaving Mutual Exclusion
- GE2. Solution
- (a)
- The following interleaving that shows the program
may terminate, but without operating as
specified. - o1 (max100) m1 o2
- m2 m3 m4 m1 (this sequence is repeated
until cars equals 100) m2 m3 m4 (process Main
terminates) - o3 (overflow barrier is raised after all cars
have entered and process Overflow terminates.)
10Assignment 1 - Interleaving Mutual Exclusion
GE2. Solution (b) One possible solution, which
is based on the first attempt at mutual
exclusion in the lecture notes, is
process Overflow int max ltinput(max)gt
while (true) while (turn1) ltif
(cars100-max1) breakgt
turn1 ltopenOverflowBarriergt while
(true) while (turn1) turn1
ltif (cars100) breakgt
int turn1, cars0
process Main while(true) ltcarsgt
turn0 while (turn0)
ltopenMainBarriergt ltcloseMainBarriergt
ltif (cars100) breakgt
11Assignment 1 - Interleaving Mutual Exclusion
GE2. Solution (b) Another possible solution, is
process Overflow int max ltinput(max)gt
while (true) ltif (cars100-max1)
breakgt proceed1
ltopenOverflowBarriergt while (true)
proceed1 ltif (cars100) breakgt
int proceed1, cars0
process Main while(true) ltcarsgt
proceed0 while (proceed0)
ltopenMainBarriergt ltcloseMainBarriergt
ltif (cars100) breakgt
12Assignment 1 - Interleaving Mutual Exclusion
Unassessed Exercise 2. Consider the following
proposed solution to the mutual exclusion
problem.
int a1, b1
process P while (true) a1
non-critical-1 while (true) b1
if (b1) break c1 a0 d1
critical-1 e1 a1
process Q while (true) a2
non-critical-2 b2 b0 while (true)
c2 if (a1) break d2
critical-2 e2 b1
13Assignment 1 - Interleaving Mutual Exclusion
UE2. (i) Does the program satisfy the mutual
exclusion property? (ii) Show that the algorithm
cannot deadlock. (iii) Show that process P be may
be starved.
14Assignment 1 - Interleaving Mutual Exclusion
UE2. Solution (i) Does the program satisfy the
mutual exclusion property? No. For example,
after the sequence a1 b1 a2 b2 c2 c1 both
P and Q are in their critical sections.
15Assignment 1 - Interleaving Mutual Exclusion
UE2. Solution (ii) For deadlock to occur both
processes must be stuck in their pre-protocol
loops indefinitely. But, if P is at b1 then a
1, by initialisation or following e1 (and noting
that Q does not assign to a). And, if a 1, then
Q cannot be stuck in its loop indefinitely.
Note the phrases without loss of
generality, or a symmetrical argument applies
to Q are invalid in this case.
16Assignment 1 - Interleaving Mutual Exclusion
UE2. Solution (iii) For example, after the
sequence a2 b2 a1 the sequence of
instructions b1 c2 d2 e2 a2 b2 can be
repeated indefinitely without P ever entering its
critical section.