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:

 

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) {

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

}