Goals: This laboratory exercise provides practice with shared memory and process synchronization in Linux, focusing on the Bounded Buffer problem.
Collaboration: You will complete this lab in teams of 2 to 3 as assigned by the instructor. You may, as always, consult with your classmates on issues of design and debugging.
~davisjan/csc/213/examples/bounded-buffer-1.c
to your account, compile it, and run it a few times. Describe
the
output you get. Review the source code and explain briefly
how
the output is produced.sleep
statement from the child process, rerun bounded-buffer-1.c,
and explain the output produced.sleep
statement to the child process, and remove it from the parent process.
Again, rerun bounded-buffer-1.c, and
explain the output produced.sleep
statements to synchronize the two processes, consider the use of
busy-waiting. In this approach, when the value stored in
shared
memory is -1, then the parent is allowed to write the next value.
When the value is not -1, the child will read it.sleep
statement for the parent with a while statement that waits until the
shared memory contains the value -1. When this condition occurs, the
parent may write the next square into the shared memory.sleep
statement for the child with a while statement that waits until the
shared memory does not contain the value -1. When this
condition
occurs, the child may read the next square from the shared memory.
At the end of the child's loop, the value in shared memory
should
be reset to -1. (Why?)~davisjan/csc/213/examples/bounded-buffer-2.c
to your account. Compile it, run it a few times, and review
the code to make sure you understand how it works.bounded-buffer-2.c
will work when there are multiple readers or when there are multiple
writers. In each case, if the code would work, explain why.
If the code would not work, give a timing sequence involving
the
several readers and/or writers showing what would go wrong.~davisjan/csc/213/examples/bounded-buffer-3.c
to your account. Compile it, run it a few times, and review
it to be sure you understand how it works.bounded-buffer-3.c,
in place of the busy-waits in Step 4 to handle the synchronization of
the reader and the writer process from bounded-buffer-1.c.
Your final program should neither busy-wait nor use sleep
statements.~davisjan/csc/213/examples/bounded-buffer-4.c
to your account, compile it, and run it.mutex.
Explain the purpose of this semaphore. Specifically, if the mutex
semaphore were omitted,
give a timing sequence involving the several readers and/or writers
should what might go wrong.~davisjan/csc/213/examples/bounded-buffer-4.c
prevents
any reader from working at the same time as any writer. Assuming that
the buffer contains several locations, however, writing to one buffer
location should not interfere with reading from another. That
is,
the critical section for readers is not the same as the critical
section for writers. Remove the mutex
semaphore and
add additional semaphores, as needed, so that some reader could work
concurrently with some writer (assuming the buffer was neither empty
nor completely full---so that both reading and writing make sense.)Consider the following problem. A program is to be written to print all numbers between 1 and 1000 (inclusive) that are not (evenly) divisible by either 2 or 3.
The problem is to be solved using three processes (P0, P1, P2) and two one-integer buffers (B0 and B1) as follows:

Janet Davis (davisjan@cs.grinnell.edu)
Created September 26, 2006 based on http://www.cs.grinnell.edu/~walker/courses/213.fa04/lab-semaphores.shtml