Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In an electronic funds transfer system, there are hundreds of identical processe

ID: 3931360 • Letter: I

Question

In an electronic funds transfer system, there are hundreds of identical processes that work as follows. Each process reads an input line specifying an amount of money, the account to be credited, and the account to be debited. Then it locks both accounts and transfers the money, releasing the locks when done. With many processes running in parallel, there is a very real danger that having locked account x it will be unable to lock y because y has been locked by a process now waiting for x. Devise a scheme that avoids deadlocks. Do not release an account record until you have completed the transactions. (In other words, solutions that lock one account and then release it immediately if the other is locked, are incorrect.)

Explanation / Answer

The key to avoiding deadlock is to acquire the locks in some fixed order, say in increasing numeric order. In this way, there can be no cycle of lock holds:

Transfer(int A1, int A2, int Amount)

{

if (A1==A2) return;

if (A1<A2) { lock(A1); lock(A2);

}

else {lock(A2); lock(A1);

}

unlock(A1); unlock(A2);

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote