This is from a textbook on operating systems. This is not homework, just part of the chapter on deadlocks.
transaction(Account from, Account to, Number amount):
mutex lock1 = getlock(from)
mutex lock2 = getlock(to)
acquire(lock1)
acquire(lock2)
withdraw(from, amount)
deposit(to, amount)
release(lock2)
release(lock1)
Deadlock is possible if two threads simultaneously invoke thetransaction() function, transposing different accounts. That is, one thread might invoke
transaction(checkingaccount, savingsaccount, 25)
and another might invoke
transaction(savingsaccount, checkingaccount, 50)
What is a good solution to this problem? One I can think of is to use a witness program that will alert the user that a deadlock has occurred, but there must be a better solution implementable by modifying the code. Any ideas?