def customer(shared, cid): barber = Semaphore(0) while True: sleep(randint(9, 10) / 100) shared.mutex.lock() if shared.customers == shared.N: balk(cid) shared.mutex.unlock() continue print(f"customer {cid} wait for...") shared.queue.append(barber) shared.customers += 1 shared.mutex.unlock() shared.customer.signal() barber.wait() getHairCut(cid) shared.customerDone.signal() shared.barberDone.wait() shared.mutex.lock() shared.customers -= 1 shared.mutex.unlock()
def __init__(self, n, n_savages): self.servings = n self.mutex = Mutex() self.empty_pot = Semaphore(0) self.full_pot = Semaphore(0) self.barrier1 = SimpleBarrier(n_savages) self.barrier2 = SimpleBarrier(n_savages)
def __init__(self, n): self.mutex = Mutex() self.servings = 0 self.full_pot = Semaphore(0) self.empty_pot = Semaphore(0) self.barrier1 = SimpleBarrier(n) self.barrier2 = SimpleBarrier(n)
def __init__(self, capacity): self.free_space = Semaphore(capacity) self.items = Semaphore(0) self.mutex = Mutex() self.produced = 0 self.consumed = 0 self.closed = False
class WareHouse: def __init__(self, capacity): self.free_space = Semaphore(capacity) self.items = Semaphore(0) self.mutex = Mutex() self.produced = 0 self.consumed = 0 self.closed = False def produce(self, time_to_produce): sleep(time_to_produce) self.mutex.lock() self.produced += 1 self.mutex.unlock() def consume(self, time_to_consume): sleep(time_to_consume) self.mutex.lock() self.consumed += 1 self.mutex.unlock() def stop_production(self, time_period): sleep(time_period) self.closed = True self.items.signal(100) self.free_space.signal(100)
def __init__(self): self.oxygen = 0 self.hydrogen = 0 self.mutex = Mutex() self.oxyQueue = Semaphore(0) self.hydroQueue = Semaphore(0) self.barrier = Barrier(3)
def __init__(self): self.mutex = Mutex() self.dealersQ = Semaphore() self.officersQ = Semaphore() self.officerCounter = 0 self.dealerCounter = 0 self.barrier = SimpleBarrier(4)
def __init__(self): self.hackers = 0 self.serfs = 0 self.mutex = Mutex() self.hackers_queue = Semaphore(0) self.serfs_queue = Semaphore(0) self.barrier = SimpleBarrier(4)
def __init__(self, n): self.mutex = Mutex() self.free_chairs = n self.queue = [] self.customer = Semaphore(0) self.barber = Semaphore(0) self.customerDone = Semaphore(0) self.barberDone = Semaphore(0)
def __init__(self): self.mutex = Mutex() self.mutex2 = Mutex() self.servings = 0 self.full_pot = Semaphore(0) self.empty_pot = Semaphore(0) self.barrier1 = SimpleBarrier(number_of_savages) self.barrier2 = SimpleBarrier(number_of_savages)
def __init__(self, N): self.N = N self.customers = 0 self.queue = [] self.mutex = Mutex() self.customer = Semaphore(0) self.customerDone = Semaphore(0) self.barberDone = Semaphore(0)
def wait(self, last_signal: Semaphore): self.M.lock() self.C += 1 if self.C == self.N: self.C = 0 # posledne vlakno nastavi pocitadlo na nulu last_signal.signal() self.T.signal(self.N) # uvolni N vlakien self.M.unlock() self.T.wait()
def __init__(self, c, m): self.c = c self.m = m self.board_queue = Semaphore(0) self.unboard_queue = Semaphore(0) self.barrier = SimpleBarrier(c) self.changed = Semaphore(0) self.loading_area = [Semaphore(0) for i in range(m)] self.unloading_area = [Semaphore(0) for i in range(m)]
def __init__(self, customer_capacity): self.N = customer_capacity self.customers = 0 self.mutex = Mutex() self.barber = Semaphore(0) self.customer = Semaphore(0) self.barber_done = Semaphore(0) self.customer_done = Semaphore(0)
def __init__(self, n, c): self.mutex = Mutex() self.chef_mutex = Mutex() self.servings = 0 self.num_of_chefs = c self.full_pot = Semaphore(0) self.empty_pot = Semaphore(0) self.barrier1 = SimpleBarrier(n) self.barrier2 = SimpleBarrier(n) self.chef_barrier2 = SimpleBarrier(c) self.chef_barrier = SimpleBarrier(c + 1)
def init(): accessData = Semaphore(1) turniket = Semaphore(1) ls_monitor = Lightswitch() ls_cidlo = Lightswitch() validData = Event() for monitor_id in range(2): Thread(monitor, monitor_id, turniket, validData, ls_monitor, accessData) for cidlo_id in range(11): Thread(cidlo, cidlo_id, turniket, validData, ls_cidlo, accessData)
def __init__(self): self.mutex = Mutex() self.cooking = Mutex() self.refill = Mutex() self.servings = 0 self.active_cooks = 0 self.cooks_done = 0 self.full_pot = Semaphore(0) self.empty_pot = Semaphore(0) self.barrier1 = SimpleBarrier(N) self.barrier2 = SimpleBarrier(N) self.pot_ready = Semaphore(0)
def __init__(self, n_savages=5, n_cooks=3): self.servings = 0 self.mutex = Mutex() self.SERVINGS_NEEDED = n_savages self.full_pot = Semaphore(0) self.empty_pot = Semaphore(0) self.barrier1 = SimpleBarrier(n_savages) self.barrier2 = SimpleBarrier(n_savages) self.cookLS = CookLS() self.cookBarrier1 = SimpleBarrier(n_cooks) self.cookBarrier2 = SimpleBarrier(n_cooks)
def __init__(self): self.lightswitch_sensors = Lightswitch() self.lightswitch_operators = Lightswitch() self.written_data_by_sensors = Event() self.without_operators = Semaphore(1) self.without_sensors = Semaphore(1) self.simple_barrier = SimpleBarrier(3) self.operator = Operator(self.lightswitch_operators, self.without_operators, self.without_sensors, self.simple_barrier) self.sensor = Sensor(self.lightswitch_sensors, self.written_data_by_sensors, self.without_sensors, self.without_operators, self.simple_barrier)
class SimpleBarrier: def __init__(self, n): self.N = n self.counter = 0 self.mutex = Mutex() self.turnstile = Semaphore(0) def wait(self): self.mutex.lock() self.counter += 1 if self.counter == self.N: self.counter = 0 self.turnstile.signal(self.N) self.mutex.unlock() self.turnstile.wait()
class SimpleBarrier: def __init__(self, N): self.N = N self.C = 0 self.M = Mutex() self.T = Semaphore(0) def wait(self): self.M.lock() self.C += 1 if self.C == self.N: self.C = 0 # posledne vlakno nastavi pocitadlo na nulu self.T.signal(self.N) # uvolni N vlakien self.M.unlock() self.T.wait()
class SimpleBarrier: def __init__(self, N): self.N = N self.C = 0 self.M = Mutex() self.T = Semaphore(0) def wait(self): self.M.lock() self.C += 1 if self.C == self.N: self.C = 0 self.T.signal(self.N) self.M.unlock() self.T.wait()
class Barrier: def __init__(self, N): self.N = N self.mutex = Mutex() self.counter = 0 self.semaphore = Semaphore(0) def wait(self): self.mutex.lock() self.counter += 1 if self.counter == self.N: self.counter = 0 self.semaphore.signal(self.N) self.mutex.unlock() self.semaphore.wait()
class SimpleBarrier(): def __init__(self, N): self.mutex = Mutex() self.capacity = N self.counter = 0 self.sem = Semaphore() def wait(self): self.mutex.lock() self.counter += 1 if self.counter == self.capacity: print(f'Lod zakotvila s posadkou {self.counter} ludi') self.counter = 0 self.sem.signal(self.capacity) self.mutex.unlock() self.sem.wait()
class SimpleBarrier: def __init__(self, num_of_threads): self.N = num_of_threads self.cnt = 0 self.mutex = Mutex() self.turnstile = Semaphore(0) def wait(self): self.mutex.lock() self.cnt += 1 if self.cnt == self.N: self.cnt = 0 for _ in range(self.N): self.turnstile.signal() self.mutex.unlock() self.turnstile.wait()
class SimpleBarrier: def __init__(self, N): self.N = N self.C = 0 self.M = Mutex() self.T = Semaphore(0) def wait(self, savage_id, barrier_id): self.M.lock() self.C += 1 print(f'savage {savage_id} before barrier {barrier_id}, {self.C} present') if self.C == self.N: print(f'savage {savage_id} opening barrier {barrier_id}') self.C = 0 # posledne vlakno nastavi pocitadlo na nulu self.T.signal(self.N) # uvolni N vlakien self.M.unlock() self.T.wait()
def __init__(self, numberOfThreads): self.numberOfThreads = numberOfThreads self.actualPosition = 1 self.fibannociList = [0, 1] self.semaphoreList = [] for _ in range(numberOfThreads): self.semaphoreList.append(Semaphore(0)) self.semaphoreList[0].signal()
def __init__(self, resources, smokers): self.agentSem = Semaphore(1) self.res = { resources[i]: { 'sem': Semaphore(0), 'val': 0, } for i in range(len(resources)) } self.res['mutex'] = Mutex() self.smo = { k: { 'sem': Semaphore(0), 'need': v, } for k, v in smokers.items() }
class SimpleBarrier: def __init__(self, n): self.N = n self.counter = 0 self.mutex = Mutex() self.turnstile = Semaphore(0) def wait(self, each=None, last=None): self.mutex.lock() self.counter += 1 if each: print(each) if self.counter == self.N: self.counter = 0 if last: print(last) self.turnstile.signal(self.N) self.mutex.unlock() self.turnstile.wait()
class SimpleBarrier: def __init__(self, N): self.N = N self.mutex = Mutex() self.cnt = 0 self.sem = Semaphore(0) def wait(self, print_str, id, print_last=False, print_each=False): self.mutex.lock() self.cnt += 1 if print_each: print(print_str.format(id, self.cnt)) if self.cnt == self.N: self.cnt = 0 if print_last: print(print_str.format(id)) self.sem.signal(self.N) self.mutex.unlock() self.sem.wait()