有人可以解释OS.Wait()中的错误原因



我在上自上而下的wait()函数的fork进程分别有此代码

import os
reply = int(input("Enter no of proc:   "))
pid = os.fork()
for i in range(reply):
    if pid == 0:
        pid = os.fork()
    else:
        os.wait()
        print(i,os.getpid(), os.getppid())

3的输出是:

Enter no of proc:   3
2 44070 44069
1 44069 44068
Traceback (most recent call last):
  File "/Users/saman/PycharmProjects/Syslab/test.py", line 12, in <module>
    os.wait()
ChildProcessError: [Errno 10] No child processes
Traceback (most recent call last):
  File "/Users/saman/PycharmProjects/Syslab/test.py", line 12, in <module>
    os.wait()
ChildProcessError: [Errno 10] No child processes
0 44068 20928

我不明白错误!

您正在儿童执行os.wait()。这些孩子没有等待的过程。

我想你想做这样的事情:

import os
reply = int(input("Enter no of proc:   "))
pid = os.fork()
for i in range(reply):
    if pid == 0:
        pid = os.fork()
if pid != 0:
    os.wait()
    print(os.getpid(), os.getppid())

最新更新