- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
[代码取自 Mark Lutz 编写的 Programming Python 4th Edition]
"forks child processes until you type 'q'"
import os
def child():
print('Hello from child', os.getpid())
os._exit(0) # else goes back to parent loop
def parent():
while True:
newpid = os.fork()
if newpid == 0:
child()
else:
print('Hello from parent', os.getpid(), newpid)
if input() == 'q': break
parent()
运行时代码输出的内容:
Hello from parent 2057 2062
Hello from child 2062
Hello from parent 2057 2068
Hello from child 2068
Hello from parent 2057 2069
Hello from child 2069
Hello from parent 2057 2070
Hello from child 2070
q
我的理解:
os.fork()
用于启动与当前进程并行的另一个进程。os.fork()
创建先前 Python session 的副本并并行打开它。os.fork()
返回新进程的 ID。我不明白的地方:
os.getpid()
的值在代码运行时从未改变?child()
函数?假设 newpid
的值 != 0,那么程序将打印出 print('Hello from parent', os.getpid(), newpid)
。但是,在那之后,它会打印来自 child 的行,而不是要求输入,因为这种情况是在 if 语句之后。os._exit(0)
在做什么?非常感谢您的宝贵时间。 :)
最佳答案
1: How come the value of os.getpid() is never changed when the code runs?
os.getpid()
的值对于父进程永远不会改变,因为这始终是相同的进程。由于 fork()
总是使用自己的 PID 创建全新的子进程克隆,因此每次子进程的 pid 都会更改。
2: Why is the child() function ever called? Let's say that the value of newpid != 0, then the program will print out print('Hello from parent', os.getpid(), newpid). However, after that, it prints the line from child rather than asking for an input as the case is after the if statement.
调用子进程是因为现在有两个进程在运行。一个调用 child()
函数,另一个调用 print
函数。他们只是在为打印到屏幕而战,在这种情况下,您看到父级首先打印“win”。
3: What is os._exit(0) doing?
参见此处:https://docs.python.org/2/library/os.html#os._exit
Exit the process with status n, without calling cleanup handlers, flushing stdio buffers, etc.
关于python - 有人可以向我解释以下 os.fork() 示例吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24052217/
有3个 repo : 有 OpenAI Baselines 存储库:https://github.com/openai/baselines . 我有它的 fork :https://github.co
我试图了解在调用 fork() 后复制文件描述符的含义及其对争用的可能影响。 在“Linux 编程接口(interface)”24.2.1 (p517) 中: When a fork() is per
我对 systemd 如何跟踪主进程存在后仍然存在的主进程的子进程感兴趣? 最佳答案 Systemd 为此使用了 cgroups。在启动任何使用服务文件定义的可执行文件之前,systemd 会创建一个
这是输出--- 家长:我的pid是4525 parent :我 parent 的 pid 是 3350 parent 开始- 4525 3350 fork 前 fork 前 child 4526 45
我之前 fork 了 jockm/vert.x 并向他发送了拉取请求。现在我想 fork vert-x/vert.x (jockm/vert.x 的上游)并向他们发送不同的拉取请求。但是,当我单击“F
我想控制从 Perl 脚本派生的进程的名称 a。理想情况下它会像这样: ./forker.pl | ... | | fork("forked.pl"); |\ | \ | `--------\ |
我知道 fork() 在更高级别上做什么。我想知道的是这个—— 一旦有 fork 调用,trap 指令就会跟随并且控制跳转以执行 fork “处理程序”。现在,这个创建子进程的处理程序如何通过创建另一
我正在研究操作系统测验,但我不知道输出什么 if(fork()) fork() 会产生。有人可以解释吗? 我不明白这一行: if(fork()) 编辑: 我所说的“输出”是指如果执行此代码,将
这个问题在这里已经有了答案: Why does this program print "forked!" 4 times? (6 个答案) 关闭 3 年前。 在 C 中,fork() 函数将为父进程
有什么方法可以区分程序中不同 fork() 函数创建的子进程。 global variable i; SIGCHLD handler function() { i--; } handle() {
我正在重新开发一个系统,该系统将通过 http 向多个供应商之一发送消息。原来是perl脚本,重新开发很可能也会用perl。 在旧系统中,同时运行多个 perl 脚本,每个供应商运行 5 个。当一条消
Git 的新手,仍然有点困惑。我在 github 上 fork 了一个项目,想将项目所有者最近对原始项目所做的一些更改引入/merge 到我的 fork 中。这可能吗?该项目是只读的,但基本上,我想让
根据维基百科(可能是错误的) When a fork() system call is issued, a copy of all the pages corresponding to the par
我需要帮助了解如何在 Go 中妖魔化进程。 package main import ( "fmt" "os" ) func start() { var procAttr os.Pro
我已经执行了这段代码。我知道消息的顺序是任意顺序的(因为我明确没有使用信号量)我的程序流程如何?为什么? 父级被执行,因此“baz”被打印一次。有人可以解释为什么不打印“bar”吗?为什么我得到“fo
这个问题已经有答案了: Why does this program print "forked!" 4 times? (6 个回答) 已关闭 5 年前。 我对 fork 进程有疑问。我有一个代码是 i
我在弄清楚如何使用在不同进程之间创建的列表时遇到了麻烦。我所拥有的是: FileList.h - 我创建的列表 #include "Node.h" typedef struct FileList {
好吧,所以我一直在 stackoverflow 上查找这个问题,并且肯定在谷歌上搜索了半个小时,但我得到的答案似乎与我真正想做的事情几乎没有任何关系,希望有人能提供帮助我在这方面,代码如下: int
我正在尝试了解 fork-join 的工作原理。 维基百科有以下合并排序示例,其中左半部分被 fork ,右半部分由当前线程处理。 mergesort(A, lo, hi): if lo t
下面的代码输出了一系列的过程,它到底做了什么: _PARENT_ / \ /
我是一名优秀的程序员,十分优秀!