CSC 325 Operating Systems with an Emphasis on UNIX
Teamwork 4

Organize yourselves into groups of two or three students. Discuss the following problems in your group. Elect one person to be group scribe to type up your team's answers.

In these problems cooperating processes share the following:

 const int BUFFER_SIZE; //This is the buffer size
 typedef item …; // item is of some type or class….
 item buffer[BUFFER_SIZE]; //array is of type item;
 int in=0, out =0;

The variables in and out are initialized to 0. The shared buffer is basically a “circular” array with integer “pointers” in and out where in points to the next free position and out to the first full position in the buffer. Elect one person to be group scribe to write up your group answers. nextp and nextc are local variables in which items to be produced or consumed are stored.

1. Bounded Buffer Problem

Producer process pseudo-code:

int nextp=0;
while (true)
{
 //produce an item in nextp;

 while((in+1)%BUFFER_SIZE==out); //do no-op;
 buffer[in]=nextp;
 in=(in+1)%BUFFER_SIZE;
}

Consumer process pseudo-code:

int nextc=0;
while (true)
{
 while(in==out); //do no-op;
 nextc=buffer[out];
 out=(out+1)%BUFFER_SIZE;

 //consume next item in nextc

}

  1. Draw a diagram to demonstrate a typical production of an item.
  2. Draw a diagram to demonstrate a typical consumption of an item.
  3. Draw a diagram to explain why the buffer is empty when in==out;
  4. Draw a diagram to explain why the buffer is full when (in + 1) % BUFFER_SIZE == out.
  5. Explain why this solution allows at most BUFFER_SIZE items in the buffer at the same time.
  6. Then explain how to modify this code to allow n items in the buffer at a time.

2. Bounded Buffer with Counter Problem

int counter = 0; //Assume that the global counter is initialized to 0.
Producer process pseudo-code:

do
{
//produce an item in nextp;

 while(counter==BUFFER_SIZE); //do no-op;
 buffer[in]=nextp;
 in=(in+1)%BUFFER_SIZE;
 counter=counter+1;
}while (true);

Consumer process pseudo-code:

do
{
 while(counter==0); //do no-op;
 nextc=buffer[out];
 out=(out+1)%BUFFER_SIZE;
 counter=counter-1;

 //consume the next item in nextc
}
while (true);

  1. Though these execute fine in sequence, they may cause problems if they are running concurrently on a time-sharing system. Give a concrete example (which is different than the one in the text and discussed in class) of how such a "concurrency" problem could occur.

Save your text file as yourlastnameT4.txt.