База ответов ИНТУИТ

Язык программирования Python

<<- Назад к вопросам

Что делает следующая программа?
import threadingl = threading.RLock()def proc(nm, n=0):  l.acquire()  try:    if n < 5:      print "*",      return proc(nm, n+1)    else:      return nm  finally:    l.release() for i in range(5):  threading.Thread(target=proc, args=(str(i),)).start()

(Отметьте один правильный вариант ответа.)

Варианты ответа
аварийно завершается при попытке выполнить l.acquire() во второй раз
печатает 1 звездочку и зависает
печатает 5 звездочек
печатает 25 звездочек(Верный ответ)
беспрерывно печатает звездочки
Похожие вопросы
Что делает следующая программа?
import threadingl = threading.Lock()def proc(nm, n=0):  l.acquire()  try:    if n < 5:      print "*",      return proc(nm, n+1)    else:      return nm  finally:    l.release() for i in range(5):  threading.Thread(target=proc, args=(str(i),)).start()
Что делает следующая программа?
import threadingready = threading.Event()def proc():  ready.wait()  # ...  print "Done!"for i in range(5):  p = threading.Thread(target=proc)  p.start()print "Prepare!"ready.set()
В каких частях программы допущены ошибки в следующем примере?
import threading# 1def proc(*args):  print "Процесс в потоке пошел!"  while 1:    pass# 2p1 = threading.Thread(target=proc(), name="t1", args=[2])# 3p1.start()
Какая ошибка допущена в следующем примере?
import threadingglobal to_evalcond = threading.Condition()def evaluate_something(x):  return 2**int(x)def evaluator(name):  global to_eval  while True:    cond.acquire()    while not to_eval:      cond.wait()    v = to_eval.pop()    cond.release()    print name, ":", evaluate_something(v)to_eval = []for n in range(3):  ev = threading.Thread(target=evaluator, args=(str(n),))  ev.setDaemon(1)  ev.start()while 1:  inp = raw_input('Вводите: ')  cond.acquire()  to_eval.append(inp)  cond.notifyAll()  cond.release()
Что делает следующая программа?
import threading, Queueitem = Queue.Queue()def consumer(nm):  while True:    print item.get(), nmdef producer(nm):  while True:    item.put(nm)for n in range(3):  threading.Thread(target=consumer, args=("c"+str(n),)).start()  threading.Thread(target=producer, args=("p"+str(n),)).start()
Какая ошибка допущена в следующем примере?
import threadingglobal to_evalcond = threading.Condition()def evaluate_something(x):  return 2**int(x)def evaluator(name):  global to_eval  while True:    cond.acquire()    while not to_eval:      cond.wait()    v = to_eval.pop()    cond.release()    print name, ":", evaluate_something(v)to_eval = []for n in range(3):  ev = threading.Thread(target=evaluator, args=(str(n),))  ev.setDaemon(1)  ev.start()while 1:  inp = raw_input('Вводите: ')  to_eval.append(inp)  cond.notifyAll()
Может ли возникнуть deadlock в следующей программе:
import threadingres_A = threading.Lock()res_B = threading.Lock()res_C = threading.Lock()def proc1():   res_A.acquire(); res_B.acquire(); res_C.acquire()   # ...   res_B.release(); res_C.release(); res_A.release()def proc2():   res_A.acquire(); res_B.acquire(); res_C.acquire()   # ...   res_C.release(); res_B.release(); res_A.release()def proc3():   res_A.acquire(); res_B.acquire(); res_C.acquire()   # ...   res_A.release(); res_B.release(); res_C.release()p1 = threading.Thread(target=proc1, name="t1")p2 = threading.Thread(target=proc2, name="t2")p3 = threading.Thread(target=proc3, name="t3")p1.start(); p2.start(); p3.start()p1.join(); p2.join(); p3.join();
Может ли возникнуть deadlock в следующей программе:
import threadingres_A = threading.Lock()res_B = threading.Lock()res_C = threading.Lock()def proc1():   res_A.acquire(); res_B.acquire(); res_C.acquire()   # ...   res_B.release(); res_C.release(); res_A.release()def proc2():   res_B.acquire(); res_C.acquire(); res_A.acquire()   # ...   res_C.release(); res_B.release(); res_A.release()def proc3():   res_C.acquire(); res_A.acquire(); res_B.acquire()   # ...   res_A.release(); res_B.release(); res_C.release()p1 = threading.Thread(target=proc1, name="t1")p2 = threading.Thread(target=proc2, name="t2")p3 = threading.Thread(target=proc3, name="t3")p1.start(); p2.start(); p3.start()p1.join(); p2.join(); p3.join();
Какие ошибки допущены в следующем примере?
import threading, Queueitem = Queue.Queue()def consumer(nm):  for i in range(3):    print item.get(), nmdef producer(nm):  for i in range(4):    item.put(nm)for n in range(4):  threading.Thread(target=consumer, args=("c"+str(n),)).start()for n in range(3):  threading.Thread(target=producer, args=("p"+str(n),)).start()
Может ли возникнуть deadlock в следующей программе:
import threadingres_A = threading.Lock()res_B = threading.Lock()def proc1():   res_A.acquire()   res_B.acquire()   # ...   res_B.release()   res_A.release()def proc2():   res_B.acquire()   res_A.acquire()   # ...   res_B.release()   res_A.release()p1 = threading.Thread(target=proc1, name="t1")p2 = threading.Thread(target=proc2, name="t2")p1.start()p2.start()p1.join()