Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def producent():
- while globallock.value:
- sem1.acquire()
- prvni.value = random.randint(0, 1000000)
- druhy.value = random.randint(0, 1000000)
- sem2.release()
- def prod_konz():
- while globallock.value:
- sem2.acquire()
- if gcd(prvni.value, druhy.value) > 1:
- soudelna.value = 1
- else:
- soudelna.value = 0
- druhy.value = -1
- totalcount2.value += 1
- sem1.release()
- sem3.release()
- def konzument():
- while globallock.value:
- sem3.acquire()
- if soudelna.value == 1:
- onecount.value += 1
- soudelna.value = -1
- totalcount.value += 1
- if totalcount.value >= N:
- globallock.value = 0
- if __name__ == "__main__":
- manager = Manager()
- N = 10000
- prvni = manager.Value('i', -1)
- druhy = manager.Value('i', -1)
- soudelna = manager.Value('i', -1)
- sem1 = Semaphore(1)
- sem2 = Semaphore(1)
- sem2.acquire()
- sem3 = Semaphore(1)
- sem3.acquire()
- onecount = manager.Value('i', 0)
- totalcount = manager.Value('i', 0)
- totalcount2 = manager.Value('i', 0)
- globallock = manager.Value('i', 1)
- 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