The following pseudo-code shows a function that has to work on a linked
list that is shared by several threads.
// Function to unlink the head node of a shared linked list
// and return a pointer to the node. If the list is empty,
// then a null pointer is returned.
Nodepointer getNode(Linkedlist L)
{
// Lock the list
acquire(L.semaphore);
// See if the list is empty and return if it is
if (L.headnode == null)
return(null);
// Find the first node from the list
Nodepointer n = L.headnode;
// Point the list's head at the node after n,
// and return the node n
L.headnode = n.next;
release(L.semaphore);
return(n);
}
- Why must the line release(L.semaphore); occur after the line
L.headnode= n.next; What problems might occur if the lines
were reversed?
- Given the function as it is written, how is it possible for the threads
using this function to suffer starvation?