Operating
Systems (CS604)
Assignment
# 3
Fall 2011
IDEA SOLUTION:
# These three
are mutexes (only 0 or 1 possible)
Semaphore
contractorReady = 0 # if 1, at
least one contractor is ready
Semaphore
ministerReady = 0
Semaphore
accessWRSeats = 1 # if 1, the # of
seats in the waiting room can be incremented or decremented
int
numberOfFreeWRSeats = N # total
number of seats in the waiting room
def
minister():
while true: # Run in an infinite loop.
wait(contractorReady) # Try to acquire a contractor - if
none is available, go to sleep.
wait(accessWRSeats) # Awake - try to get access to modify
# of available seats, otherwise sleep.
numberOfFreeWRSeats++ #
One waiting room chair becomes afree.
signal(ministerReady) # I am ready.
signal(accessWRSeats) # Don't need the lock on the chairs
anymore.
# (Enter Door 1.)
def
Contractor():
while true: # Run in an infinite loop.
wait(accessWRSeats) # Try to get access to the waiting
room chairs.if numberOfFreeWRSeats > 0: # If there are any free seats:
numberOfFreeWRSeats-- #
sit down in a chair
signal(contractorReady) #
notify the minister, who's waiting until there is a contractor
signal(accessWRSeats) #
don't need to lock the chairs anymore
wait(ministerReady) #
wait until the minister is ready
# (Finish the meeting here.)
else:
# otherwise, there are
no free seats;
signal(accessWRSeats) #
but don't forget to release the lock on the seats!
# (Leave without a meeting with
minister.)
if minister=busy
close D1;
No comments:
Post a Comment
“You can't change the past, but you can ruin the present by worrying about the future”