Overview

In the last reading, we read about the need for synchronization and were introduced to one mechanism for providing it: semaphores. In this reading we’ll get a more in-depth look at how we may use semaphores in our concurrent programs. We’ll also look at another basic synchronization primitive, the barrier, that will be useful for your final project.

Required Reading

  1. Using Barrier Synchronization, up through and including the “Destroying a Synchronization Barrier” section.
  2. Sections 12.5.3 - 12.5.4 (pp. 1002-1008) from the course textbook.

Learning Objectives

BASIC Learning Objectives

Each student will be responsible for learning and demonstrating proficiency in the following objectives PRIOR to the class meeting. The reading quiz will test these objectives.

  1. Write C code that initializes and destroys a pthread barrier for a given number of threads.
  2. Explain what is returned by a call to the pthread_barrier_wait function.
  3. Compare the use cases of binary semaphores and counting semaphores.
  4. Describe both the producer-consumer problem and the readers-writers problem, giving real world examples of both problems.
  5. Compare and contrast the first and second readers-writers problems.
  6. Explain what is meant by starvation in the context of the synchronization.

ADVANCED Learning Objectives

The following objectives should be mastered by each student DURING and FOLLOWING the class session through active work and practice.

  1. Write, read, and trace C programs that use pthread barriers for synchronization.
  2. Identify scenarios where a binary semaphore or a counting semaphore may be used to avoid synchronization errors.
  3. Write, read, and trace C programs that use semaphores for synchronization.
  4. Given a C program that uses semaphores, determine whether starvation is possible.

Pre-class Exercises

These exercises are geared towards mastering the BASIC learning objectives listed above. You are expected to submit them before class and it is highly recommended that you complete them before attempting the reading quiz.

  1. Complete the following C code to initialize the barrier such that it has the default barrier attributes and would require five threads to reach that barrier before any of them could pass.

    pthread_barrier_t the_more_the_barrier;
    
    // Write a line of code to initialize the barrier
  2. How does a binary semaphore differ from a counting semaphore?

  3. Part A: Give an example of a problem that could be modeled as a producer-consumer problem, beyond those examples described in the book.

    Part B: Repeat Part A, except for the readers-writers problem.