Question: Given the following pseudo code: // Transfer from account acc_from to account acc_to, // where each account has a mutex field and an amount field
Given the following pseudo code:
// Transfer from account acc_from to account acc_to, // where each account has a mutex field and an amount field bool Transfer (amount, acc_from, acc_to) { pthread_mutex_lock(&acc_from.mutex); pthread_mutex_lock(&acc_to.mutex); if (acc_from.balance < amount) return ERROR; acc_from.balance -= amount; acc_to.balance += amount; pthread_mutex_unlock(&acc_from.mutex); pthread_mutex_unlock(&acc_to.mutex); return SUCCESS; } Client1() { Transfer (amount1, account1, account2); } Client2() { Transfer (amount2, account2, account1); } int main(){ // Some code to initialize two global accounts: account1 and account2 ... // The two threads pthread_create(&tid1, NULL, Client1, NULL); pthread_create(&tid2, NULL, Client2, NULL); } 1. Find and describe a potential deadlock in the pseudo code.
2. Now you are given a function Order(pthread_mutex_t *m1, pthread_mutex_t *m2), which returns 0 if m1 and m2 are the same and returns 1 or -1 if m1 and m2 are different. This Order() function has the following properties: if Order(&mutexA, &mutexB) returns value X (where X is not equal to 0), then Order(&mutexB, &mutexA) returns value -X; if Order(&mutexA, &mutexB) and Order(&mutexB, &mutexC) return the same value X, then Order(&mutexA, &mutexC) also return value X. With such a function, how would you fix the potential deadlock. Please describe.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
