I'm learning multiprocessing in Python and I'm having some trouble visualizing os.fork() in my mind. I get the general cases that are thrown around the website, but I'm really having a hard time with this one.
Code is as follows:
import os
for i in range(2):
pid1 = os.fork()
pid2 = os.fork()
if (pid1>0):
print("A")
if (pid2==0):
print("B")
What I know is that 10 As and 10 Bs are printed. I've messed around with print() to find out what the process tree would look like, and I found that it matches my original idea, something like this:
This diagram represents behaviour of the code, where "type" just means if they came from either a "pid1" or a "pid2" line. Green circles represent the first iteration of the cycle, and blue cirles represent the second. Using this diagram I can account for the 10 Bs printed, given that there are 10 processes of type 2 and each one of those prints B. Where I'm having trouble is in understanding which ones print A. Thanks in advance.