• edited by
2,385 views
2 2 votes

is there deadlock in the concurrent execution of two thread given below?

void laurel() {
    lock_acquire(mutex);
    /* do something */
    
    lock_acquire(file1);
        /* write to file 1 */
 
    lock_acquire(file2);
    /* write to file 2 */
 
    lock_release(file1);
    lock_release(mutex);
 
    /* do something */
    
    lock_acquire(file1);
 
    /* read from file 1 */
    /* write to file 2 */
 
    lock_release(file2);
    lock_release(file1);
}

 

void hardy() {
        /* do stuff */
    
    lock_acquire(file1);
    /* read from file 1 */
 
    lock_acquire(file2);
    /* write to file 2 */
    
    lock_release(file1);
    lock_release(file2);
 
    lock_acquire(mutex);
    /* do something */
    lock_acquire(file1);
    /* write to file 1 */
    lock_release(file1);
    lock_release(mutex);
}

i’m not getting any deadlock. please comment below if you find any.

*Idon’t have answer. 

1 Answer

Best answer
5 5 votes

$laurel$ goes on till $\text{lock_release(mutex);} $, $laurel$ has $\text{file2}$ and $\textbf{file1}$ is free to be acquired.

$hardy$ runs $\text{lock_acquire(file1);}$, $hardy$ has $\textbf{file1}$

$laurel$ runs  $\text{lock_acquire(file1);}$ which is acquired by $hardy$, and $hardy$ runs  $\text{lock_acquire(file2);}$ which is acquired by $laurel$. So, this is a deadlock.

• edited by
Position:
Show:

Related questions

3 3 votes
1 1 answer
1.0k
1.0k views
1 1 vote
1 1 answer
1.4k
1.4k views
SamAddy asked Jan 20, 2019
1,367 views
Will there be Mutual Exclusion & Deadlock ?
0 0 votes
1 1 answer
719
719 views
Rackson asked Dec 14, 2018
719 views
Please describe the Deadlock Condition in synchronization and when will be synchronization possible and condition ..?