Respuesta :
Answer:
See explaination
Explanation:
function DWARVES()
{
num(dwarves) = num(dwarves)+1;
while (num(dwarves)!=5) ;
P(Ground)
Critical Section code;
V(Ground)
num(dwarves) = 0;
}
Function ELVES()
{
num(Elves) = num(Elves) + 1;
if(num(Elves) ! =5);
P(Ground)
Critical section code;
V(Ground)
num(Elves) = 0
}
Algo Explained -
We have two functions in this algo. First one is for Dwarves and second one is for elves. When any Dwarve execute it goes to its function increment number of dwarves and wait in a loop created by while loop untill number of dwarves become equal to 5 . If number of dwarf become equal to 5 it tries to enter into critical section of code . P(ground) means obtainig lock on ground . and V(Ground) means releasing that lock. If dwarves are using the grond means they are executing the critical section then untill they execute the critical section no Elves should execute critical section. For that binary semaphores or mutexes has been used . Similarly function Elves is implemented
ii) This algorithm should work fine. But If we get a situation like when dwarves are executing the critical section and got preempted before releasing the locks then there may arise a condition of starvation for Elves . Starvation is a case of longer waiting deadlock is a case of infinite waiting.
iii) So to improve our code we can add some timer so that after that each of dwarves or Elves should release the ground .