Summary: This laboratory exercise provides practice with shared memory and process synchronization in Linux.
Review sample programs 7-10 in An Introduction to Concurrency in Unix-based [GNU] C Through Annotated Examples, as discussed in class.
Review the corresponding Linux-based programs (read-write-1.c through read-write-4.c) distributed in class.
Copy ~walker/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.
As we did in class, remove the sleep statement from the child process, rerun read-write-1.c, and explain the output produced.
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.
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.
Initialize the shared memory location in main memory to -1 before the fork operation. (Why must this be done before the fork?)
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.
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.
Copy programs ~walker/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.
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.
Copy ~walker/c/concurrency/read-write-3.c, to your account, compile it with gcc and run it.
Review read-write-3.c to be sure you understand how it works.
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.
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.
Copy ~walker/c/concurrency/read-write-4.c, to your account, compile it with gcc and run it.
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.
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).
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:
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.
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.
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:
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:
This document is available on the World Wide Web as
http://www.cs.grinnell.edu/~walker/courses/213.fa04/lab-semaphores.shtml
|
created October 7, 2000 last revised October 5, 2004 |
|
| For more information, please contact Henry M. Walker at (walker@cs.grinnell.edu) |