Tuesday, January 15, 2008

Research Topic

1.What are the major differences between deadlock, starvation, and race?
The major difference between deadlock, starvation and race is that in deadlock, the problem occurs when the jobs are processed. Starvation, however is the allocation of resource that prevents one job to be executed. Race occurs before the process has been started.
2. Give some real life example of deadlock, starvetion and race?

Deadlock: When two person are about to buy one product at the time.
Starvation: When one person borrowed a pen from his classmate and his
classmate get his pen back.
Race: When two guys have the same girlfriend.
3. Four necessary condition needed for the deadlock from exercise #2:
if the product is only one.
if the two person needed that one product urgently.
if there's other alternative products available.
if the two person are brand concious and the product happen to be what they like.
4.
Design an algorithm for using it so that both deadlock and starvation are not possible.

public boolean tryAcquire( int n0, int n1, ... ) { if ( for all i: ni ≤ availi ) { // successful acquisition availi -= ni for all i; returntrue; // indicate success } else return false; // indicate failure}

init) Semaphore s = new Semaphore(1,1);
Thread A Thread B

-------- --------

s.acquire(1,0); s.acquire(0,1);
s.acquire(0,1); s.acquire(1,0);
Thread B--------
while(true) {
s.acquire(0,1);
if ( s.tryAcquire(1,0) ) // if second acquisition succeeds
break; // leave the loop
else {
s.release(0,1); // release what is heldsleep( SOME_AMOUNT); // pause a bit before trying again

}
}

run action s.value--- ------ -------
(1,1)A s.acquire(1,0) (0,1)B s.acquire(0,1) (0,0)A s.acquire(0,1)
A blocks on secondB s.tryAcquire(1,0) => falseB s.release(0,1)
(0,1)A s.acquire(0,1) (0,0) A succeeds on second

5. figure 5.16 shows a tunnel going trough a mountain and two streets parallel to each other- one at each intrance/exit of the tunnel. Traffic light are located at each end of the tunnel to control the crossflow of tghe traffic through each intersection.
a. Deadlock will not happen because there are two traffic lights that control the traffic.The deadlock may when some of the motorist dont follow the traffic light because there's only one bridge to drive through.
b. Deadlock can be detected when there will be a huge bumper to bumper to the traffic and there will be accident that will happen.
c. The solution to prevent deadlock is that, the traffic lights should be accurate and motorist should follow it. In order to have a nice driving through the bridge.

6. Based on figure 5.17 answer the following question.
a. This is not a deadlocked.
b. There is no blocked processes.
c. P2 can freely request on R1 and R2.
d. P1 can freely request on R1 and R2.
e. Both P1 and P2 have requested R2.

1. P1 will wait after the request of P2.
2. P2 will wait after the request of P1.