Laboratory Exercises For Computer Science 295

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:

  1. Review sections 4.1-4.2 in Hartley's text concerning the definition of semaphores and their use in SR.
    1. 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.

    2. 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?

    3. 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.

    4. 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?

  2. 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.

  3. 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.

  4. Program 4.2 contains three semaphores, elements, spaces, and mutex. Explain why each of these semaphores are needed.

  5. 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.

  1. 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 .

  2. 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)

Additional assignment (to be done individually): (Due Thursday, April 10)

Do TWO (2) of the following problems:

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