UNIT-3 Full
UNIT-3 Full
Deadlocks: System Model, Deadlock Characterization, Methods for Handling Deadlocks, Deadlock
Prevention, Deadlock Avoidance, Deadlock Detection, Recovery from Deadlock.
• As we can see from Fig: Buffer has total 8 spaces out of which
the first 5 are filled, in = 5(pointing next empty position) and
out = 0(pointing first filled position). • Since count = count + 1; is divided into three
parts:
• Let's start with the producer who wanted to produce an
element " F ", according to code it will enter into the • Load Rp, m[count] → will copy count value which is
producer() function, while(1) will always be true, itemP = F will 5 to register Rp.
be tried to insert into the buffer, before that while(count == n); • Increment Rp → will increment Rp to 6.
will evaluate to be False.
• Suppose just after Increment and before the
execution of third line (store m[count], Rp) Context
Switch occurs and code jumps to consumer code. .
.
VINUTHA M S, DEPT OF CSE,Dr AIT 3
Consumer Code: • Since count = count - 1; is divided into three parts:
• Now starting consumer who wanted to consume the first • Load Rc, m[count] → will copy count value which is
element " A ", according to code it will enter into the 5 to register Rp.
consumer() function, while(1) will always be true,
while(count == 0); will evaluate to be False( since the • Decrement Rc → will decrement Rc to 4.
count is still 5, which is not equal to 0. • store m[count], Rc → count = 4.
• itemC = Buffer[out]→ itemC = A ( since out is 0) • Now the current value of count is 4
• out = (out + 1) mod n → (0 + 1)mod 8→ 1, therefore out = • Suppose after this Context Switch occurs back to
1( first filled position) the leftover part of producer code. . .
• A is removed now • Since context switch at producer code was occurred
• After removal of A, Buffer look like this after Increment and before the execution of the third
line (store m[count], Rp)
• Where out = 1, and in = 6
• So we resume from here since Rp holds 6 as
incremented value
• Hence store m[count], Rp → count = 6
• Now the current value of count is 6, which is
wrong as Buffer has only 5 elements, this
condition is known as Race Condition and
Problem is Producer-Consumer Problem.
• The problem arises when all of the philosophers pick up their left
fork at the same time, and then all try to pick up their right fork at
the same time. This results in a deadlock, where no philosopher
can start eating.
Deadlocks: System Model, Deadlock Characterization, Methods for Handling Deadlocks, Deadlock
Prevention, Deadlock Avoidance, Deadlock Detection, Recovery from Deadlock.