Monday, September 13, 2010

[CS 704D] Mutual Exclusion and Trial Algorithms- 3

Let us look at only the relevant portion of the code, look up the rest in Milan Milenkovic's book. We'll look at the p1's code, we know p2 code is similar to this one with the changes with respect to checking the status of p1.

program/module mutex3
....

var
 p1using, p2using : boolean;  { in this version we have binary variable which show which process is in the CS]

process p1;
 begin
     while true do [ do this when the p1 has been initiated]
     begin
           p1using :=true:    [set the flag that shows p1 is using the CS; this has been moved to a earlier execution position; now p1using will be set safely]        while p2using do [keeptesting]; [Busy wait if p2 is using the CS]
                Critical section
           p1using := false;   [clear the flag, let others use the CS]
        .........
        end {while}
 end{p1}


You would think after three tries, all the problems would be resolved by now. As we saw with algorithm the problem with setting of p1using does not happen here. Does that mean we have finally resolved all the issues! Looks like, not really. Under some conditions,  p1 and p2 can get each other into an infinite loop, hanging everything thereby.

Suppose p1 is preempted after it has set the p1using. p2 which is executing now will drop down to p1using and loop forever. Now let's say p1 gets control back and tries to drop to the CS and finds p2using true and will start looping on that. Thus, even though the resource is free none of the processes are getting access to it and hangs everything! No one getting an access to the resource violates one of the fundamental properties.

Having looked at all these problems, we can easily appreciate the simplicity and elegance of Djikstra's semaphore solution. I'll advise you to look at the semaphore and the operations defined and see, if any of the problems that came up with our tying the various solutions, are avoided completely or not.

No comments:

Post a Comment