def __init__(self, list_size): self.list_size = list_size # number of items in the singly linked list self.append_mutex = Mutex() self.no_search = Semaphore(1) self.no_append = Semaphore(1) self.search_LS = Lightswitch() self.append_LS = Lightswitch()
class SimpleBarrier: #### sl 78 def __init__(self, N): self.N = N self.count = 0 self.m = Mutex() #self.m = Semaphore(1) # mutex self.b = Event() # barier def barrier(self): self.m.lock() #self.m.wait() self.count += 1 self.m.unlock() #self.m.signal() if self.count == self.N: self.b.signal() self.b.wait()
class SimpleBarrier: """ ADT, use only barrier() """ def __init__(self, n): self.n = n self.count = 0 self.mutex = Mutex() self.turnstile = Semaphore(0) def barrier(self): self.mutex.lock() self.count += 1 if self.count == self.n: self.count = 0 self.turnstile.signal(self.n) self.mutex.unlock() self.turnstile.wait()
class EventBarrierOptimized: """ ADT, use only barrier() """ def __init__(self, n): self.n = n self.count = 0 self.mutex = Mutex() self.event = Event() def barrier(self): self.mutex.lock() if self.count == 0: self.event.clear() self.count += 1 if self.count == self.n: self.count = 0 self.event.signal() self.mutex.unlock() self.event.wait()
def __init__(self, n): self.n = n # number of chairs in the waiting room self.customers_count = 0 # number of customers in the waiting room self.customers_cut = 0 # number of customers that received their haircut self.mutex = Mutex( ) # protects access to the customer_count shared variable self.customer = Semaphore( 0) # customer signals that he is ready to get his hair cut self.barber_done = Semaphore( 0) # barber signals when the haircut is done self.customer_done = Semaphore( 0) # customer signals when he is satisfied with the haircut self.queue = Queue( ) # for queueing customers; each customer puts his semaphore into this FIFO queue
def __init__(self, n): self.n = n # number of chairs in the waiting room self.customers_count = 0 # number of customers in the waiting room self.customers_cut = 0 # number of customers that received their haircut self.mutex = Mutex( ) # protects access to the customer_count shared variable self.barber = Semaphore( 0 ) # barbers signals that a customer can go sit in the barber chair self.customer = Semaphore( 0) # customer signals that he is ready to get his hair cut self.barber_done = Semaphore( 0) # barber signals when the haircut is done self.customer_done = Semaphore( 0) # customer signals when he is satisfied with the haircut
def __init__(self): self.mutex = Mutex() # signal smoker that he can smoke self.smoker_tobacco = Semaphore(0) self.smoker_paper = Semaphore(0) self.smoker_match = Semaphore(0) # signal dealer that he can deal self.dealer_tobacco = Semaphore(0) self.dealer_paper = Semaphore(0) self.dealer_match = Semaphore(0) self.is_tobacco = 0 self.is_paper = 0 self.is_match = 0
class Lightswitch: def __init__(self): self.count = 0 self.mutex = Mutex() def lock(self, semaphore): self.mutex.lock() self.count += 1 if self.count == 1: semaphore.wait() self.mutex.unlock() def unlock(self, semaphore): self.mutex.lock() self.count -= 1 if self.count == 0: semaphore.signal() self.mutex.unlock()
# ppds-labs: cigarette_smokers_problem_with_dealer_agent_not_waiting # Copyright (c) 2019, Milten Plescott. All rights reserved. # SPDX-License-Identifier: MIT from ppds import Mutex, Semaphore, Thread from random import randint from time import sleep _print = print print_mutex = Mutex() green_style = "\033[32m" end_style = "\033[0m" def print(*args, **kwargs): print_mutex.lock() _print(*args, **kwargs) print_mutex.unlock() class Shared: def __init__(self): self.mutex = Mutex() # signal smoker that he can smoke self.smoker_tobacco = Semaphore(0) self.smoker_paper = Semaphore(0) self.smoker_match = Semaphore(0) # signal dealer that he can deal
def __init__(self): self.count = 0 self.mutex = Mutex()
class Lightswitch: def __init__(self): self.count = 0 self.mutex = Mutex() def lock(self, semaphore): self.mutex.lock() self.count += 1 if self.count == 1: semaphore.wait() self.mutex.unlock() def unlock(self, semaphore): self.mutex.lock() self.count -= 1 if self.count == 0: semaphore.signal() self.mutex.unlock() def lock_test(self, semaphore, t_id): self.mutex.lock() self.count += 1 if self.count == 1: print("First thread walked in and turned on the light!") print("thread_id = {:2d}, number of threads inside = {:2d}\n".format(t_id, self.count)) semaphore.wait() else: print("A thread walked in, but there already were some threads inside.") print("thread_id = {:2d}, number of threads inside = {:2d}\n".format(t_id, self.count)) self.mutex.unlock() def unlock_test(self, semaphore, t_id): self.mutex.lock() self.count -= 1 if self.count == 0: print("Last thread walked out and turned off the light!") print("thread_id = {:2d}, number of threads inside = {:2d}\n".format(t_id, self.count)) semaphore.signal() else: print("A thread walked out, but there are still some threads inside.") print("thread_id = {:2d}, number of threads inside = {:2d}\n".format(t_id, self.count)) self.mutex.unlock()
def __init__(self, N): self.N = N self.count = 0 self.m = Mutex() #self.m = Semaphore(1) # mutex self.b = Semaphore(0) # barier
def __init__(self, n): self.n = n self.count = 0 self.mutex = Mutex() self.event = Event()
def __init__(self, n): self.n = n self.count = 0 self.mutex = Mutex() self.turnstile = Semaphore(0)