Lecture 6 - CPU Scheduling 3: Multiprocessors, Linux 2.6, FreeBSD ULE. Cooperating Processes, Bounded Buffer Problem
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;
...
}
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;
...
}