in Operating System edited by
1,085 views
2 votes
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. 

in Operating System edited by
1.1k views

4 Comments

@srestha

Ma'am thanks for the edit :D.

0
0

@aambazinga

welcome :)

But from where u got this question?

Is all locks shared lock or exclusive?

moreover can same lock applied to file1 and file2 at the same time?

0
0
edited by

@srestha Ma'am,

this is some assignment's question. https://cgi.cse.unsw.edu.au/~cs3231/14s1/assignments/asst1/

all locks are exclusive

3 locks are there.. mutex, file1, file2.

i've missed to add these points. 

 

0
0

1 Answer

5 votes
5 votes
Best answer

$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

4 Comments

@Shubhgupta

why can't we assumed always best case?

if nothing given, we assume the worst case, not the best. if assuming best, then why not assume that they both can write simultaneously as well?

if we start making assumptions as per our convenience, every other answer would be different.

0
0
I am not arguing any statement. I am just asking whether it can be possibility or not. Question needs to specify the clarity if there is nothing mentioned then yes every one can put their perspective.
0
0

@Shubhgupta

you are not wrong,if read lock or write lock would be acquired explicitly. 

but here there is is no individual read/write lock. only one lock is there for both the operations

after acquiring what they are doing is not visible outside, so we can't say na, that okay this time laurel is reading the file1, let's share this lock to hardy,,... or next time it is writing on file1, it can't be shared. this solution would create complicacy. isn't it?

i think this is one possible explanation to your doubt. :)

0
0