Second Exam
CS354 Spring 2009
March 25, 2009
Name: ______________________________________________________________________
Question | Maximum | Current |
1. True/False | 11 pts |
|
2. Short questions | 4 |
|
3. | 4 |
|
4. | 4 |
|
5. | 4 |
|
6. | 4 |
|
7. | 4 |
|
8. Programming question | 15 |
|
Total | 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
1. True/False Questions (write F or T) (11 points, 1 point each)
__ Semaphores are only initialized with a count >= 0
__ “cond_wait” needs to be called inside a testing loop
__ Heap-allocated memory can be shared between threads
__ The calls sema_wait and sema_post are different in that sema_post must be called inside a critical section.
__ Mutexes must be unlocked always in the same order to avoid a deadlock
__ The read/write lock implementation discussed in class allows simultaneous lock acquisition by different writers.
__ An iterative server passes slave sockets in round-robin fashion to a pool of threads.
__ Thread-safe code is always re-entrant.
__ The principle of least common mechanism warns against sharing resources between services of different importance.
__ Different threads in the same process can run different algorithms.
__ Monitors are a fair synchronization mechanism.
Short Questions
2. Describe a situation in which you would use spinlocks.
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
3. Which information does the OS need to keep track of when a process opens files, and where is it stored?
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
4. What is the difference between dup() and dup2() ? For each of them, give an example of where they are used in the shell project.
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
5. Comment on the idea of using a global variable to indicate error values in a multi-threaded environment. Is it good or bad and why?
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
6. If "count++" is only one C statement, why does it need to be protected by a mutex to be thread-safe ?
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
7. In a system with N numbered mutexes, threads need to get two consecutive mutex locks (e.g., m[i] and m[i+1]) before doing some operations. Describe in pseudo-code how the locking should be done to prevent deadlocks, including for corner cases.
__________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
8. You are hired to make a simulation of a farm. The simulation needs to keep track of eggs, hens, workers and a supply of egg boxes recycled by customers. In the space below, write a function “start_simulation” that is passed the number of hens, boxes, customers and workers as arguments, initializes any needed synchronization mechanisms, and starts the appropriate threads. Also write the functions that will run each thread. Your answers will be graded based on whether your implementation enforces the following constraints:
●.The hens lay an egg every day. (1 point)
●.A customer consumes 2 eggs every day out of a box, if they managed to get one. A box contains 12 eggs. (1 point)
●.Consumers must queue and so are sold boxes on a first-come first-served basis. They can only have one box at a time and come back once they have consumed the eggs in that box (3 points)
●.Workers fill the boxes one at a time. That is, there is a single filling station that only one worker may use at a time to fill a single box. Workers queue to use the filling station. This avoids having several partially filled boxes. (3 points)
●.Obviously, a worker needs to wait inside the filling station until 12 hens have laid an egg, as the boxes must contain 12 eggs. (3 points)
●.There is a fixed, limited number of boxes in the entire simulation, and they are recycled. There must not be a worker with a box in hand waiting for the filling station while a worker without a box is inside the filling station. Consumers must return their empty box before waiting in line for a full one. (3 points)
●.Workers take 1 minute to make a filled box available to a consumer (e.g., in a refrigerator). (1 point)
Total 15 points. Tip: use a “day_sleep(int x)” function, which you don't have to define, to simulate threads waiting for x days, and sleep(60) to wait a minute.
// define any globals here
void hen(){
}
void customer() {
}
void worker() {
}
void start_simulation (int n_hens, int n_boxes, int n_customers, int n_workers) {
}