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?
– Ed S
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.
– Bacco
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).
– Bacco
In fact, a program only knows natively that it is Fork testing the PID itself, otherwise it would not even be able to differentiate.
– Bacco
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).
– Bacco
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?
– Ed S
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.
– Bacco
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!
– Ed S
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..
– Bacco
For example, what I’d like to do is: how to create the same effect using Threads instead of Fork?
– Ed S
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.
– Bacco
You cannot create the same effect. It has nothing to do with thread
– Bacco