Lab 6: Applying Synchronization Primitives
Ice Cream Parlor was a lecture example created by Jerry Cain.
Before the end of lab, be sure to fill out the lab checkoff sheet here!
Before starting, get the starter code by cloning the lab6
folder:
$ git clone /usr/class/cs110/repos/lab6/shared lab6
$ cd lab6
$ make
Problem 1: Ice Cream Parlor
In this lab problem, we will implement a simulation of customers ordering ice cream in an ice cream parlor. The example is a bit contrived, but it allows us to work through a complex synchronization setup without actually having a very complicated program. You’ll see some applications of mutexes, semaphores, and condition variables that will be extremely helpful in Assignment 5.
The goal of this lab is to help you practice:
- When should you use each synchronization primitive? You’ll need to use mutexes, semaphores, and condition variables in different situations for this simulation.
- When you’re working on a big, complicated problem, how do you break it down into small pieces that you can implement and test incrementally? It’s crucial to avoid writing large amounts of code without testing, because if you have a bug, it will be like trying to find a needle in a haystack.
Simulation overview
This ice cream parlor comes from a somewhat odd universe:
- There are
kNumCustomers
customers. Each decide to order 1-4 ice cream cones. However, instead of ordering them at a counter like they normally would, they hire clerks on demand to make their cones. (Imagine using an app like Fiverr or Instacart where you could hire someone on the spot.) If a customer comes in wanting 3 ice cream cones, he hires 3 clerks on the spot to make each cone for him. There is no fixed number of clerks; a customer can summon a clerk out of thin air as necessary. - A single manager sits in an office. When clerks make the ice cream cones, she inspects them one-by-one and decides whether to approve or reject them.
- Each clerk that is hired rushes to the parlor and makes a cone, before presenting it to a manager for approval. If the manager approves the cone, the clerk hands it to the customer and leaves; otherwise, the clerk re-makes the cone.
- A single cashier rings up each customer’s order.
- Some number of electricians arrive at the ice cream parlor needing to make repairs. They wait until all customers leave the shop, then perform the repairs.
Here are slides that walk through the various parts of the simulation: [keynote], [pdf]
Implementing the simulation
You can find the ice-cream-parlor.cc
starter code in the lab6
directory
(instructions for cloning are above). Alternatively, you can work through this
cplayground.
Customer/Clerk interactions
- How can a customer summon clerks on demand, out of thin air?
- How should a customer wait until his clerks have finished making his cones?
Clerk/Manager interactions
- How can we ensure that only one clerk is in the manager’s office at a time?
- How can we make the manager sleep until a clerk needs an inspection?
- How can we make the clerk sleep until the inspection is complete?
- How can we communicate the results of the inspection (passed/failed) from the manager to the clerk?
Customer/Cashier interactions
- How should the customers order themselves in line? (How does the cashier know who to help first, second, third? If two threads arrive at the same time, how do we make sure one thread goes first, followed by the next thread?)
- How should the cashier wait until a customer has joined the line?
- How should the cashier notify a specific customer that they are finished?
Electrician/Customer interactions
- How should the electricians wait until all customers have left the store?