edited by
802 views
2 votes
2 votes

Unable to understand the following text taken from Galvin 9th edition Chapter 7 Deadlock Page 326. 

Imposing a lock ordering does not guarantee deadlock prevention if locks can be acquired dynamically. For example, assume we have a function that transfers funds between two accounts. To prevent a race condition, each account has an associated mutex lock that is obtained from a get lock() function such as shown in the following program:

void transaction(Account from, Account to, double amount)
{ 
           mutex lock1, lock2; 
           lock1 = get lock(from); 
           lock2 = get lock(to);
           
           acquire(lock1);
              acquire(lock2);
                withdraw(from, amount);
                deposit(to, amount);
              release(lock2);
           release(lock1);
}

Deadlock is possible if two threads simultaneously invoke the transaction() function, transposing different accounts. That is, one thread might invoke

transaction(checking account, savings account, 25);

and another might invoke

transaction(savings account, checking account, 50);

edited by

1 Answer

0 votes
0 votes

Alright so I would suggest you look at the code snippet given before this (Figure 7.4). 

After you have understood that consider the code snippet in your query:

void transaction(Account from, Account to, double amount)
{ 
           mutex lock1, lock2; 
           lock1 = get lock(from); 
           lock2 = get lock(to);
           
           acquire(lock1);
              acquire(lock2);
                withdraw(from, amount);
                deposit(to, amount);
              release(lock2);
           release(lock1);
}

Here unlike the 'actual' method of giving the resources an order (such as F(mutex_1) = 1, F(mutex_2) = 2 ) we give the ordering dynamically in the above code snippet here:

lock1 = get lock(from);  

lock2 = get lock(to);

Now look at the function calls:

transaction(checking account, savings account, 25);

transaction(savings account, checking account, 50);

Look at the first two parameters of both transactions very carefully. 

In the first transaction the call would be something like this:  

lock1 = get lock(checking account);  (assume this initializes lock1 as '1')

lock2 = get lock(savings account);    (assume this initializes lock2 as '2')

Do the same for the 2nd transaction and you will see that the second transaction will try to acquire lock in the order 2,1 which is what we're trying to avoid using "lock ordering". This is equivalent to the code snippet I asked you to read before.                       

Related questions

2 votes
2 votes
1 answer
1
NIKU asked Oct 29, 2017
3,291 views
There are 5 processes and 10 instances of a Resource. If each process needs ‘P’ instances which is the minimum value of ‘P’ for the deadlock to occur?(a) 1 ...
2 votes
2 votes
0 answers
3
Rishabh Gupta 2 asked Nov 25, 2017
563 views
In any of the deadlock prevention schemes, is it possible that the request will be granted even if it leaves the system to an unsafe state (but not in deadlock state)?