Could you help me with this code, I need to do "faneuil Hall problem"
this says the following:
"There are three kinds of threads: immigrants, spectators, and a one judge Immigrants must wait in line, check in, and then sit down. At some point, the judge enters the building. When the judge is in the building, no one may enter, and the immigrants may not leave. Spectators may leave. Once all immigrants check in, the judge can confirm the naturalization After the confirmation, the immigrants pick up their certificates of U.S. Citizenship. The judge leaves at some point after the confirmation. Spectators may now enter as before. After immigrants get their certificates, they may leave. "
The thing is that when synchronizing the threads, the immigrant thread does not unblock the judge so in the if it takes me as juez = 0
I do not know what I'm doing wrong if you could lend me a hand and explain to me that I'm wrong. Thank you very much ..
import threading
import time
condition = threading.Semaphore(1)
mutex = threading.Semaphore(1)
confirmar = threading.Event()
entrar = 0
registrar = 0
juez = 0
class Inmigrante(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
global entrar, registrar
print ("comenzar inmigrante\n")
condition.acquire()
print("[%s]Inmigrante: Entrar\n" % time.strftime("%H:%M:%S"))
entrar += 1
print("[%s]Inmigrante: Entrar: %d\n" % (time.strftime("%H:%M:%S"), entrar))
print ("aaa\n")
condition.release()
mutex.acquire()
print("[%s]Inmigrante: Registar\n" % time.strftime("%H:%M:%S"))
registrar += 1
print("[%s]Inmigrante: Registar: %d\n" % (time.strftime("%H:%M:%S"), registrar))
print ("valor de juez es: %d\n" % juez )
if(juez == 1 and entrar == registrar):
mutex.notify()
else:
mutex.release()
print("[%s]Inmigrante: Sentarme\n" % time.strftime("%H:%M:%S"))
confirmar.wait()
print("[%s]Inmigrante: Jurar\n" % time.strftime("%H:%M:%S"))
print("[%s]Inmigrante: Obtener certificado\n" % time.strftime("%H:%M:%S"))
condition.acquire()
print("[%s]Inmigrante: Salir\n" % time.strftime("%H:%M:%S"))
condition.release()
class Juez(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
global entrar, registrar
print ("comenzar juez\n")
condition.acquire()
mutex.acquire()
print("[%s]Juez:Entrar\n" % time.strftime("%H:%M:%S"))
juez = 1
print ("valor juezzzz: %d \n" % juez)
print("[%s]Juez: Juez: %d\n" % (time.strftime("%H:%M:%S"), juez))
if(entrar > registrar):
mutex.release()
print("[%s]Juez:Confirmando a inmigrante\n" % time.strftime("%H:%M:%S"))
confirmar.set()
entrar = registrar = 0
print("[%s]Juez: Entrar: %d Registrar: %d\n" % (time.strftime("%H:%M:%S"), entrar, registrar))
print("[%s]Juez:Salir\n" % time.strftime("%H:%M:%S"))
juez = 0
print("[%s]Juez: Juez: %d\n" % (time.strftime("%H:%M:%S"), juez))
mutex.release()
condition.release()
class Espectador(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
print ("comenzar espectador\n")
condition.acquire()
print("[%s]Espectador: Entrar\n" % time.strftime("%H:%M:%S"))
condition.release()
print("[%s]Espectador: Espectear\n" % time.strftime("%H:%M:%S"))
print("[%s]Espectador: Salir\n" % time.strftime("%H:%M:%S"))
inmigrante = Inmigrante()
judge = Juez()
espectador = Espectador()
inmigrante.start()
espectador.start()
judge.start()
inmigrante.join()
espectador.join()
judge.join()