include "cs4410.php"; newheader('MP Synchronization FAQ'); ?>
Why am I getting an AttributeError for readlines while running q11.py and q12.py?
In the buildtree() function, the command sys.stdin.readlines(): expects data from the standard input. You need to arrange to run your program in such a way that the standard input supplies some meaningful input. If you run your program without any input specified, you'll likely receive the aforementioned AttributeError. One way you can direct meaningful values to your program's standard input is to run your program under the command prompt, and to supply the input by hand. This gets tiring very fast, so you might want to figure out how to redirect the contents of a file to a program's standard input. The file contents will appear to the program just as if you had typed them yourself. You can accomplish this by using the redirection operator "<" on the command line, like this: python q11.py <q11.input.txt. If you are running your program from within an IDE, you'll need to go into some setup options to make the same change (typically, the IDE will ask you how to run your program and let you specify input redirection.
Please do not hardcode the input file into your program. That creates fragile programs that depend on their inputs being placed in certain places in the filesystem. Programs should just read their input and print their output, so users can redirect that input and output as they wish.
More information on I/O in Python can be found here.
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.