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被打印了两次,更加说明了子进程是复制父进程得来的
原文地址:https://www.cnblogs.com/saolv/p/9127437.html
时间: 2024-10-07 05:30:00