• edited by
1,161 views
2 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);

1 Answer

0 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.                       

Position:
Show:

Related questions

3 3 votes
1 answers 1 answer
4.0k
4.0k views
NIKU asked Oct 29, 2017
4,017 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 (...
4 4 votes
1 1 answer
131
131 views
GO Classes asked Aug 19
131 views
Consider the following statements concerning deadlocks:Deadlock prevention is based on identifying a sufficient condition for deadlock and designing resource-allocation a...
3 3 votes
1 1 answer
84
84 views
GO Classes asked Aug 19
84 views
A banking system has one exclusive lock $L_i$ for every account $i$. A transfer from account $x$ to account $y$ requires both $L_x$ and $L_y$ to be held simultaneously.Tr...
3 3 votes
1 1 answer
95
95 views
GO Classes asked Aug 19
95 views
Consider the following deadlock-prevention policies:Impose a global ordering on resource types and permit a process to request resources only in increasing order. If a pr...