Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import thread
- import time, random
- import threading
- garfo = list()
- for i in range(5):
- garfo.append(threading.Semaphore(1))
- def filosofo(f):
- f = int(f)
- while True:
- # garfo da esquerda
- garfo[f].acquire()
- # garfo da direita
- garfo[(f + 1) % 5].acquire()
- i = random.randint(1, 5)
- print ("Filosofo %i comendo por %i segundos..." %(f,i))
- time.sleep(i)
- garfo[f].release()
- garfo[(f + 1) % 5].release()
- t = random.randint(1, 10)
- print ("Filosofo %i pensando por %i segundos..."%(f,t))
- time.sleep(t)
- for i in range(5):
- print ("Filosofo",i,"iniciado")
- thread.start_new_thread(filosofo, tuple([i]))
- while 1: pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement