예제 #1
0
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()
예제 #2
0
def agent_3(shared):
    while True:
        sleep(randint(0, 10) / 100)
        shared.agentSem.wait()
        print("agent: tobacco, match")
        shared.tobacco.signal()
        shared.match.signal()
예제 #3
0
def print_res(res_id, old, new):
    s = "\tp_w_%s: " % res_id
    for k in old.keys():
        if k == 'mutex':
            continue
        s += "%s %d-->%d; " % (k, old[k]['val'], new[k]['val'])
    print(s)
예제 #4
0
파일: Zadanie6.py 프로젝트: japubek/PPaDS
    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()
예제 #5
0
파일: savages.py 프로젝트: pvavercak/ppds
 def lock(self, cook_id, shared):
     self.mutex.lock()
     self.cnt += 1
     if self.cnt == 1:
         shared.empty_pot.wait()
         print(f"kuchar {cook_id:02}: spustam varenie")
     self.mutex.unlock()
예제 #6
0
def agent_1(shared):
    while True:
        sleep(randint(0, 10) / 100)
        shared.agentSem.wait()
        print("agent: tobacco, paper")
        shared.tobacco.signal()
        shared.paper.signal()
예제 #7
0
def agent_2(shared):
    while True:
        sleep(randint(0, 10) / 100)
        shared.agentSem.wait()
        print("agent: paper, match")
        shared.paper.signal()
        shared.match.signal()
예제 #8
0
def cook(s):
    while True:
        s.empty_pot.wait()
        print('cook cooking')
        sleep(randint(50, 200) / 100)
        print('cook: adding servings to the pot')
        s.put_servings_to_pot(10)
        s.full_pot.signal()
예제 #9
0
def fnc_process(shared):
    shared.items.wait()
    shared.mutex.lock()
    item = shared.queue.pop(0)
    print("Spracovany %d vyrobok" % item.id)
    shared.mutex.unlock()
    shared.free.signal()
    sleep(PROCESS_TIME)  # process time
예제 #10
0
파일: savages.py 프로젝트: pvavercak/ppds
 def unlock(self, cook_id, shared):
     self.mutex.lock()
     self.cnt -= 1
     if self.cnt == 0:
         print(f"kuchar {cook_id:02}: varenie ukoncene")
         shared.servings = shared.SERVINGS_NEEDED
         shared.full_pot.signal()
     self.mutex.unlock()
예제 #11
0
def agent(agent_id, res, shared):
    while True:
        #sleep(randint(0,1)/100)
        sleep(randint(9, 10) / 100)
        shared.agentSem.wait()
        shuffle(res)
        print("agent %s:" % agent_id, res)
        for r in res:
            shared.res[r]['sem'].signal()
예제 #12
0
def savage(savage_id, s):
    while True:
        s.mutex.lock()
        if s.servings == 0:
            print(f'savage {savage_id} signalling cook')
            s.empty_pot.signal()
            s.full_pot.wait()
        s.get_serving_from_pot(savage_id)
        s.mutex.unlock()
        eat(savage_id)
예제 #13
0
def put_serving_in_pot(servings, shared, cook_id):

    print("kuchar %2d: varim" % cook_id)
    sleep(0.4 + randint(0, 2) / 10)
    shared.mutex2.lock()
    shared.servings += 1
    if shared.servings == servings:
            print("kuchar %2d hrniec je plny" % cook_id)
            shared.full_pot.signal()
    shared.mutex2.unlock()
예제 #14
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()
예제 #15
0
def compute_fibonacci(fib, i):
    while True:
        fib.wait()
        if (fib.count == i):
            fib.arr[i + 2] = fib.arr[i + 1] + fib.arr[i]
            fib.count += 1
            fib.N -= 1
            fib.signal()
            print(f'Zostavajucich vlakien: {fib.N}')
            break
예제 #16
0
def smoker_paper(shared):
    while True:
        sleep(randint(0, 10) / 100)
        shared.tobacco.wait()
        print("\ttobacco: tooken by smoker_paper")
        shared.match.wait()
        print("smokes: smoker_paper")
        make_cigarette()
        shared.agentSem.signal()
        smoke()
예제 #17
0
파일: zadanie3.py 프로젝트: japubek/PPaDS
def write(shared):
    while True:
        sleep(randint(1, 10)/timeForWrite)
        # print("vpisovanie1")
        shared.semaphore.wait()
        # shared.turniket.wait()
        # print("vypisovanie2")
        sleep(randint(1, 10)/timeForWrite)
        # shared.turniket.signal()
        shared.semaphore.signal()
        print("vpisovanie3")
예제 #18
0
def monitor(id, data_present, monitor_ls, block_sensors, block_monitors):
    data_present.wait()
    while True:
        block_monitors.wait()
        monitor_count = monitor_ls.lock(block_sensors)
        block_monitors.signal()
        read_time = randint(40, 50)
        print(
            f'monit "{id:02}": pocet_citajucich_monitorov='
            f'{monitor_count:02}, trvanie_citania={read_time:02}')
        sleep(read_time / 1000)
        monitor_ls.unlock(block_sensors)
예제 #19
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()
예제 #20
0
파일: zadanie4.py 프로젝트: japubek/PPaDS
 def operator(self, operator_id):
     while True:
         self.simple_barrier.wait()
         self.without_operators.wait()
         num_reading_ops = self.ls_ops.lock(self.without_sensors)
         self.without_operators.signal()
         waiting_time_of_operator = randint(4, 5) / 100
         print('monit "%03d": pocet_citajucich_monitorov=%03d,'
               'trvanie_citania=%0.3f\n' %
               (operator_id, num_reading_ops, waiting_time_of_operator))
         sleep(waiting_time_of_operator)
         self.ls_ops.unlock(self.without_sensors)
예제 #21
0
def check_smokers_res(res_avail, shared):
    smo_avail = []
    for s in shared.smo.keys():
        s_keys = shared.smo[s]['need'].keys()
        if not set(res_avail).issuperset(set(s_keys)):
            continue
        for need in s_keys:
            if shared.res[need]['val'] < shared.smo[s]['need'][need]:
                continue
        smo_avail.append(s)
    print("\tSMO_AVAIL=", smo_avail)
    return smo_avail
예제 #22
0
파일: Zadanie_5.py 프로젝트: Kubo-SVK/ppds
def put_servings_in_pot(id, M, shared):
    while True:
        shared.cooking.lock()
        if(shared.active_cooks == M):
            shared.cooking.unlock()
            return
        shared.active_cooks += 1
        shared.cooking.unlock()
        print(f'kuchar {id}: varim')
        # navarenie jedla tiez cosi trva...
        sleep(0.4 + randint(0, 2) / 10)
        shared.servings += 1
예제 #23
0
파일: savages.py 프로젝트: pvavercak/ppds
 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()
예제 #24
0
파일: Zadanie_5.py 프로젝트: Kubo-SVK/ppds
def cook(id, M, C, shared):
    while True:
        shared.empty_pot.wait()
        put_servings_in_pot(id, M, shared)

        shared.refill.lock()
        print(f'kuchar {id}: hotovo')
        shared.cooks_done += 1
        if(shared.cooks_done == C):
            shared.active_cooks = 0
            shared.cooks_done = 0
            shared.full_pot.signal()
        shared.refill.unlock()
예제 #25
0
파일: zadanie4.py 프로젝트: japubek/PPaDS
 def sensor_H(self, sensor_id):
     while True:
         sleep(randint(5, 6) / 100)
         num_writing_sens = self.ls_sens.lock(self.without_operators)
         self.without_sensors.wait()
         self.without_sensors.signal()
         waiting_time_of_sensor = randint(20, 25) / 1000
         print('cidlo "%03d": pocet_zapisujucich_cidiel=%03d,'
               "trvanie_zapisu=%0.3f\n" %
               (sensor_id, num_writing_sens, waiting_time_of_sensor))
         sleep(waiting_time_of_sensor)
         self.simple_barrier.wait()
         self.ls_sens.unlock(self.without_operators)
예제 #26
0
    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)
예제 #27
0
def sensor(id, update_time: Callable[[], float], data_present,
           sensor_ls, block_sensors, block_monitors):
    while True:
        sensor_count = sensor_ls.lock(block_monitors)
        block_sensors.wait()
        update = update_time()
        print(
            f'cidlo "{id:02}": pocet_zapisujucich_cidiel='
            f'{sensor_count:02}, trvanie_zapisu={update:02}')
        sleep(update / 1000)
        block_sensors.signal()
        sensor_ls.unlock(block_monitors)
        data_present.signal()
        sleep(randint(50, 60) / 1000)
예제 #28
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()
예제 #29
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()
예제 #30
0
    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)