zoukankan      html  css  js  c++  java
  • python中fork()函数生成子进程分析-乾颐堂

       python的os module中有fork()函数用于生成子进程,生成的子进程是父进程的镜像,但是它们有各自的地址空间,子进程复制一份父进程内存给自己,两个进程之 间的执行是相互独立的,其执行顺序可以是不确定的、随机的、不可预测的,这点与多线程的执行顺序相似。  

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    import os
    def child():
        print 'A new child:', os.getpid()
        print 'Parent id is:', os.getppid()
        os._exit(0)
    def parent():
        while True:
            newpid=os.fork()
            print newpid
            if newpid==0:
                child()
            else:
                pids=(os.getpid(),newpid)
                print "parent:%d,child:%d"%pids
                print "parent parent:",os.getppid()       
            if raw_input()=='q':
                break
    parent()

        在我们加载了os模块之后,我们parent函数中fork()函数生成了一个子进程,返回值newpid有两个,一个为0,用以表示子进程,一个是大于 0的整数,用以表示父进程,这个常数正是子进程的pid. 通过print语句我们可以清晰看到两个返回值。如果fork()返回值是一个负值,则表明子进程生成不成功(这个简单程序中没有考虑这种情况)。如果 newpid==0,则表明我们进入到了子进程,也就是child()函数中,在子进程中我们输出了自己的id和父进程的id。如果进入了else语句, 则表明newpid>0,我们进入到父进程中,在父进程中os.getpid()得到自己的id,fork()返回值newpid表示了子进程的id,同时我们输出了父进程的父进程的id. 通过实验我们可以看到if和else语句的执行顺序是不确定的,子、父进程的执行顺序由操作系统的调度算法来决定。

    www.qytang.com/
    http://www.qytang.com/cn/list/29/
    http://www.qytang.com/cn/list/28/610.htm
    http://www.qytang.com/cn/list/28/595.htm
    http://www.qytang.com/cn/list/28/583.htm
    http://www.qytang.com/cn/list/28/582.htm
    http://www.qytang.com/cn/list/28/576.htm
    http://www.qytang.com/cn/list/28/523.htm
    http://www.qytang.com/cn/list/28/499.htm
    http://www.qytang.com/cn/list/28/488.htm
    http://www.qytang.com/cn/list/28/466.htm
    http://www.qytang.com/cn/list/28/463.htm
    http://www.qytang.com/cn/list/28/458.htm
    http://www.qytang.com/cn/list/28/455.htm
    http://www.qytang.com/cn/list/28/447.htm

  • 相关阅读:
    visual studio 2008 在调试的时候出现无法启动程序错误时什么原因
    android illegallstateexception:get field slot from row 0 col 1 failed
    android activity has leaked window phonewindow
    the content of the adapter has changed but listview did not
    android create table android_metadata failed
    豌豆荚 软件 android 550 that path is inaccessible sdcard
    android nullpointerexception println needs a message
    android sqlite3 乱码
    android unable to instantiate activity componentinfo
    android.database.sqlite.SQLiteExcepption: near ">":syntax error:,while compiling:
  • 原文地址:https://www.cnblogs.com/qytang/p/5580860.html
Copyright © 2011-2022 走看看