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()
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 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.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 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()
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()
class PowerPlant: def __init__(self): self.barrier = Barrier(3) self.monitor_ls = LightSwitch() self.sensor_ls = LightSwitch() self.no_sensors = Semaphore(1) self.no_monitors = Semaphore(1) def monitor(self, monitor_id): while True: self.barrier.event.wait() self.no_monitors.wait() number_of_monitors_reading = self.monitor_ls.lock(self.no_sensors) self.no_monitors.signal() duration = randint(40, 50) / 1000 print('monit "%02d": ' 'pocet_citajucich_monitorov=%02d, ' 'trvanie_citania=%03d\n' % (monitor_id, number_of_monitors_reading, duration*1000)) sleep(duration) self.monitor_ls.unlock(self.no_sensors) def sensor(self, sensor_id, duration): while True: sleep(randint(50, 60) / 1000) number_of_sensors_writing = self.sensor_ls.lock(self.no_monitors) self.no_sensors.wait() print('cidlo "%02d": ' 'pocet_zapisujucich_cidiel=%02d, ' 'trvanie_zapisu=%03d\n' % (sensor_id, number_of_sensors_writing, duration * 1000)) sleep(duration) self.no_sensors.signal() self.barrier.wait_with_events() self.sensor_ls.unlock(self.no_monitors)
class SimpleBarrier: def __init__(self, N): self.N = N # pocet vlaken, ktore budu cakat self.counter = 0 # premenna, ktora rata pocet vlaken, co cakaju self.mutex = Semaphore( 1) # Semaphor inicializovany na 1 je to iste co Mutex self.barriera = Semaphore(0) # def barrier(self): # vlakno prislo ku bariere, zvys counter ze prislo # v jednom case moze inkrementovat counter iba jedno vlakno self.mutex.wait() self.counter += 1 self.mutex.signal() # ak prisli vsetky vlakna k bariere otvor ju a pusti prveho... # to co dojde posledne aj prve vyjde (?) if self.counter == self.N: self.barriera.signal() # inak budu vlakna cakat self.barriera.wait() # pustene vlakno pusti dalsie self.barriera.signal()
class Barrier(object): def __init__(self, N): self.N = N self.cnt = 0 self.mutex = Mutex() self.b1 = Semaphore(0) self.b2 = Semaphore(0) def wait(self): self.mutex.lock() self.cnt += 1 if self.cnt == self.N: print("------") self.b1.signal(self.N) self.mutex.unlock() self.b1.wait() self.mutex.lock() self.cnt -= 1 if self.cnt == 0: self.b2.signal(self.N) self.mutex.unlock() self.b2.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, savage_id, print_last_thread=False, print_each_thread=False): self.mutex.lock() self.cnt += 1 if print_each_thread: print(print_str % (savage_id, self.cnt)) if self.cnt == self.N: self.cnt = 0 if print_last_thread: print(print_str % (savage_id)) self.sem.signal(self.N) self.mutex.unlock() self.sem.wait()
class Shared(object): def __init__(self): self.mutex = Semaphore(1) self.oxygenCount = 0 self.hydrogenCount = 0 self.oxyQueue = Semaphore(1) self.hydroQueue = Semaphore(2) self.barrier = Barrier(3) def oxygen(self): """sleep na simulovanie náhodného príchodu atómov""" sleep(randint(1, 10) / 10) """ spoločný semafor, na ktorom atómy čakajú a vstupujú po jednom a zvýšenie počídla, keďže vstupujú po jednom, tak nepotrebujeme už nijak chrániť prístup k premennej oxygenCount. """ self.mutex.wait() self.oxygenCount += 1 print("Oxygen count = ", self.oxygenCount) """ podmienka, ktora overuje, či je aj dostatok atómov na tvorbu molekuly vody, ak nie je, tak pusti ďalší atóm cez semafor, inak spraví zlúčenie. """ if self.hydrogenCount < 2: self.mutex.signal() else: self.oxygenCount -= 1 self.hydrogenCount -= 2 self.oxyQueue.signal() self.hydroQueue.signal(2) print("bonding") print("count after bond: oxygen: %d, hydrogen: %d\n" % (self.oxygenCount, self.hydrogenCount)) """ Kyslík čaká, kým mu prídu dva vodíky, cez prejde len jeden. """ self.oxyQueue.wait() """ Bariera, ktorá pusti cez 3 prvky, aby nenastalo, že nejaký prvok sa stihne obehnúť """ self.barrier.wait() """ Po úšpešnom spojení nastane, kyslík otvorí semafor pre ďalší prvok aby nenastal deadlock. """ self.mutex.signal() def hydrogen(self): """sleep na simulovanie náhodného príchodu atómov""" sleep(randint(1, 10) / 10) """ spoločný semafor, na ktorom atómy čakajú a vstupujú po jednom a zvýšenie počídla, keďže vstupujú po jednom, tak nepotrebujeme už nijak chrániť prístup k premennej hydrogenCount. """ self.mutex.wait() self.hydrogenCount += 1 print("Hydrogen count = ", self.hydrogenCount) """ podmienka, ktora overuje, či je aj dostatok atómov na tvorbu molekuly vody a kyslíka, ak nie je, tak pustí ďalší atóm cez semafor, inak spraví zlúčenie. """ if self.hydrogenCount < 2 or self.oxygenCount < 1: self.mutex.signal() else: self.oxygenCount -= 1 self.hydrogenCount -= 2 self.oxyQueue.signal() self.hydroQueue.signal(2) print("bonding") print("count after bond: oxygen: %d, hydrogen: %d\n" % (self.oxygenCount, self.hydrogenCount)) """ Vodík čaká, kým mu príde ďalší vodík a kyslík, cez prejdu dva. """ self.hydroQueue.wait() """ Bariera, ktorá pusti cez 3 prvky, aby nenastalo, že nejaký prvok sa stihne obehnúť. """ self.barrier.wait()
class Molecule: def __init__(self): # binarny semafor self.mutex = Semaphore(1) self.oxygenQueue = Semaphore(0) self.hydrogenQueue = Semaphore(0) self.oxygen = 0 self.hydrogen = 0 self.barrier = Barrier(3) def oxygenFunc(self): # zamadzenie pristupu do KO self.mutex.wait() self.oxygen += 1 """ Ak je este nedostatocny pocet vodikov tak chod dalej a uvolni pridanie sa dalsiemu kysliku """ if self.hydrogen < 2: self.mutex.signal() else: self.oxygen -= 1 self.hydrogen -= 2 # odobera sa kyslik z "fronty" self.oxygenQueue.signal() # odoberaju sa 2 vodiky z "fronty" self.hydrogenQueue.signal(2) # Pridava sa jeden kyslik do "fronty" self.oxygenQueue.wait() print("oxygen do zlucenia") # zlucovanie self.bond() """ bariera, kde sa caka na dobehnutie vsetkych 3 atomov po zluceni, aby sa mohli ist dalsie atomy zlucovat """ self.barrier.wait() self.mutex.signal() def hydrogenFunc(self): # zamadzenie pristupu do KO self.mutex.wait() self.hydrogen += 1 """ Ak je nedostatocny pocet kyslikov alebo vodikov tak uvolni pridanie sa dalsiemu vodiku a chood dalej """ if self.hydrogen < 2 or self.oxygen < 1: self.mutex.signal() else: self.oxygen -= 1 self.hydrogen -= 2 # odobera sa kyslik z "fronty" self.oxygenQueue.signal() # odoberaju sa 2 vodiky z "fronty" self.hydrogenQueue.signal(2) # Pridava sa vodik do zlucenia self.hydrogenQueue.wait() print("Hydrogen do zlucenia") # zlucovanie self.bond() """ bariera, kde sa caka na dobehnutie vsetkych 3 atomov po zluceni, aby sa mohli ist dalsie atomy zlucovat """ self.barrier.wait() def bond(self): # prevedenie zlucovania sleep(randint(5, 15) / 10) print("Zlucenie buniek")