include "cs4410.php"; newheader('MP Synchronization FAQ'); ?>
Q: Are we allowed to take advantage of the optional argument to the acquire method that causes the semaphore to be non-blocking and return true or false if the acquire does not succeed?
A: No, for MP1, please stick with the semaphore interface we discussed in class (init, P, and V). It's clear and simple to reason about.
How can I use monitors in Python? There is no object called monitor; only Locks, Semaphores and Condition variables. Should I create a Monitor class?
In Python the behaviors of Monitors are realized by Condition() objects that carry a Lock() inside them. By acquiring and releasing this Lock you enter the monitor and then you can wait, notify and notifyAll on the Condition object(s).
Here is a pseudo example:
class PrinterRoom(): def __init__(self):self.printerBusy = False self.printerLock = Lock() self.printerAvailable = Condition(self.printerLock) def waitforPrinter(self): with printerLock: #acquire the monitor lock while printerBusy: #check if you need to wait printerAvailable.wait() #wait until somebody notifies you printerBusy = True #set printerBusy to True, to make others wait def donewithPrinter(self): with printerLock: #acquire the monitor lock printerBusy = False #set printerBusy to False, when you are done printerAvailable.notify() #notify one person waiting for the printer to be available class Student(): def printdocument(self):pr = PrinterRoom() while True: pr.waitforPrinter() use printer now pr.donewithPrinter() chat with friends and hang out For more information on synchronization objects in Python, you can read the Python threading module documentation.