I understand a parallel execution using fork. How does the fork work?

3
import os

def filho():
    print('Ola do filho', os.getpid())
    os._exit(0)

def pai():
    while True:
        newpid = os.fork()# fornece ID para um novo processo!
        if newpid == 0:
            filho()
        else:
            print('Ola do pai', os.getpid(), newpid)
        if input() == 'q':
            print("Fechando o pai", os.getpid())
            break

pai()

Make sense if newpid == 0 ? I do not understand what the code really does!

    
asked by anonymous 07.04.2018 / 18:08

2 answers

8

Note that other than threads , fork() creates a completely independent process. For the OS, there are two executables running in parallel, not two processes of the same executable.

The confusing part of fork() (and key to understanding that it is not a thread substitute) is that it duplicates the process at runtime , and in full .

It does not run a new start instance , it duplicates the process even at the point where it is being processed, with all values and variables intact.

Just because of this, I think the name "parent" and "child" are not suitable, because they are perfect clones.

In particular, in this section:

def pai():
    while True:
        newpid = os.fork()# fornece ID para um novo processo!
        #NESTE MOMENTO TEM 2 CÒDIGOS EM EXECUÇÃO NESTE MESMO PONTO

And then, each of the two running will fall from one side of the IF:

    if newpid == 0 # Zero significa que é a cópia
        filho()
    else:          # Diferente de zero é o original, cujo
                   # newpid é o ID do outro processo (o novo)

As we have the program duplicated and running at the same point, the only way to know which is which is by the different value returned in newpid in each case.

The best way to understand fork is to watch the movie "THE PRESTIGE", which in Brazil came as "THE GREAT TRICK":

  

Trailer: link

    
07.04.2018 / 18:13
2

From official documentation :

  

os.fork ()

     

Fork a child process. Return 0 in the child and the child's process id in the parent. If an error occurs OSError is raised.

When os.fork is called, the state of the program is doubled in memory. Now you have two processes running, and they need to know who the "parent" process is and who the "child" process is. For this, the function returns 0 for the child process, and another number for the parent process.

Note that os.fork is Unix-only. A more pythonical and multiplatform way of managing child / parent processes is to use the built-in module multiprocessing and if __name__ == '__main__' , which will only be true in the original process.

    
07.04.2018 / 18:13