Answer:
See Explaination
Step-by-step explanation:
Separating this critical section into two sections:
void transaction(Account from, Account to, double amount)
{
Semaphore lock1, lock2;
lock1 = getLock(from);
lock2 = getLock(to);
wait(lock1);
withdraw(from, amount);
signal(lock1);
wait(lock2);
deposit(to, amount);
signal(lock2);
}
will be the best solution in the real world, because separating this critical section doesn't lead to any unwanted state between them (that is, there will never be a situation, where there is more money withdrawn, than it is possible).
The simple solution to leave the critical section in one piece, is to ensure that lock order is the same in all transactions. In this case you can sort the locks and choose the smaller one for locking first.