首页 > 编程语言 > 详细

Programming Python - 2. System Tools -2.5 Parallel System Tools

时间:2014-03-24 05:46:27      阅读:487      评论:0      收藏:0      [点我收藏+]

Forking is based on the notion of copying programs: when a program calls the fork routine, the
operating system makes a new copy of that program and its process in memory and
starts running that copy in parallel with the original. Some systems don’t really copy
the original program (it’s an expensive operation), but the new copy works as if it were
a literal copy.

 

all forked processes run independently and in parallel under the operating system’s control, and
children may continue to run after their parent exits.

 

os.fork built-in function. Because this function
generates a copy of the calling program, it returns a different value in each copy: zero
in the child process and the process ID of the new child in the parent

bubuko.com,布布扣
import os
def child():
    print("hello from child", os.getpid())
    os.exit(0)

def parent():
    while True:
        newpid=os.fork()
        if newpid==0:
            child()
        else:
            print("hello from parent",os.getpid(), newid)

        if input()==q: break

parent()
bubuko.com,布布扣

 

*fork cannot work well in typical win; but can work under cygwin

Programming Python - 2. System Tools -2.5 Parallel System Tools,布布扣,bubuko.com

Programming Python - 2. System Tools -2.5 Parallel System Tools

原文:http://www.cnblogs.com/jsquare/p/3619930.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!