Partial Solutions to Mid Term Exam
May, 2008
Dr. T.L. Yu
|
|
a) Does the code achieve mutual exclusion?
b) Is deadlock possible for T1 and T2? Why? If your answer is yes, modify the codes so that the two threads are deadlock free; if your answer is no, prove it.
a)Yes, it is because a thread can enter the critical section
if only it has acquired the two semaphores ( "keys" ).
If one thread holds a semaphore, the other cannot have it.
b)Yes. Because threads run concurrently, it could happen that
T1 has executed down ( S1 ) and T2 has executed
down ( S2 ) . So now T1 is waiting for T2 to release ( UP )
S2 and T2 is waiting for T1 to release S1 and thus they in deadlock.
To prevent deadlock, we can simply require both T1 and T2
to 'lock' ( down ) semphores in the same order S1, S2 so that whoever
has first locked S1 can proceed to lock S2 and the other thread has to wait
on S1. The following is
the modified
code that is deadlock free:
T1:
down ( S1 );
down ( S2 );
CS();
up ( S2 );
up ( S1 );
T2:
down ( S1 );
down ( S2 );
CS();
up ( S2 );
up ( S1 );
| Client | max. need ( in million $ ) | |
|---|---|---|
| A | 6 | |
| B | 4 | |
| C | 3 | |
| D | 3.2 | |
| E | 0.5 |
A a certain time, the bank has lent the following amount of money to the clients.
| Client | outstanding loan( in million $ ) | |
|---|---|---|
| A | 3 | |
| B | 2 | |
| C | 2 | |
| D | 1.5 | |
| E | 0.3 |
a)Is the bank in a safe state? Why?
b)Suppose client D asks for another $1 M ( million ), should the
bank loan the money to her? Why? ( Show your steps clearly. )
a) Yes, the bank is in a safe state. This is because the money
available in the bank is
available = 10 - ( 3 + 2 + 2 + 1.5 + 0.3 ) = 10 - 8.8 = 1.2
So it has enough money to terminate E, who would return 0.3 to and
available becomes
available = 1.2 + 0.3 = 1.5
It then has enough money to terminate C ( who may still need 1 )
then D and so on. Eventually, all clients can finish their projects.
The following table shows the 'terminating' steps:
| Client | money loaned |
max. need | still need | Avail > still need? | terminate order | new avail after terminate |
|---|---|---|---|---|---|---|
| A | 3 | 5 | 2 | Yes ( 3.5 > 2 ) | 3 | 6.5 |
| B | 2 | 4 | 2 | Yes ( 6.5 > 2 ) | 4 | 8.5 |
| C | 2 | 3 | 1 | Yes ( 1.5 > 1 ) | 2 | 3.5 |
| D | 1.5 | 3.2 | 1.7 | Yes ( 8.5 > 1.7 ) | 5 | 10 |
| E | 0.3 | 0.5 | 0.2 | Yes ( 1.2 > 0.2 ) | 1 | 1.5 |
b) No, the bank should not loan her because if the bank does so, it would have available = 0.2 and would be in an unsafe state since it could not 'terminate' all the clients as shown in the following table.
| Client | money loaned |
max. need | still need | Avail > still need? | terminate order | new avail after terminate |
|---|---|---|---|---|---|---|
| A | 3 | 5 | 2 | No ( 0.5 < 2 ) | 5 | 0.5 |
| B | 2 | 4 | 2 | No ( 0.5 < 2 ) | 4 | 0.5 |
| C | 2 | 3 | 1 | No ( 0.5 < 1 ) | 3 | 0.5 |
| D | 2.5 | 3.2 | 0.7 | No ( 0.5 < 0.7 ) | 2 | 0.5 |
| E | 0.2 | 0.5 | 0.2 | Yes ( 0.2 ≥ 0.2 ) | 1 | 0.5 |
| C3 = | ![]() |
1 2 3 1 |
![]() |
tm = | ![]() |
1 3 3 1 |
![]() |
Should P3 deliver the message immediately? Why?
| C2 = | ![]() |
1 2 3 4 |
![]() |
tm = | ![]() |
2 2 3 5 |
![]() |
Should P2 deliver the message immediately? Why?
If Pj has received a message from Pi, it delivers
the message immediately when two conditions are satisfied:
b) No. Because condition 2 is violated ( Note: j=2, i=1 here ):
a) Yes, P3 should deliver the message immediately because the
two conditions are satisfied ( note: i = 2 ):
You should draw the diagrams also.
| C1 = | ![]() |
1 0 0 |
![]() |
C2 = | ![]() |
2 2 0 |
![]() |
C3 = | ![]() |
0 1 3 |
![]() |
(i)
(ii)No, it is not a consistent cut. The Theorem states that
if C = { c1, c2, ... ,cn } is
a cut with vector time TC, then the cut
is consistent iff
But here TC[1] = 2 ≠ 1 = C1[1]. Therefore,
it cannot be a consistent cut.
TC =

2
2
3 
TC =
C1[1]
C2[2]
.
.
Cn[n]
P1 initiated the recording process and right before P1 sent out the marker, a $1 transaction is in transit on C12, a $2 transit on C21, a $3 transit on C13, and a $4 transit on C23 ( assume that the units are in million dollars ) and branches P1, P2, and P3 have money $6, $8, $10 respectively ( not including any money in transit ). Assume that during the recording process, after a branch has received the money in transit, it has not sent out any additional money. With the help of diagrams, answer the following questions.
| State | money |
|---|---|
| LS1 | .. |
| LS2 | .. |
| LS3 | .. |
| C12 | .. |
| C13 | .. |
| C21 | .. |
| C23 | .. |
| C31 | .. |
| C32 | .. |
For details, please refer to your notes.
Answers:
State money LS1 $6 LS2 $9 LS3 $13 C12 0 C13 0 C21 $2 C23 $4 C31 0 C32 0
![]() |
1 0 0 |
![]() |
, | ![]() |
0 1 0 |
![]() |
, | ![]() |
0 0 1 |
![]() |
a) Draw a diagram to show all the transitions and events.
b) Find the vector clocks of events e16, e26 and e36.
c) Draw on your diagram and state explicitly to give an example for each of the following global states. Your global state should be consisted of the the events given ( e.g. e11 ) but should not contain any event that is sending ( e.g. e12 ) or receiving a message ( e.g. e22 ).
b) c)
C(e26) = (2, 6, 5 )
C(e36) = (0, 1, 6 )