关于 Python 中的 os.fork() 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24538466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:46:12  来源:igfitidea点击:

Regarding The os.fork() Function In Python

pythonfork

提问by user3490561

I'm just beginning with python and I developed a simple program to fork a parent process. Here's the code I've written so far...

我刚开始使用 python,我开发了一个简单的程序来分叉父进程。这是我到目前为止编写的代码......

#!/usr/bin/env python
import os

def child():
    print "We are in the child process with PID= %d"%os.getpid()

def parent():
    print "We are in the parent process with PID= %d"%os.getpid()
    newRef=os.fork()
    if newRef==0:
        child()
    else:
        print "We are in the parent process and our child process has PID= %d"%newRef

parent()

From what I understand, the code should begin by calling the parent process and display its PID. Afterwards, the os.fork()is called and it creates a copy of the parent process, and since we are already in the parent process, the newRefvariable should contain a value that is positive and the elsepart of my code is the one that should be executed. My question is that: why did the code initiate to call the child()function afterwards although the ifpart of my code should not execute.

据我了解,代码应该首先调用父进程并显示其 PID。之后,os.fork()调用 并创建父进程的副本,因为我们已经在父进程中,newRef变量应该包含一个正值,else我的代码部分是应该执行的部分。我的问题是:child()尽管if我的部分代码不应该执行,但为什么代码会在之后启动调用该函数。

Thanks in advance :)

提前致谢 :)

采纳答案by Chris Barker

After you return from fork, you now have two processesexecuting the code immediately following fork.

从 返回后fork,您现在有两个进程执行紧随其后的代码fork

So your statement:

所以你的声明:

since we are already in the parent process

因为我们已经在父进程中

is only half-true. After os.forkreturns, the parent process continues executing the code as the parent process, but the child process continues executing the exact same codewith the same local and global variables, with the exception of the return value of fork. So, from the child process's perspective, newRefhas the value of 0, and from the parent's perspective, newRefhas a positive value. Both processes will execute the code accordingly.

只对了一半。后os.fork返回时,父进程继续执行代码的父进程,而子进程继续执行完全相同的代码具有相同本地和全局变量,用的返回值的例外fork。因此,从子进程的角度来看,newRef具有 的值0,而从父进程的角度来看,newRef具有正值。两个进程都会相应地执行代码。

回答by murgatroid99

When you call os.fork, you create a new process that is an exact copy of the existing process exceptthat in the original process, forkreturns the process ID of the new (child) process, and in the new process, forkreturns 0. This difference is how you can do something different in the parent and in the child.

当您调用 时os.fork,您将创建一个新进程,它是现有进程的精确副本,除了在原始进程中,fork返回新(子)进程的进程 ID,而在新进程中,fork返回0. 这种差异是您如何在父母和孩子身上做不同的事情。

In your specific code, the return value of forkin the child is 0, so the child process calls the childfunction. In the parent, the return value is not 0, so the else clause is executed.

在特定的代码,返回值fork的孩子0,所以子进程调用的child函数。在父级中,返回值不是0,因此执行 else 子句。