gpt4 book ai didi

Python:os.fork() 是如何工作的?

转载 作者:IT老高 更新时间:2023-10-28 20:56:50 27 4
gpt4 key购买 nike

我正在学习 python 中的多处理。我尝试了多处理,在阅读了多处理模块的源代码后,我发现它使用os.fork(),所以我写了一些代码来测试os.fork() ,但我被卡住了。我的代码如下:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import time

for i in range(2):
print '**********%d***********' % i
pid = os.fork()
print "Pid %d" % pid

我认为每次打印都会执行两次,但它们会执行三次。我无法理解这是如何工作的?我读了Need to know how fork works?
从这篇文章说它也将被执行两次,所以我被卡住了......

最佳答案

首先,删除 print '******...' 行。它只是让每个人都感到困惑。相反,让我们试试这段代码......

import os
import time

for i in range(2):
print("I'm about to be a dad!")
time.sleep(5)
pid = os.fork()
if pid == 0:
print("I'm {}, a newborn that knows to write to the terminal!".format(os.getpid()))
else:
print("I'm the dad of {}, and he knows to use the terminal!".format(pid))
os.waitpid(pid, 0)

好的,首先,什么是“fork”? Fork 是现代且符合标准的操作系统(M$ Windows 除外:操作系统的笑话几乎是现代且符合标准)的一项功能,它允许进程(又名:“程序” ,其中包括 Python 解释器!)从字面上完全复制自身,有效地创建一个新进程(“程序”的另一个实例)。一旦魔术完成,两个过程都是独立的。更改其中一个中的任何内容不会影响另一个。

负责拼出这个黑暗而古老的咒语的过程被称为父过程。这种对生命本身的不道德憎恶的无灵魂结果被称为子进程。

正如所有人都知道的那样,包括那些并非如此的人,你可以成为那些通过 os.fork() 出卖自己灵魂的精选程序员群体中的一员.该函数执行一个 fork 操作,从而导致第二个进程凭空创建。

现在,这个函数返回什么,或者更重要的是,如何返回?如果你不想发疯,不要去阅读 Linux 内核的 /kernel/fork.c 文件!一旦内核做了我们知道它必须做的事情,但我们不想接受它,os.fork()两个 进程中返回!是的,连调用栈都被复制了!

那么,如果它们是精确的副本,那么如何区分 parent 和 child 呢?简单的。如果 os.fork() 的结果为零,那么您正在 child 中工作。否则,您在父级中工作,返回值是子级的 PID(进程标识符)。反正 child 可以从os.getpid()得到自己的PID,不是吗?

现在,考虑到这一点,并且在循环中执行 fork() 会导致困惑,这就是发生的情况。我们将原始进程称为“主”进程...

  • Master:i = 0, fork 成 child-#1-of-master
    • Child-#1-of-master:i = 1 fork 成 child-#1-of-child-#1-of-master
    • Child-#1-of-child-#1-of-master:for 循环结束,退出
    • Child-#1-of-master:for 循环结束,退出
  • Master:i = 1, fork 成 child-#2-of-master
    • Child-#2-of-master:i = 1 fork 成 child-#1-of-child-#2-of-master
    • Child-#1-of-child-#2-of-master:for 循环结束,退出
    • Child-#2-of-master:for 循环结束,退出
  • Master:for循环结束,退出

如您所见,共有 6 个父/子打印来自 4 个独特的进程,产生 6 行输出,类似于...

I'm the dad of 12120, and he knows to use the terminal!

I'm 12120, a newborn that knows to write to the terminal!

I'm the dad of 12121, and he knows to use the terminal!

I'm 12121, a newborn that knows to write to the terminal!

I'm the dad of 12122, and he knows to use the terminal!

I'm 12122, a newborn that knows to write to the terminal!

但这只是任意的,它可以输出这个代替......

I'm 12120, a newborn that knows to write to the terminal!

I'm the dad of 12120, and he knows to use the terminal!

I'm 12121, a newborn that knows to write to the terminal!

I'm the dad of 12121, and he knows to use the terminal!

I'm 12122, a newborn that knows to write to the terminal!

I'm the dad of 12122, and he knows to use the terminal!

或者除此之外的任何东西。操作系统(和你主板的时髦时钟)完全负责进程获取时间片的顺序,所以去 blame on Torvalds (and expect no self-steem when back)如果你不喜欢内核如何管理你的进程;)。

我希望这对你有所启发!

关于Python:os.fork() 是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33560802/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com