示例#1
0
 def __init__(self, n):
     self.N = n
     self.counter = 0
     self.fibonacci = [0, 1] + [0] * n
     self.threads = [0] * (n + 1)
     self.mutex = Mutex()
     self.event = Event()
     self.event.signal()
     for j in range(n + 1):
         self.threads[j] = Semaphore(0)
     self.threads[0].signal(1)
示例#2
0
文件: zadanie4.py 项目: japubek/PPaDS
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()
示例#3
0
class Barrier:
    def __init__(self, n):
        self.n = n
        self.count = 0
        self.mutex = Mutex()
        self.event = Event()

    def wait(self):
        self.mutex.lock()
        self.count += 1
        if self.count == self.n:
            self.event.signal()
        self.mutex.unlock()
        self.event.wait()
示例#4
0
文件: Atomka_2.py 项目: Kubo-SVK/ppds
class Barrier():
    def __init__(self, n):
        self.mutex = Mutex()
        self.event = Event()
        self.counter = n
        self.n = n

    def wait(self):
        self.mutex.lock()
        self.counter -= 1
        if(self.counter == 0):
            self.counter = self.n
            self.event.signal()
            self.mutex.unlock()
            return
        self.mutex.unlock()
        self.event.wait()
 def __init__(self, size, adt):
     self.adt = adt
     self.size = size
     self.counter = 0
     self.fibonacci = list()
     self.fibonacci.append(0)
     self.fibonacci.append(1)
     if adt == "semaphore":
         self.semaphore = Semaphore(1)
     elif adt == "event":
         self.event = Event()
示例#6
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)
示例#7
0
文件: main.py 项目: mkov2009/ppds
 def __init__(self, count):
     self.n = count
     self.mutex = Mutex()
     self.events = [0] * count
     for j in range(count):
         self.events[j] = Event()
     self.events[0].signal()
     self.array = [0, 1] + [0] * count
     self.sem = [0] * count
     for j in range(count):
         self.sem[j] = Semaphore(0)
     self.sem[0].signal()
示例#8
0
class Shared:
    def __init__(self, n):
        self.N = n
        self.counter = 0
        self.fibonacci = [0, 1] + [0] * n
        self.threads = [0] * (n + 1)
        self.mutex = Mutex()
        self.event = Event()
        self.event.signal()
        for j in range(n + 1):
            self.threads[j] = Semaphore(0)
        self.threads[0].signal(1)

    def fnc_fibonacci_seq(self, pin):
        self.threads[pin].wait()
        self.fibonacci[pin + 2] = self.fibonacci[pin] + self.fibonacci[pin + 1]
        self.threads[pin + 1].signal()

    def fnc_fibonacci_event(self, pin):
        while True:
            self.event.wait()
            self.mutex.lock()
            if self.counter == pin:
                break
            self.mutex.unlock()
        self.mutex.unlock()
        self.mutex.lock()
        self.counter += 1
        self.mutex.unlock()
        self.fibonacci[pin + 2] = self.fibonacci[pin] + self.fibonacci[pin + 1]
        self.event.signal()
示例#9
0
文件: zadanie4.py 项目: japubek/PPaDS
 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)
示例#10
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()
示例#11
0
文件: Atomka_2.py 项目: Kubo-SVK/ppds
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])
示例#12
0
class SimpleBarrierEvent:
    def __init__(self, num_of_threads):
        self.N = num_of_threads
        self.C = num_of_threads
        self.mutex = Mutex()
        self.turnstile = Event()

    def wait(self):
        self.mutex.lock()
        if self.C == 0:
            self.turnstile.clear()
            self.C += 1
            if self.C == self.N:
                self.turnstile.set()
        self.mutex.unlock()
        self.turnstile.wait()
示例#13
0
文件: seminar.py 项目: japubek/PPaDS
class ReusableBarrier:
    def __init__(self, numberOfThreads):
        self.numberOfThreads = numberOfThreads
        self.mutex = Mutex()
        self.event = Event()
        self.counter = 0

    def wait(self):
        self.mutex.lock()
        if self.counter == 0:
            self.event.clear()
        self.counter += 1

        if self.counter == self.numberOfThreads:
            self.event.signal()
            self.counter = 0

        self.mutex.unlock()
        self.event.wait()
示例#14
0
文件: zadanie4.py 项目: japubek/PPaDS
 def __init__(self, number_of_threads):
     self.number_of_threads = number_of_threads
     self.mutex = Mutex()
     self.event = Event()
     self.counter = 0
示例#15
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)


data_present = Event()
block_sensors = Semaphore(1)
block_monitors = Semaphore(1)
sensor_ls = LightSwitch()
monitor_ls = LightSwitch()

sensors = [
    Thread(sensor, sensor_id, lambda: randint(10, 20),
           data_present, sensor_ls,
           block_sensors, block_monitors)
    for sensor_id in range(N_SENSORS - 1)]

sensors.append(
    Thread(sensor, N_SENSORS - 1, lambda: randint(20, 25),
           data_present, sensor_ls,
           block_sensors, block_monitors))
示例#16
0
 def __init__(self, n):
     self.n = n
     self.count = 0
     self.mutex = Mutex()
     self.event = Event()
示例#17
0
 def __init__(self, num_of_threads):
     self.N = num_of_threads
     self.C = num_of_threads
     self.mutex = Mutex()
     self.turnstile = Event()