Playville is a tiny town in fictional middle earth, which is inhabited by dwarves and elves. Playville has a playground to which dwarves and elves go if they wish to play. The sporting rules of Playville state that a play can take place only if either there are 5 dwarves or 3 elves present to play. If the playing condition is not met (i.e., 5 dwarves or 3 elves are not present at the playground), an arriving person (dwarf or elf) has to wait in their respective waiting area near the playground. When either the dwarves or the elves have their quorum, they move on to the playground to play, if the playground is already not in use. The playground, at any time, is either empty or it is occupied by playing dwarves or it is occupied by playing elves.

After a dwarf or elf team has played to its satisfaction, it leaves the ground. Assume all dwarves are identical to each other, and all elves are identical to each other too.

a. Assuming dwarves and elves to be concurrent processes, and using P and V operations, devise an algorithm for each type of process.
b. Is your solution correct?
c. Discuss possibly what you could do to improve your solution.

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 .