Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import time
- from multiprocessing import Process, Manager, Value, Barrier, Semaphore
- from math import gcd
- prvni = Value('i', -1)
- druhy = Value('i', -1)
- soudelna = Value('i', -1)
- empty_buff1 = Semaphore(1)
- empty_buff2 = Semaphore(1)
- full_buff1 = Semaphore(1)
- full_buff1.acquire()
- full_buff2 = Semaphore(1)
- onecount = Value('i', 0)
- totalcount = Value('i', 0)
- totalcount2 = Value('i', 0)
- globallock = Value('i', 1)
- def producent():
- while globallock.value:
- empty_buff1.acquire()
- prvni.value = random.randint(0, 1000000)
- druhy.value = random.randint(0, 1000000)
- full_buff1.release()
- def prod_konz():
- while globallock.value:
- full_buff1.acquire()
- empty_buff2.acquire()
- if gcd(prvni.value, druhy.value) > 1:
- soudelna.value = 1
- else:
- soudelna.value = 0
- totalcount2.value += 1
- empty_buff1.release()
- full_buff2.release()
- def konzument():
- while globallock.value:
- full_buff2.acquire()
- if soudelna.value == 1:
- onecount.value += 1
- totalcount.value += 1
- if totalcount.value >= N:
- globallock.value = 0
- empty_buff2.release()
- if __name__ == "__main__":
- manager = Manager()
- N = 1000
- processes = []
- functions = [producent, prod_konz, konzument]
- starttime = time.time()
- for i in functions:
- p = Process(target=i)
- processes.append(p)
- p.start()
- for p in processes:
- p.join()
- print(onecount.value/totalcount.value)
- print(totalcount.value, totalcount2.value)
- print(time.time()-starttime)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement