Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. import thread
  2. import time, random
  3. import threading
  4.  
  5. garfo = list()
  6. for i in range(5):
  7. garfo.append(threading.Semaphore(1))
  8.  
  9. def filosofo(f):
  10. f = int(f)
  11. while True:
  12. # garfo da esquerda
  13. garfo[f].acquire()
  14. # garfo da direita
  15. garfo[(f + 1) % 5].acquire()
  16. i = random.randint(1, 5)
  17. print ("Filosofo %i comendo por %i segundos..." %(f,i))
  18. time.sleep(i)
  19. garfo[f].release()
  20. garfo[(f + 1) % 5].release()
  21. t = random.randint(1, 10)
  22. print ("Filosofo %i pensando por %i segundos..."%(f,t))
  23. time.sleep(t)
  24.  
  25.  
  26. for i in range(5):
  27. print ("Filosofo",i,"iniciado")
  28. thread.start_new_thread(filosofo, tuple([i]))
  29.  
  30. while 1: pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement