I understand a parallel run using Fork. How does Fork work?

Asked

Viewed 1,322 times

2

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

Makes sense if newpid == 0 ? I don’t understand what the code really does!

2 answers

8


Note that different from threads, the fork() creates a completely independent process. For OS, they become two executables running in parallel, not two processes of the same executable.

The part that confuses the fork() (and fundamental to understand that it is not a substitute for thread) is that it duplicates the process during the run, and in full.

Him does not execute startup instance, it duplicates the process even at the point where it is being processed, with all values and variables intact.

Just for that, I think the name "Parent" and "Child" are not suitable, as 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 soon after, each of the two running will fall on 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)

Since 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 of the cases.

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

Trailer: https://www.youtube.com/watch?v=I-zSIAgJszQ

  • If possible, it could show the same process above but using Threads to compare?

  • I just edited explaining that thread has nothing to do with Fork. There is nothing to compare between the two, thread is from the same program, Fork is a separate program. The same, with the same values, but totally independent.

  • An example: there is no way that one of the instances of Fork can directly access one variable or function from the other. They are 2 separate programs. Thread is the same program, only happens to be running a certain function in "parallel" (actually interlacing the processes).

  • In fact, a program only knows natively that it is Fork testing the PID itself, otherwise it would not even be able to differentiate.

  • The only thing "similar" between the two is that 2 instances "forkadas" do things in parallel, and 2 threads also, but this is almost a "coincidence". If one day someone relates the two, it’s more creating confusion than understanding (or worse, if the person is teaching a Rk class and wants to compare it to a thread during class, it’s getting in the way of the student - thread is thread, Fork is Fork).

  • So the object of the code I posted is to perform the child and father functions in parallel, right? Why not call child() right after newpid?

  • How do you know it’s the son? There are 2 programs running in that stretch. One is the original (wrongly called the Father), the other is Fork (wrongly called the son). That’s the key. Understand that where I put my note in comment in the first excerpt, there are already 2 programs, no longer one.

  • I’ll study some more. It’s still confusing for me. I thought that by calling Fork(), I could already perform the other function in parallel!

  • Think of Fork like this: if you were hit by a "duplicating ray," how would each of you know what the original is? The two would be the same person, only they exist in parallel..

  • For example, what I’d like to do is: how to create the same effect using Threads instead of Fork?

  • A thread is a new arm that is born in your body. A Fork is you duplicated, adult, with all your memories and experience in the 2 bodies.

  • 2

    You cannot create the same effect. It has nothing to do with thread

Show 7 more comments

2

Of official documentation:

the.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 the os.fork is called, the state of the program is duplicated in memory. Now you have two processes running, and they need to know who is the "father" process and who is the "son" process. For this, the function returns 0 for the child process, and another number for the parent process.

It is worth remembering that os.fork is unique to Unix. A more pythonic and cross-platform way of managing child/parent processes is to use the built-in module multiprocessing and if __name__ == '__main__', that will only be true in the original process.

  • If possible, it could show the same process above but using Threads to compare?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.