"You know better than to trust a strange computer!"
- C3PO, The Empire Strikes Back
Last time we started to look at the producer-consumer problem. Here, we consider the bounded buffer case.
Bounded Buffer, buffer size n
For simplicity, we will assume the objects being produced and consumed are int values.
This solution leaves one buffer entry empty at all times:
int buffer[n]; int in=0; int out=0;
while (1) { ... produce item; ... while (((in+1)%n) == out); /* busy wait */ buffer[in]=item; in=(in+1)%n; }
while (1) { while (in==out); /* busy wait */ item=buffer[out]; out=(out+1)%n; ... consume item; ... }
Is there any danger with this solution in terms of concurrency? Remember that these processes can be interleaved in any order - the system could preempt the producer at any time and run the consumer.. Things to be careful about are shared references to variables.
Note that only one of the processes can modify the variables in and out. Both use the values, but only the producer modifies in and only the consumer modifies out. Try to come up with a situation that causes incorrect behavior - hopefully you cannot.
Perhaps we want to use the entire buffer...let's add a variable to keep track of how many items are in the buffer, so we can tell the difference between an empty and a full buffer:
int buffer[n]; int in=0; int out=0; int counter=0;
while (1) { ... produce item; ... while (counter==n); /* busy wait */ buffer[in]=item; in=(in+1)%n; counter=counter+1; }
while (1) { while (counter==0); /* busy wait */ item=buffer[out]; out=(out+1)%n; counter=counter-1; ... consume item; ... }
We can now use the entire buffer. However, there is a potential danger here. We modify counter in both the producer and the consumer.
Everything looks fine, but let's think about how a computer actually executes those statements to increment or decrement counter.
counter++ really requires three machine instructions: (i) load a register with the value of counter's memory location, (ii) increment the register, and (iii) store the register value back in counter's memory location. There's no reason that the operating system can't switch the process out in the middle of this.
Consider the two statements that modify counter:
Producer | Consumer | ||
P1 | R0 = counter; | C1 | R1 = counter; |
P2 | R0 = R0 + 1; | C2 | R1 = R1 - 1; |
P3 | counter = R0; | C3 | counter = R1; |
Consider one possible ordering: P1 P2 C1 P3 C2 C3 , where counter=17 before starting. Uh oh.
What we have here is a race condition. We need to make sure that when one process starts modifying counter, that it finishes before the other can try to modify it. This requires synchronization of the processes.
If there were mutliple producers or consumers, we would have the same issue with the modification of in and out.
We need to make those statements that increment and decrement counter atomic. We say that the modification of counter is a critical section.
The Critical-Section problem:
Any solution to the critical section problem must satisfy three conditions:
We first attempt to solve this for two processes, P0 and P1. They share some variables to synchronize. We fill in <CS Entry> and <CS Exit> from above with code that should satisfy the three conditions.
Critical Section Algorithm 1
int turn=0;
while (1) { while (turn!=i); /* busy wait */ /* critical section */ turn=j; /* non-critical section */ }
Note the semicolon at the end of the while statement's condition at the line labeled "busy wait" above. This means that Pi just keeps comparing turn to i over and over until it succeeds. This is sometimes called a spin lock. For now, this is our only method of making one process wait for something to happen. More on this later.
This does satisfy mutual exclusion, but not progress (alternation is forced).
Critical Section Algorithm 2
We'll avoid this alternation problem by having a process wait only when the other has "indicated interest" in the critical section.
boolean flag[2]; flag[0]=false; flag[1]=false;
while (1) { flag[i]=true; while (flag[j]); /* critical section */ flag[i]=false; /* non-critical section */ }
flag[i] set to true means that Pi is requsting access to the critical section.
This one also satisties mutual exclusion, but not progress.
If we swap the order of the flag[i]=true; and while (flag[j]); statements, we no longer satisfy mutual exclusion.
Critical Section Algorithm 3
We combine the two previous approaches:
int turn=0; boolean flag[2]; flag[0]=false; flag[1]=false;
while (1) { flag[i]=true; turn=j; while (flag[j] && turn==j); /* critical section */ flag[i]=false; /* non-critical section */ }
So, we first indicate interest. Then we set turn=j;, effectively saying "no, you first" to the other process. Even if both processes are interested and both get to the while loop at the "same" time, only one can proceed. Whoever set turn first gets to go first.
This one satisfies all three of our conditions. This is known as Peterson's Algorithm.
Bakery algorithm
Can we generalize this for n processes? The Bakery Algorithm (think deli/bakery "now serving customer X" systems) does this.
The idea is that each process, when it wants to enter the critical section, takes a number. Whoever has the smallest number gets to go in. This is more complex than the bakery ticket-spitters because two processes may grab the same number (to guarantee that they wouldn't would require mutual exclusion - exactly the thing we're trying to implement), and because there is no attendant to call out the next number - the processes all must come to agreement on who should proceed next into the critical section.
We break ties by allowing the process with the lower process identifier (PID) to proceed. For Pi, we call it i. This assumes PIDs from 0 to n-1 for n processes, but this can be generalized.
Although two processes that try to pick a number at about the same time may get the same number, we do guarantee that once a process with number k is in, all processes choosing numbers will get a number > k.
Notation used below: an ordered pair (number, pid) fully identifies a process' number. We define a lexicographic order of these:
The algorithm:
boolean choosing[n]; int number[n];
while (1) { choosing[i]=true; number[i]=max(number[0],number[i],...,number[n-1])+1; choosing[i]=false; for (j=0; j<n; j++) { while (choosing[j]); while ((number[j]!=0) && ((number[j],j) < (number[i],i))); } /* critical section */ number[i]=0; /* non-critical section */ }
So great, we have a solution. But...problems: