You are on page 1of 3

Operating Systems

Assignment 3
Session: FALL 2014
Deadline Date: 5/JAN/2015 11:59 AM

Viva/Quiz will be conducted for evaluation


You are to submit typed explanation of all questions on Turnitin.
Explanation should include how you came with the solution and how
does it solve the problem. For problem 1 submission folder will be
created on UCP shares.
Write in your own words to answer the following, if you use material
from any book/website apart from your recommended book please
provide relevant references. All work will be assumed yours otherwise.
Justify your answers where needed. Explain briefly and concisely
1. We often build higher-level synchronization primitives on top of lower-level ones. For
example, we have seen how one can build mutexes using spin-locks during Lecture.
Another useful high-level primitive is the reader/writer lock. A reader/writer lock allows
multiple threads to acquire read access to shared data simultaneously, on the other hand, a
thread modifying the shared data can only proceed when no other thread is accessing the
data. A reader/writer lock can be implemented on top of mutexes and conditional
variables.
Recall that mutex and conditional variable export the following interfaces:
Mutex has type mutex_t and it implements lock(mutex_t *m) and unlock(mutex_t

*m).
Conditional variable has type condvar_t. The function cond_wait(condvar_t *cv,
mutex_t *m) releases the mutex pointed to by m and blocks the invoking thread until
cond_broadcast or cond_notify is called. The function cond_broadcast(condvar_t *cv)
wakes up all threads blocked in cond_wait. The function cond_notify(condvar_t *cv)
wakes up one of the threads blocked in cond_wait.

The reader/writer lock has type rwlock t and it needs to implement the following interfaces:
read lock(rwlock_t *rw) //allow any number of readers to proceed if no writer has grabbed
lock

Operating Systems
read unlock(rwlock_t *rw)
write lock(rwlock_t *rw) //allow a writer to proceed only if no reader nor writer has grabbed
lock
write unlock(rwlock_t *rw)
Write C code to implement the 4 function interfaces of reader/writer lock using the mutex and
condition variable. Hint: you might want to declare rwlock t as follows (or you are welcome
to use a different data structure for rwlock_t):
typedef struct rwlock {
int nreaders; //number of readers who have grabbed the lock, initialized to 0
int nwriters; //number of writers who have grabbed the lock, initialized to 0
mutex_t m;
condvar_t cv;
} rwlock t;

2. This problem demonstrates the use of semaphores to coordinate three types of processes.
Santa Claus sleeps in his shop at the North Pole and can only be wakened by either (1) all
nine reindeer being back from their vacation in the South Pacific, or (2) some of the elves
having difficulties making toys; to allow Santa to get some sleep, the elves can only wake
him when three of them have problems. When three elves are having their problems
solved, any other elves wishing to visit Santa must wait for those elves to return. If Santa
wakes up to find three elves waiting at his shops door, along with the last reindeer having
come back from the tropics, Santa has decided that the elves can wait until after
Christmas, because it is more important to get his sleigh ready. (It is assumed that the
reindeer do not want to leave the tropics, and therefore they stay there until the last
possible moment.) The last reindeer to arrive must get Santa while the others wait in a
warming hut before being harnessed to the sleigh. Solve this problem using semaphores.
3. The following problem was once used on an exam:
Jurassic Park consists of a dinosaur museum and a park for safari riding. There are m
passengers and n single-passenger cars. Passengers wander around the museum for a while,
then line up to take a ride in a safari car. When a car is available, it loads the one passenger it

Operating Systems
can hold and rides around the park for a random amount of time. If the n cars are all out
riding passengers around, then a passenger who wants to ride waits; if a car is ready to load
but there are no waiting passengers, then the car waits. Use semaphores to synchronize the m
passenger processes and the n car processes.

You might also like