Lab 7: Semaphores in SR
Lab 7: Semaphores in SR
Goals: This lab provides experience with some simple applications of
semaphores. (Within this lab, issues of starvation and deadlock are
ignored.)
Steps for this Lab:
-
Review sections 4.1-4.2 in Hartley's text concerning the definition of
semaphores and their use in SR.
-
While the traditional semaphore operations are labeled P and
V for historical reasons, some modern textbooks use the names
signal and wait. Do you think wait refers to
P or V (signal refers to the other operation)?
Explain your conclusion.
-
In SR, semaphores can be declared and initialized with the statement
sem mutex := 1
Some computer scientists consider the assignment statement to be somewhat
misleading in this context. Why do you think these people have this
viewpoint?
-
Copy Program 4.1 of Hartley's text to your account. (This program is
available as ~walker/parallel/sr/book/simple-semaphore.sr .)
Compile and run the program, with the options described in Section 4.1.
Explain how the two versions of output are produced.
-
Within the body of Program 4.1, add the assignment statement
mutex := 0
Now compile the program and explain what happens.
Why do you think this assignment statement viewed differently by the
compiler than the``assignment'' in the original declaration statement?
-
Section 4.3.1 outlines the Producers/Consumers Problem and shows one
solution written in SR. Write a 4-sentence abstract which describes this
problem completely, yet concisely.
-
Program 4.2 is available as ~walker/parallel/sr/book/prod-cons.sr .
While this program presents code that solves the Producers/Consumers
Problem, the program is incomplete for it does not contain any comments.
Complete the program by providing appropriate comments, including notes
about relevant variables, procedures, and blocks of code.
-
Program 4.2 contains three semaphores, elements, spaces, and
mutex. Explain why each of these semaphores are needed.
-
Compile and run Program 4.2 for 2 producers and 3 consumers, recording the
output (with submit) to obtain at least 30 lines of output.
Explain the sequence of code execution that produced this output.
Two Classical Problems
The Sleeping-Barber Problem: A barbershop consists of a waiting room
with n chairs, and the barber room containing the barber chair. If there
are no customers to be served, the barber goes to sleep. If a customer
enters the barbershop and all chairs are occupied, then the customer leaves
the shop. If the barber is busy, but chairs are available, then the
customer sits in one of the free chairs. If the barber is asleep, the
customer wakes up the barber. Write a program to coordinate the barber and
the customers. [From Silberschatz and Galvin, Operating
Systems Concepts Fourth Edition, p. 212]
The Readers and Writers Problem: Imagine a big data base, such as an
airline reservation system, with many competing processing wishing to read
and write it. It is acceptable to have multiple processes reading the data
base at the same time, but if one process is writing (i.e., changing) the
data base, no other processes may have access to the data base, not even
readers. The question is how do you program the readers and the writers?
[From Tanenbaum, Operating Systems: Design and
Implementation pp. 77-79.]
Sections 4.3.2 and 4.3.4 of Hartley's text give solutions to these
problems using programs 4.4 and 4.8, respectively.
-
Choose one of these two problems and corresponding sections of Hartley's
text.
[Students who took CS 207 last semester must chose Section
4.3.4.]
Answer the equivalent of Steps 3, 4, and 5 above for the problem you have
chosen. [The above problem statements provide an abstract of the
problem under consideration, completing Step 2 for your chosen problem.]
Note: Programs 4.4 and 4.8 are available in directory
~walker/parallel/sr/book . When you copy them to your account,
change the name to end in .sr .
-
Do Hartley's Section 4.5.2, the assignment on the Multiple
Producers/Consumers Problem.
Lab work to be turned in: (Due Tuesday, April 8)
-
Commentary from Step 1 above.
-
Programs and test runs from Steps 6 and 7 above.
Additional assignment (to be done individually): (Due Thursday, April
10) Do TWO (2) of the following problems:
-
Hartley's assignment 4.5.7
-
Hartley's assignment 4.5.8
-
Hartley's assignment 4.5.9
-
Write a version of the fractals computation program, which produces similar
output to the ones done previously in this course. This version of the
program should have the following elements:
-
The computation should be done by separate threads within the same
process, so a shared-memory model with semaphores applies.
-
Points of the Julia Set should be computed column-by-column, and each
thread should work on a contiguous block of columns.
-
A thread should compute and print the results for one column before going
on to its next column.
-
To avoid any possible printing contention, threads should use a common
semaphore to control access to the output. That is, a thread should not
attempt output until it can be sure no other thread will be printing, and a
semaphore should be used to enforce this policy.
-
Each thread should utilize local memory as much as possible (although
certainly some overall parameters and any semaphores will need to be global).
For each assignment, turn in your code, test runs, and an argument of
correctness, (following the general lab instructions from Lab 1).
This document is available on the World Wide Web as
http://www.math.grin.edu/~walker/courses/295/lab7.html
created March 31, 1997
last revised March 31, 1997