| CS 451 | Willamette University | Spring, 2019 |
|
Topics in Computer Science:
Operating Systems and Concurrency |
||
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).
Copy 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 spins as long as shared memory contains -1 (i.e., the parent has not yet placed a value into the memory.) Once a nonnegative value appears in shared memory, the child can read that value and then reset the shared memory 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 spins as long as shared memory contains a nonnegative number (i.e., the child has not yet retrieved the previous value from memory). Once the memory contains -1, the parent may write the next nonnegative number to shared memory.
Copy program 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 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 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.
|
created October 7, 2000 revised October 5, 2004 updated 10 March 2019 |
|
| For more information, please contact Henry M. Walker at (walker@cs.grinnell.edu) |