トップ «前の日記 最新 次の日記»

2022-03-03 (Th) [長年日記]

_ python で deadlock!

#!/usr/bin/env python

import sys
import os
import threading
import time

mutex = threading.Lock()

def work1():
    while True:
        time.sleep(1)
        pid = os.fork()
        mutex.acquire()
        mutex.release()
        if pid == 0:
            sys.exit(0)

def work2():
    while True:
        mutex.acquire()
        time.sleep(0.01)
        mutex.release()

thread = threading.Thread(target=work2)
thread.start()

work1()

実行すると、プロセスが増えていく…

deadlock 起きてんじゃん!


編集 パスワード変更