CLICK HERE FOR THOUSANDS OF FREE BLOGGER TEMPLATES »

Thursday, January 17, 2008

DEADLOCKS

1.)
DEADLOCK- A set of blocked processes each holding a resource and waiting to acquire a resource held by another process in the set.

STARVATION – One process is active, but never gets the resource.

RACE- A synchronization problem between two processes vying for the same resource.

2.)
*Real-life example of deadlock is Bridge traffic can only be in one direction.
*Real-life example of starvation is in the staircase, when two people meet at the opposing side.The staircase can be accomodated by one person.

*Real-life example of Race is when two people arrived at the same time on the same door.

3.)
*MUTUAL EXCLUSION- Only one process at a time can use a resource.

*HOLD AND WAIT- A process holding at least one resource is waiting to acquire additional resources held by other processes.

*NO PREEMPTION- A resource can be released only voluntarily by the process holding it, after that process has completed its task.

*CIRCULAR WAIT- There exists a set {P0, P1, …, P0} of waiting processes such that P0 is waiting for a resource that is held by P1, P1 is waiting for a resource that is held by P2, …, Pn–1 is waiting for a resource that is held by Pn, and P0 is waiting for a resource that is held by P0.

4.)
*Algorithm for prevention of deadlock and starvation:
public boolean tryAcquire( int n0, int n1, ... ) { if ( for all i: ni ≤ availi ) { // successful acquisition availi -= ni for all i; return true; // 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 succeedsbreak; // leave the loopelse {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.)
A-Yes! When the bridge is destroyed.
B- When there are no traffic lights.
C- To prevent deadlock make all the road to be one-way.

6.)
A. NO!
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.