Reza
Reza

Reputation: 728

Can someone explain the cause of error in the os.wait()?

I have this piece of code for fork child process respectively in a top down with wait() function but when i ran the script i got an error.

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())

the output for 3 is:

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

I don't understand the error!

Upvotes: 0

Views: 1346

Answers (1)

Tom&#225;š Š&#237;ma
Tom&#225;š Š&#237;ma

Reputation: 864

You are executing os.wait() in children. These children has no process to wait on.

I think you want to do something like this:

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())

Upvotes: 3

Related Questions