Homework #2


Solve the following problems / questions from the class textbook (6th edition):

5.4, 5.5
6.3, 6.4, 6.9
in addition to the following problems:

The too-much-milk problem:
You and your roommate have an agreement that whenever one of you notices that there is no milk left, you'll go to the store to buy some. Unfortunately, there's only room for one container of milk in your refrigerator. If someone brings home a container of milk and there is already milk there, the new milk must be left on the counter to spoil.
Show how to avoid the milk spoiling situation using semaphores.

The readers and writers problem:
Consider a file of data, such as used by an airline reservation system which keeps track of the reserved seats on a flight or set of flights. There are travel agents all around the country who can access this file, so there are potential issues of contention. There is no problem if many different agents want to read the file at the same time, but only one agent at a time should be able to write to the file.
The rules we want to enforce are these:
   Any number of readers can simultaneously read the file;
   Only one writer at a time can write to the file;
   If a writer is writing, no reader can read;
   No process will starve - any process that wants to read or write will eventually be allowed to read or write.
Show how to implement readers / writers using semaphores.

Q: How much time did working on the problems take you (excluding typing the solutions)?


Note: All solutions should be: (a) presented in that order, (b) typed, and (c) the total length of the problem solutions that don't require coding should not exceed 2 pages, in which the font size is at least 11 pt. (You don't need to retype the problem descriptions if that constrains you.)