Ejemplo n.º 1
0
    def __init__(self):
        """ Initializes an instance of OreoFactory. Shared resources and locks
            get created in here.

        """
        MP.__init__(self)
        # TODO implement me
        self.lock = self.Lock('lock')
        self.num_cookies = self.Shared('number of cookies', 0)
        self.num_icing = self.Shared('number of layers of icing', 0)
        self.cookies_ready = self.lock.Condition('cookies are ready')
        self.icing_ready = self.lock.Condition('icing is ready')
Ejemplo n.º 2
0
    def __init__(self):
        """ Initializes an instance of BusStoplight. Shared resources and locks
            get created in here.

        """
        MP.__init__(self)
        # TODO implement me
        self.lock = self.Lock('lock')
        self.boardingStudents = self.Shared('number of boarding students', 0)
        self.departingStudents = self.Shared('number of departing students', 0)
        self.canBoard = self.lock.Condition('can board')
        self.canDepart = self.lock.Condition('can depart')
Ejemplo n.º 3
0
    def __init__(self):
        """ Initializes an instance of BusStoplight. Shared resources and locks
            get created in here.

        """
        MP.__init__(self)
        # TODO implement me
        self.boarding = self.Semaphore('boarding', 1)
        self.departing = self.Semaphore('departing', 1)
        self.multipleStudents = self.Semaphore('multiple students crossing', 1)
        self.boardingStudents = self.Shared('number of boarding students', 0)
        self.departingStudents = self.Shared('number of departing students', 0)
Ejemplo n.º 4
0
	def __init__(self, max_coffee_bean_units):
		""" Initializes an instance of EspressoCzar. Shared resources and locks
			get created in here.

		:arg max_coffee_bean_units: is an int, expects value FILL_CAPACITY
		"""
		MP.__init__(self)
		# TODO implement me
		# self.debug = True
		self.deque = deque()
		self.lock = self.Lock('monitor lock')
		self.empty = self.lock.Condition('machine is empty')
		self.full = self.lock.Condition('machine is full')
Ejemplo n.º 5
0
    def __init__(self, max_coffee_bean_units):
        """ Initializes an instance of EspressoCzar. Shared resources and locks
        get created in here.

        :arg max_coffee_bean_units: is an int, expects value FILL_CAPACITY
        """
        MP.__init__(self)
        # TODO implement me
        self.sema_empty = self.Semaphore("machine empty",
                                         max_coffee_bean_units)
        self.sema_full = self.Semaphore("machine full", 0)
        self.queue = list()
        self.lock = self.Lock("queue lock")
Ejemplo n.º 6
0
    def __init__(self):
        """ Initializes an instance of ReaderWriterManager. Shared resources and
            locks get created in here.

        """
        MP.__init__(self)
        self.monitor_lock = self.Lock('monitor lock')

        self.num_readers = self.Shared('readers currently reading', 0)
        self.num_writers = self.Shared('writers currently writing', 0)
        self.waiting_readers = self.Shared('number of waiting readers', 0)
        self.waiting_writers = self.Shared('number of waiting writers', 0)
        self.can_read = self.monitor_lock.Condition('can read')
        self.can_write = self.monitor_lock.Condition('can write')
Ejemplo n.º 7
0
    def __init__(self, max_coffee_bean_units):
        """ Initializes an instance of EspressoCzar. Shared resources and locks
			get created in here.

		:arg max_coffee_bean_units: is an int, expects value FILL_CAPACITY
		"""
        MP.__init__(self)
        # TODO implement me
        # self.debug = True
        self.deque = deque()
        self.mutex = self.Semaphore('mutex lock', 1)
        self.machineFull = self.Semaphore('machine is full', 0)
        self.machineEmpty = self.Semaphore('machine is empty',
                                           max_coffee_bean_units)
Ejemplo n.º 8
0
    def __init__(self, max_coffee_bean_units):
        """ Initializes an instance of EspressoCzar. Shared resources and locks
            get created in here.

        :arg max_coffee_bean_units: is an int, expects value FILL_CAPACITY
        """
        MP.__init__(self)
        # TODO implement me
        self.capacity = self.Shared("capacity", max_coffee_bean_units)
        self.queue = list()
        self.size = self.Shared("shared", 0)
        self.lock = self.Lock("queue lock")
        self.full_cv = self.lock.Condition(
            "there is bean in the coffee machine")
        self.empty_cv = self.lock.Condition(
            "the bean in the coffee machine is not full")
Ejemplo n.º 9
0
    def __init__(self, num_workers):
        """ Initializes an instance of GraphicsEngine. Shared resources and locks
            get created in here.

        """
        MP.__init__(self)
        # TODO implement me
        self.lock = self.Lock('lock')
        self.total_threads = self.Shared('total number of threads',
                                         num_workers)
        self.threads_done_with_last_frame = self.Shared(
            'number of threads that have done with last frame', 0)
        self.threads_ready_for_next_frame = self.Shared(
            'number of threads that are ready for next frame', 0)
        self.all_threads_done_with_last_frame = self.lock.Condition(
            'all threads have done with last frame')
        self.all_threads_ready_for_next_frame = self.lock.Condition(
            'all threads are ready for next frame')
Ejemplo n.º 10
0
    def __init__(self):
        """ Initializes an instance of BrokerageManager. Shared resources and
            locks get created in here.

        """
        MP.__init__(self)
        # TODO implement me
        #self.debug = True
        self.lock = self.Lock('lock')
        self.AWSLambdaTask = self.Shared('number of AWSLambdaTask', 0)
        self.CronTask = self.Shared('number of CronTask', 0)
        self.IndexingTask = self.Shared('number of IndexingTask', 0)
        self.AWSLambdaTask_available = self.lock.Condition(
            'AWSLambdaTask available')
        self.CronTask_available = self.lock.Condition('CronTask available')
        self.IndexingTask_available = self.lock.Condition(
            'IndexingTask available')
        self.can_request = self.lock.Condition('can make a request')
Ejemplo n.º 11
0
    def __init__(self, n):
        MP.__init__(self)
        self.capacity = self.Shared("capacity of career fair", n)
        self.total = self.Shared("total number of people in career fair", 0)
        self.cs = self.Shared("number of cs students in career fair", 0)
        self.econ = self.Shared("number of econ students in career fair", 0)
        self.aem = self.Shared("number of aem students in career fair", 0)
        self.waiting_recruiter = self.Shared("number of waiting recruiters", 0)

        self.lock = self.Lock("monitor lock")
        self.career_fair_not_full = self.lock.Condition(
            "career fair can accept more people")
        self.cs_le_60 = self.lock.Condition("number of cs students is <= 60%")
        self.no_econ = self.lock.Condition(
            "there is no econ students in career fair")
        self.no_aem = self.lock.Condition(
            "there is no aem students in career fair")
        self.no_waiting_recruiter = self.lock.Condition(
            "there is no waiting recruiters")
Ejemplo n.º 12
0
    def __init__(self, n):
        MP.__init__(self)
        # TODO implement me
        # counter
        self.capacity = self.Shared("Capacity", n)
        self.total = self.Shared("total", 0)
        self.cs_count = self.Shared("cs student", 0)
        self.econ_count = self.Shared("econ student", 0)
        self.aem_count = self.Shared("aem student", 0)
        self.wait_hr_count = self.Shared("waiting recruiter", 0)

        # lock
        self.lock = self.Lock("career fair")
        self.wait_hr_lock = self.Lock("waiting recruiter counter lock")

        # condition variable
        self.empty_cv = self.lock.Condition("career fair not empty")
        self.cs_cv = self.lock.Condition("cs student can enter career fair")
        self.econ_cv = self.lock.Condition("econ student can enter career fair")
        self.aem_cv = self.lock.Condition("aem student can enter career fair")
        self.hr_cv = self.lock.Condition("recruiter waits outside career fair")
Ejemplo n.º 13
0
#       a) Running the code repeatedly is a good way to test for correctness.
#       b) To make sure your code is correct, you must reason about the code
#           instead of rely on observed outputs.


class Worker1(MPthread):
    def __init__(self, mp):
        MPthread.__init__(self, mp, 'Worker 1')

    def run(self):
        while True:
            print('Hello from Worker 1')


class Worker2(MPthread):
    def __init__(self, mp):
        MPthread.__init__(self, mp, 'Worker 2')

    def run(self):
        while True:
            print('Hello from Worker 2')


if __name__ == '__main__':
    mp = MP()
    w1 = Worker1(mp)
    w2 = Worker2(mp)

    w1.start()
    w2.start()
Ejemplo n.º 14
0
 def __init__(self):
     MP.__init__(self)
     self.counter = self.Shared('counter', 0)
     self.lock = self.Lock("counter lock")
Ejemplo n.º 15
0
 def __init__(self):
     MP.__init__(self)
     self.counter = self.Shared('counter', 0)
     self.counterLock = self.Semaphore('counter lock', 1)
Ejemplo n.º 16
0
                print('coordinator {} reserved cars {}, {}'.format(
                    self.id, first_rental, extra_rental))
            else:
                print('coordinator {} reserved car {}'.format(
                    self.id, first_rental))

            self.delay(0.1)

            if needs_extra:
                if extra_rental < first_rental:  # ordering
                    rental_cars[extra_rental].release()
                    rental_cars[first_rental].release()
                else:
                    rental_cars[first_rental].release()
                    rental_cars[extra_rental].release()
            else:
                rental_cars[first_rental].release()


################################################################################
## DO NOT WRITE OR MODIFY ANY CODE BELOW THIS LINE #############################
################################################################################

if __name__ == '__main__':

    CARS_IN_STOCK = 5
    manager = MP()
    rental_cars = [manager.Lock('m' + str(i)) for i in range(CARS_IN_STOCK)]
    for i in range(3):
        Coordinator(i, manager).start()