| Current Path : /var/www/html/bibhas.ghoshal/lab_files/ |
| Current File : /var/www/html/bibhas.ghoshal/lab_files/Assignment6.2.html |
Shared Memory and Process Synchronization in Linux
*Summary:* This laboratory exercise provides practice with shared memory
and process synchronization in Linux.
Preparation
1.
Review sample programs 7-10 in An Introduction to Concurrency in
Unix-based [GNU] C Through Annotated Examples
<http://www.cs.grinnell.edu/%7Ewalker/c/concurrency-index.html>
2.
Review the corresponding Linux-based programs (read-write-1.c
through read-write-4.c)
Experiments with Shared Memory
3.
Copy ~walker/c/concurrency/read-write-1.c
<http://www.cs.grinnell.edu/%7Ewalker/c/concurrency/read-write-1.c>
to your account, compile it with gcc, and run it a few times.
Describe the output you get, and explain briefly how it is produced.
4.
As we did in class, remove the sleep statement from the child
process, rerun read-write-1.c, and explain the output produced.
5.
Restore the sleep statement from the previous step, and remove it
from the parent process. Again, rerun read-write-1.c, and explain
the output produced.
6.
Rather than rely upon sleep statements to synchronize the two
processes, consider the use of spinlocks. In this approach, the
parent will write to shared memory when the memory location contains
the value -1, and the child will read when the memory location is
not -1.
1.
Initialize the shared memory location in main memory to -1
before the fork operation. (Why must this be done before the fork?)
2.
Replace the sleep statement for the child by a spinlock that
checks that the shared memory contains a nonnegative value. At
the end of the child's loop, the shared memory location should
be reset to -1.
3.
Remove the sleep statement from the parent at the end of the
loop, and insert a spinlock at the beginning of the loop that
checks that memory is -1. When this condition occurs, the parent
may write the next nonnegative number to shared memory.
7.
Copy programs ~walker/c/concurrency/read-write-2.c
<http://www.cs.grinnell.edu/%7Ewalker/c/concurrency/read-write-2.c>
to your account. Then, compile read-write-2.c, run them a few times,
and review the code to be sure you understand how the programs work.
8.
Explain whether read-write-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 might go wrong.
Experiments with Semaphores
9.
Copy ~walker/c/concurrency/read-write-3.c
<http://www.cs.grinnell.edu/%7Ewalker/c/concurrency/read-write-3.c>,
to your account, compile it with gcc and run it.
10.
Review read-write-3.c to be sure you understand how it works.
11.
Use the idea of semaphores, as implemented in read-write-3.c, in
place of the spinlocks in Step 6 to handle the synchronization of
the reader and writer process from read-write-1.c. Your final
program should not use either spinlocks or sleep statements.
12.
With this use of semaphores, explain whether your code in step 11
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 might go wrong.
Experiments with Multiple Readers and Multiple Writers
13.
Copy ~walker/c/concurrency/read-write-4.c
<http://www.cs.grinnell.edu/%7Ewalker/c/concurrency/read-write-4.c>,
to your account, compile it with gcc and run it.
14.
This program contains a third semaphore, mutex. Explain the purpose
of this semaphore. Specifically, if semaphore mutex were omitted,
give a timing sequence involving the several readers and/or writers
showing what might go wrong.
15.
Program read-write-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 need not be considered exactly the same as the critical
section for writers. Remove semaphore mutex and add additional
semaphores, as needed, so that some reader could work concurrently
with some writer (assuming the buffer contained some data but was
not full -- so both reading and writing made sense).
A Simple Pipeline Program
*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.
This problem is to be solved using three processes (P0, P1, P2) and two
one-integer buffers (B0 and B1) as follows:
1.
P0 is to generate the integers from 1 to 1000, and place them in B0
one at a time. After placing 1000 in the buffer, P0 places the
sentinel 0 in the buffer, and terminates.
2.
P1 is to read successive integers from B0. If a value is not
divisible by 2, the value is placed in B1. If the value is positive
and divisible by 2, it is ignored. If the value is 0, 0 is placed in
B1, and P1 terminates.
3.
P2 is to read successive integers from B1. If a value is not
divisble by 3, it is printed. If the value is positive and divisible
by 3, it is ignored. If the value is 0, P2 terminates.
The following diagram illustrates this processing:
pipeline processing
16.
Write a program to implement P0, P1, and P2 as separate processes
and B0 and B1 as separate pieces of shared memory -- each the size
of just one integer. Use semaphores to coordinate processing. Access
to B0 should be independent of access to B1; for example, P0 could
be writing into B0 while either P1 was writing into B1 or P2 was
reading.
------------------------------------------------------------------------
*Work to be turned in:*
* Commentary for steps 3-5, 6a, 8, 12, 14
* Programs for steps 6, 11, 15, and 16.
/Note:/ For steps 6, 11, 15 and 16, use the format for submitting
assignments
<http://www.cs.grinnell.edu/%7Ewalker/courses/213.fa04/assignments.shtml#format>
for each program.
------------------------------------------------------------------------
This document is available on the World Wide Web as
http://www.cs.grinnell.edu/~walker/courses/213.fa04/lab-semaphores.shtml
------------------------------------------------------------------------