以python代碼解釋fork系統調用
阿新 • • 發佈:2018-06-03
ppi .get 復制 AC ... () 系統 pri pan
import os print(‘Process (%s) start...‘ % os.getpid()) # Only works on Unix/Linux/Mac: pid = os.fork() print ("haha") if pid == 0: print(‘I am child process (%s) and my parent is %s.‘ % (os.getpid(), os.getppid())) else: print(‘I (%s) just created a child process (%s).‘ % (os.getpid(), pid))""" haha haha Process (876) start... I (876) just created a child process (877). I am child process (877) and my parent is 876. """
執行到os.fork()時,發起fork系統調用,操作系統復制父進程得到子進程。fork系統調用完畢,返回給父進程子進程的id號,返回給子進程0,然後父子進城開始執行。我們可以看到haha被打印了兩次,更加說明了子進程是復制父進程得來的
以python代碼解釋fork系統調用