OS/161 Paint Shop Synchronization Problem
Nov 13, 2015
The assignment was assigned about a month ago. But we are students. We don’t do these until the deadline is knocking at the door. So I started looking into the assignment yesterday. Solving the problem took a few hours in total which I have to say was a lot faster than I expected.
Problem
The problem is something like this. There’s a paint shop, a few staffs working there and some customers. The customers bring in paint cans which are to be filled with different tints of colors according to their need. The staff who is free at that moment takes an order, fills the can and serves it back to the customer. Pretty much that’s it.
The issue here is that, if the tasks are not synchronized, it can become a total disaster. One customer might order one can, but receive another one ordered by someone else. It may also be possible that the staffs think that there are no more customers remaining and so they go home while there are actually customers still waiting to submit their orders.
You can find the problem in greater details here.
Solution
The way I solved it was very simple. The customers push their orders in a queue. Access to the queue is protected by a semaphore. As the order has been submitted, the customer waits using another semaphore. The staffs receive the orders from the queue. They fill the cans and put them in the array containing all the ready orders. The customers are notified by the semaphore and then they check the array to see if their can is ready. If there are no orders and all the customers have gone home, the staffs close the paint shop.