1. Mutexes is a piece of code written in entry section of a program which will lock the shared resource not to be used by any other program/process until the process critical section is completely executed. In other words we can say that mutex is implemented to prevent a programs getting into a Race condition.
2. This code will run into deadlock situation as both the programs is trying to access resource R2 simultaneously. In this code function P1 is using a resource R1 already and requires another resource but the at the same time function P2 is holding resource R2 and therefore a deadlock situation is getting arrived.
When process P1 is passing second lock m2 for accessing resource R2 at the same time process P2 is trying to lock 2nd resource. This is exactly where the deadlock situation is getting arrived
3. This best way to avoid deadlock problem is to make sure whenever different processes lock multiple mutexes,they should do in the same order . When locks are taken in prescribed order ,deadlock will not arrive.
so process P2 should be written as mentioned below
void P2(){
atomic_lock (&m1);
atomic_lock(&m2);
// run critical region...
unlock (&m1);
unlock(&m2);
}
The below pseudocode shows functions p1 and p2, used by two separate processes. These processes share...