operating system - Visually where is the Deadlock? Why? How to avoid/fix? -


i trying understand , solve following problem.

the following program attempts use pair of semaphores t , s mutual exclusion.

initially: s = 1, t = 0.

    thread 1    thread 2     --------    -------- 1.  p(s);       p(s); 2.  v(s);       v(s); 3.  p(t);       p(t); 4.  v(t);       v(t); 

please remember that:

the p operation wastes time or sleeps until resource protected semaphore becomes available, @ time resource claimed.

the v operation inverse: makes resource available again after process has finished using it.


1. why program cause deadlock?

2. changes can made initial semaphore values remove deadlock potential?


update

according comments able better understand , illustrate deadlock visually below, please let me know if understood correctly.

here how deadlock happens, if thread 1 gets cpu time before thread 2:

enter image description here

how fix this?

set value of t 1

the deadlock @ line 3. @ line both threads waiting continuously acquire lock on resource t. initial value of t 0, means in locked state, example if thread1 reaches first line 3 waits till value of t becomes 1 , after time thread2 wait @ same line t become 1. in way both process wait continuously resource creating `deadlock.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -