Page David
Page David

Reputation: 1413

what will process created by fork() do in python?

I wrote the following code, but I don't understand how it works very well:

NUM=8
def timec():
    x=1000000
    while x>0:
        x-=1
pid_children=[]
start_time=time.time()
for i in range(NUM):
    pid=os.fork()
if pid==0:
    timec()
    os._exit(0)
else:
    pid_children.append(pid)

for j in pid_children:
    os.waitpid(j,0)
    print(time.time()-start_time)

I cannot understand where the child process starts or where it will finish.

And another question is will the waitpid() method wait for the child process to finish its work, or will it just return as soon as it is called?

Upvotes: 2

Views: 1039

Answers (1)

Russell Cohen
Russell Cohen

Reputation: 725

When os.fork() is called, the program splits into two completely separate programs. In the child, os.fork() returns 0. In the parent, os.fork() returns the process id of the child.

The key distinction about os.fork() is that it does not create a new thread that shares the memory of the original thread, but instead creates an entirely new process. The new process has a copy of the memory of it's parent. Updates in the parent are not reflected in the child and updates in the child are not reflected in the parent! The each have their own state.

Given that context, here are the answers to your specific questions:

Where do the child processes start?

pid = os.fork()

This will generate more than NUM processes because after the first iteration you will have 2 processes inside of the for loop, each of which will fork into 2 processes, yielding 4 total processes after the second iteration. In total 256 (2^8) processes will be created!

Where do the child processes end?

Some will exit at:

os._exit(0)

Others will exit at the end of the file. That's because you overwrote pid in the subsequent iterations of the loop, so some children became orphaned (and never ran timec()).

pid_children will always only have a single process in it. That's because the entire state of the program is forked, and each fork (which has it's own copy of the list) only adds one element to the list.

What does waitpid do?

os.waitpid(pid) will block until the process with pid pid has completed.

os.fork() documentation

os.waitpid() documentation

Upvotes: 2

Related Questions