Operating System Assignment 3
Operating System Assignment 3
Assignment 3
Session: FALL 2014
Deadline Date: 5/JAN/2015 11:59 AM
*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.