Advertisement
BlueManCZ

Untitled

Mar 21st, 2018
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.58 KB | None | 0 0
  1. def producent():
  2.     while globallock.value:
  3.         sem1.acquire()
  4.         prvni.value = random.randint(0, 1000000)
  5.         druhy.value = random.randint(0, 1000000)
  6.         sem2.release()
  7.  
  8.  
  9. def prod_konz():
  10.     while globallock.value:
  11.         sem2.acquire()
  12.         if gcd(prvni.value, druhy.value) > 1:
  13.             soudelna.value = 1
  14.         else:
  15.             soudelna.value = 0
  16.         druhy.value = -1
  17.         totalcount2.value += 1
  18.         sem1.release()
  19.         sem3.release()
  20.  
  21.  
  22. def konzument():
  23.     while globallock.value:
  24.         sem3.acquire()
  25.         if soudelna.value == 1:
  26.             onecount.value += 1
  27.         soudelna.value = -1
  28.         totalcount.value += 1
  29.         if totalcount.value >= N:
  30.             globallock.value = 0
  31.  
  32.  
  33. if __name__ == "__main__":
  34.     manager = Manager()
  35.  
  36.     N = 10000
  37.  
  38.     prvni = manager.Value('i', -1)
  39.     druhy = manager.Value('i', -1)
  40.     soudelna = manager.Value('i', -1)
  41.  
  42.     sem1 = Semaphore(1)
  43.     sem2 = Semaphore(1)
  44.     sem2.acquire()
  45.     sem3 = Semaphore(1)
  46.     sem3.acquire()
  47.  
  48.     onecount = manager.Value('i', 0)
  49.     totalcount = manager.Value('i', 0)
  50.     totalcount2 = manager.Value('i', 0)
  51.  
  52.     globallock = manager.Value('i', 1)
  53.  
  54.     processes = []
  55.     functions = [producent, prod_konz, konzument]
  56.     starttime = time.time()
  57.     for i in functions:
  58.         p = Process(target=i)
  59.         processes.append(p)
  60.         p.start()
  61.  
  62.     for p in processes:
  63.         p.join()
  64.  
  65.     print(onecount.value/totalcount.value)
  66.     print(totalcount.value, totalcount2.value)
  67.     print(time.time()-starttime)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement