Exemple #1
0
def consumer_benchmark(repetitions, service_time):
    keys = ["consumption_time", "consumers_count", "consumed_items"]
    data = {keys[0]: [], keys[1]: [], keys[2]: []}
    for t_consumption in range(1, 11):
        for n_consumers in range(1, 11):
            partial_sum = 0
            consume_time = t_consumption / 100
            for _ in range(repetitions):
                warehouse = WareHouse(10)
                consumers = [
                    Thread(consumer, warehouse, consume_time)
                    for _ in range(n_consumers)
                ]
                producers = [
                    Thread(producer, warehouse, 0.05, 0.01) for _ in range(10)
                ]
                warehouse.stop_production(service_time)
                [thread.join() for thread in consumers + producers]
                partial_sum += warehouse.consumed / service_time
            consumed_per_sec = partial_sum / repetitions
            data[keys[0]].append(consume_time)
            data[keys[1]].append(n_consumers)
            data[keys[2]].append(consumed_per_sec)
    save_to_pickle(data)
    return data
Exemple #2
0
def init_and_run():

    threads = list()
    molecule = Molecule()

    # neustale sa tvoriace vlakna vodikov a kyslikov
    while True:
        threads.append(Thread(molecule.oxygenFunc))
        threads.append(Thread(molecule.hydrogenFunc))
Exemple #3
0
def run_model(n, m):
    threads = list()
    shared = SharedObject(n)
    for savage_id in range(n):
        threads.append(Thread(savage, savage_id, shared))
    threads.append(Thread(cook, m, shared))

    for t in threads:
        t.join()
Exemple #4
0
def test_simple_barrier(num_of_threads):
    sb = SimpleBarrier(num_of_threads)
    threads = list()
    for i in range(num_of_threads):
        t = Thread(simple_barrier_example, sb, i)
        threads.append(t)

    for t in threads:
        t.join()
Exemple #5
0
def run_model(n, m):
    threads = list()
    shared = SharedObject(m)
    for customer_id in range(n):
        threads.append(Thread(customer, customer_id, shared))
    threads.append(Thread(barber, shared))

    for t in threads:
        t.join()
Exemple #6
0
def test_reusable_barrier(num_of_threads):
    sb1 = SimpleBarrier(num_of_threads)
    sb2 = SimpleBarrier(num_of_threads)
    threads = list()
    for i in range(num_of_threads):
        t = Thread(reusable_barrier_example, sb1, sb2, f"Thread {i}")
        threads.append(t)

    for t in threads:
        t.join()
Exemple #7
0
def main():
    c = 4
    m = 3
    s = Shared(c, m)
    for i in range(15):
        Thread(passenger, s, i)
    for i in range(m):
        Thread(car, s, i)
    s.loading_area[0].signal()
    s.unloading_area[0].signal()
Exemple #8
0
def init_and_run(N, M, C):
    threads = list()
    shared = Shared()
    for savage_id in range(0, N):
        threads.append(Thread(savage, savage_id, C, shared))

    for cook_id in range(0, C):
        threads.append(Thread(cook, cook_id, M, C, shared))

    for t in threads:
        t.join()
Exemple #9
0
def run_threads():
    N_CUSTOMERS = 3
    CAP = 2
    barbershop = Shared(CAP)

    threads = [
        Thread(customer, id + 1, barbershop) for id in range(N_CUSTOMERS)
    ]
    threads.append(Thread(barber, barbershop))

    [th.join() for th in threads]
Exemple #10
0
def run():
    N_SAVAGES = 5
    N_COOKS = 2

    shared = Shared(N_SAVAGES, N_COOKS)

    savages = [Thread(savage, i, shared) for i in range(N_SAVAGES)]

    cooks = [Thread(cook, i, shared) for i in range(N_COOKS)]

    [th.join() for th in savages + cooks]
Exemple #11
0
def init_and_run():

    threads = list()
    shared = Shared()
    for savage_id in range(0, number_of_savages):
        threads.append(Thread(savage, savage_id, shared))

    for cook_id in range(0, number_of_cook):
        threads.append(Thread(cook, max_servings, shared, cook_id))

    for t in threads:
        t.join()
Exemple #12
0
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)
Exemple #13
0
def run_model(n, m, c):
    savages = list()
    chefs = list()
    shared = SharedObject(n, c)
    for chef_id in range(c):
        chefs.append(Thread(chef, chef_id, shared, m))
    for savage_id in range(n):
        savages.append(Thread(savage, savage_id, shared))

    for t in chefs:
        t.join()

    for t in savages:
        t.join()
Exemple #14
0
def run_model():
    oxygen.mid = 'O'
    hydrogen.mid = 'H'
    shared = Shared()

    while True:
        Thread(choice([oxygen, hydrogen]), shared)
        sleep(randint(0, 3) / 100)
Exemple #15
0
def test_simple_fibonacci(N):
    threads = list()
    fibonacci = [0, 1]
    mtx = Mutex()
    for thread_num in range(N):    
        threads.append(Thread(calculate_fibonacci_simple, mtx, fibonacci, thread_num))

    for t in threads:
        t.join()
Exemple #16
0
def init(x):
    accessData = Semaphore(1)
    turniket = Semaphore(1)
    ls_monitor = Lightswitch()
    ls_cidlo = Lightswitch()
    validData = Event()
    barrier_1 = Barrier(x)
    barrier_2 = Barrier(x)

    for monitor_id in range(x):
        Thread(monitor, monitor_id, turniket, validData,
               ls_monitor, accessData, barrier_1, barrier_2)

    for cidlo_id in range(2):
        # Cidla P a T
        Thread(cidlo, cidlo_id, turniket, validData,
               ls_cidlo, accessData, [10, 20])

    # Cidlo H
    Thread(cidlo, 2, turniket, validData, ls_cidlo, accessData, [20, 25])
Exemple #17
0
def main():
    forks = [Semaphore(1) for _ in range(PHIL_NUM)]
    footman = Semaphore(PHIL_NUM - 1)

    # phils = [Thread(phil, forks, footman, id) for id in range(PHIL_NUM)]

    lefties = randint(1, PHIL_NUM - 1)
    phil_type = [0] * lefties + [1] * (PHIL_NUM - lefties)
    phils = list()
    for phil_id in range(PHIL_NUM):
        if phil_type[phil_id]:
            phils.append(
                Thread(phil, forks, phil_id, phil_id,
                       ((phil_id + 1) % PHIL_NUM)))
        else:
            phils.append(
                Thread(phil, forks, phil_id, ((phil_id + 1) % PHIL_NUM),
                       phil_id))

    for p in phils:
        p.join()
Exemple #18
0
def test_fibonacci(N):
    threads = list()
    sync = list()
    fibonacci = [0, 1]
    for _ in range(N):
        sync.append(Event())
    # Implementacia moze byt aj pomocou Semaphore objektu:
    # for _ in range(N):
    #     sync.append(Semaphore(0))
    for thread_id in range(N):
        threads.append(Thread(calculate_fibonacci, sync, fibonacci, thread_id))

    for t in threads:
        t.join()
Exemple #19
0
def run_model():
    shared = Shared()

    smokers = []
    smokers.append(Thread(smoker_match, shared))
    smokers.append(Thread(smoker_tobacco, shared))
    smokers.append(Thread(smoker_paper, shared))

    agents = []
    agents.append(Thread(agent_1, shared))
    agents.append(Thread(agent_2, shared))
    agents.append(Thread(agent_3, shared))

    for t in agents + smokers:
        t.join()
Exemple #20
0
            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()


threads = []
sh = Shared()
for i in range(100):
    t = Thread(sh.oxygen)
    threads.append(t)

for i in range(200):
    t = Thread(sh.hydrogen)
    threads.append(t)

for t in threads:
    t.join()
Exemple #21
0
from fei.ppds import print


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()


def barrier_example(barrier, thread_id):
    sleep(randint(1, 10) / 10)
    print("vlakno %s pred barierou" % thread_id)
    barrier.wait()
    print("vlakno %s po bariere" % thread_id)


sb = SimpleBarrier(5)
for i in range(5):
    t = Thread(barrier_example, sb, f'Thread {i}')
Exemple #22
0
def main():
    s = Shared()
    for i in range(5):
        Thread(hacker, s, i)
        Thread(serf, s, i)
Exemple #23
0
            sh.dealerCounter -= 4
            sh.dealersQ.signal(4)
            isCaptain = True

        elif sh.officerCounter == 2 and sh.dealerCounter >= 2:
            sh.officerCounter -= 2
            sh.dealerCounter -= 2
            sh.officersQ.signal(2)
            sh.dealersQ.signal(2)
            isCaptain = True

        else:
            sh.mutex.unlock()

        sh.dealersQ.wait()
        print(f'Dealer {id} nastupuje na lod')
        sail(sh, isCaptain)


threads = []
sh = Shared()

officers = 10
dealers = 10

for i in range(officers):
    threads.append(Thread(officer, sh, i))

for i in range(dealers):
    threads.append(Thread(dealer, sh, i))
Exemple #24
0
    while True:
        shared.mutex.lock()
        if shared.counter >= shared.end:
            shared.mutex.unlock()
            break
        shared.array[shared.counter] += 1
        shared.counter += 1
        shared.mutex.unlock()


def counter3(shared):
    while True:
        shared.mutex.lock()
        cnt = shared.counter
        shared.counter +=1
        shared.mutex.unlock()
        if cnt >= shared.end:
            break
        shared.array[cnt] += 1


for i in range(5):
    sh = Shared(1_000_000)
    t1 = Thread(counter3, sh)
    t2 = Thread(counter3, sh)

    t1.join()
    t2.join()

    print(Histogram(sh.array))
Exemple #25
0
def main():
    s = Shared(0)
    for i in range(10):
        Thread(savage, i, s)
    Thread(cook, s)
Exemple #26
0
        shared.turn.signal()
        sleep(randint(0, 10) / 10)

        lightSwitch.lock(shared.room)
        print("R - inside ")
        sleep(0.3 + randint(0, 4) / 10)
        lightSwitch.unlock(shared.room)
        print("R - after ")


shared = Shared()
lightSwitch = LightSwitch()
threads = []

# Pocet zapisovatelov
n_of_writers = 1

# Pocet citatelov
n_of_readers = 3

for i in range(n_of_writers):
    t = Thread(writer, shared)
    threads.append(t)

for i in range(n_of_readers):
    t = Thread(reader, shared)
    threads.append(t)

for t in threads:
    t.join()
Exemple #27
0
        self.mutex = Mutex()


def compute_fibonacci(fib, i):
    sleep(randint(1, 10) / 100)  # vynutene prepnutie
    while True:
        fib.mutex.lock()
        if (fib.count == i):
            fib.arr[i + 2] = fib.arr[i + 1] + fib.arr[i]
            fib.count += 1
            fib.mutex.unlock()
            break
        fib.mutex.unlock()


fib = Fibonacci(20)
threads = list()

for i in range(fib.N):
    threads.append(Thread(compute_fibonacci, fib, i))

for t in threads:
    t.join()

print(fib.arr)

for i in range(2, len(fib.arr)):  # kontrola synchronizacie
    if (fib.arr[i] != fib.arr[i - 2] + fib.arr[i - 1]):
        print("Chyba v synchronizacii")
        break
Exemple #28
0
class SimpleBarrier:
    def __init__(self, number_of_threads):
        self.number_of_threads = number_of_threads
        self.mutex = Mutex()
        self.event = Event()
        self.counter = 0

    def wait(self):
        self.mutex.lock()
        self.counter += 1
        if self.counter == self.number_of_threads:
            self.event.signal()
        self.mutex.unlock()
        self.event.wait()


threads = []
power_station = PowerStation()

threads.append(Thread(power_station.sensor.sensor_H, 0))
threads.append(Thread(power_station.sensor.sensor_P_T, 1))
threads.append(Thread(power_station.sensor.sensor_P_T, 2))

for i in range(8):
    t = Thread(power_station.operator.operator, i)
    threads.append(t)

for t in threads:
    t.join()
Exemple #29
0
    sh.ls.lock()
    print(f'Reader {i} vstupil do miestnosti')
    for j in range(20):
        _ = 2**j
    sh.ls.unlock()
    print(f'Reader {i} odisiel z miestnosti')


def writer(sh, i):
    sleep(randint(0, 8)/10)
    sh.sem.wait()
    print(f'Writer {i} vstupil do miestnosti')
    for j in range(20):
        _ = 2**j
    sh.sem.signal()
    print(f'Writer {i} odisiel z miestnosti')


sh = Shared()

threads = list()

for i in range(20):
    threads.append(Thread(writer, sh, i))

for i in range(100):
    threads.append(Thread(reader, sh, i))

for i in threads:
    i.join()
def main():
    c = 4
    s = Shared(c)
    for i in range(5):
        Thread(passenger, s, i)
    Thread(car, s)