分两份拷贝文件,父进程拷贝文件的前半部分,子进程拷贝文件的后半部分。
import os import time #获取文件大小 size = os.path.getsize('wait.py') # f = open('wait.py') pid = os.fork() if pid < 0: print('create process failed') elif pid == 0: n = size // 2 fw = open('child','w') with open('wait.py') as f: while True: if n < 64: data = f.read(n) fw.write(data) break data = f.read(64) fw.write(data) n -= 64 fw.close() else: fw = open('parent','w') with open('wait.py') as f: f.seek(size // 2,0) while True: data = f.read(64) if not data: break fw.write(data) fw.close()