首页 > 技术文章 > 创建新的进程fork()

-hjj 2018-11-09 18:39 原文





#os 模块提供大量和系统相关的功能函数接口
#os模块的使用是系统相关的 在不同的系统中
#可能使用方法不同 
import os 

print('before create process')
a = 10

#创建新的进程
pid = os.fork()

if pid < 0:
    print('create process failed')
elif pid == 0:
    print('This is the new process')
    print(a)
    a = 1000
else:
    print('This is the parent process')

print("The process end")
print(a)

#结果打印了两遍“The process end”,第一遍是父进程打印的,第二遍是子进程打印的,创建进程运行时,父进程运行一遍后,子进程会为父进程开辟一个内存空间再执行一遍相同的内容
#fork()函数通过系统调用创建一个与原来进程几乎完全相同的进程



推荐阅读