• Coding
  • Semaphores & Monitors Exercises

On Thursday I have an Operating Systems exam. I've got a few exercises that keep driving me nuts whenever I look at the answers - they feel very confusing. I thought I'd share them with you while I study for the exam:

1. The Sleeping Barber Problem: I know the answer to this one
A barbershop consists of a waiting room with n chairs and a barber room with one 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.

2. The Cigaette-Smokers Problem: I have no idea how to solve this one, but I think monitors are involved
Consider a system with three smoker processes and one agent process. Each smoker continuously rolls a cigarette and then smokes it. But to roll and smoke a cigarette, the smoker process needs three ingredients: tobacco, paper and matches. One of the smoker processes has paper, another has tobacco, and the third has matches. The agent has an infinite supply of all three materials. The agent places two of the ingredients on the table. The smoker who has the remaining ingredient then makes and smokes a cigarette, signaling the agent on completion. The agent then puts out another two of the three ingredients and the cycle repeats. Write a program to synchronize the agent and the smokers.
-- I haven't given up on solving this one yet, I'll share the answer in case I reached anything
In the second exercise: does each of the 3 smokers have an infinite number of his distinct item?
CSGeek wroteIn the second exercise: does each of the 3 smokers have an infinite number of his distinct item?
Most likely yes. I copied the question as is. But since the question says "continuously rolls a cigarette", it means they also have infinite number of their distinct material.
Hey Kassem, I'm at work right now. Can it wait for tonight?

EDIT: I just read your exercise. I would suggest you start writing a main function. It will help you organize yourself and keep track of where you're headed.
Yes sure, it can wait. My exam is tomorrow morning.

I think I'm almost there, what's still missing is the main function - and some tweaks. I'll wait for your feedback tonight. Thank you :)
Hey Kassem, I'm also unable to help with the code right now but I noticed your second problem is an instance of the Producer-Consumer problem with multiple item types in the buffer.

Maybe that can help steer you in the right direction? It would mean your solution approach (monitor) is correct.
@saeidw: I thought about using monitors because this problem seemed to have some margin of similarity with the problem of Dining Philosophers, and monitors seem to do a great job at solving that problem.
Kassem, I just did a quick search for this problem and found it here. The solution is actually pretty simple if you go by the problem description on that page. Don't click that if you want to solve it yourself, there's some code up there.

Think of the table as the shared resource, and synchronize the three processes' access to it. The agent determines which process to signal based on what it places on the table. A smoker locks the table, makes his cigarette and smokes it, then unlocks the table.
This looks very simple and straight forward. It solves the problem nicely, but now I really do not know whether my answer is correct or not. I prefer using the answer you provided in case I got asked this question in the exam because I strongly doubt my professor would understand any of the stuff I wrote in my solution. He has definitely got the answer from the internet and will most likely grade us according to how similar our answer is going to be compared to what he found.

I'll wait for rahmu's feedback and then decide on which answer is best. Thanks for the help saeid, appreciated :)