P(s1);
a++;
P(s2);
v++;
V(s2);
V(s1);
(s1, s2 are semaphores). All variables are automatic. Now, consider two threads running this fragment of code simultaneously, can there be a deadlock? Why, or why not?
if(a > 0)
P(s1);
else
P(s2);
b++;
P(s3);
if(b < 0 && a <= 0)
P(s1);
else if(b >= 0 && a > 0)
P(s2);
else
P(s4);
a++;
V(s4);
V(s3);
V(s2);
V(s1);
s1, s2, s3 and s4 are semaphores. All variables are automatic. Now, consider two threads running this fragment of code simultaneously, can there be a deadlock? Why, or why not?
while(1)where Table is a shared object that encapsulates the system's shared state. As a result of the function Table::Work(), the philosopher makes enough soup to fill a bowl. Similarly, the result of the function Table::Eat() is that a philosopher picks a bowl and consumes all the soup in it. Note that there is no correspondence between bowls and philosophers, i.e., a philosopher will eat from any available bowl that contains soup, and will fill any available empty bowl.
{
flip the coin
if(head) Table.Work()
else Table.Eat()
Think()
}