Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

May someone explain the following os.fork() example to me?

[Code taken from Programming Python 4th Edition by Mark Lutz]

"forks child processes until you type 'q'"
import os
def child():
    print('Hello from child', os.getpid())
    os._exit(0) # else goes back to parent loop

def parent():
    while True:
        newpid = os.fork()
        if newpid == 0:
            child()
        else:
            print('Hello from parent', os.getpid(), newpid)
        if input() == 'q': break

parent()

What the code outputs when ran:

Hello from parent 2057 2062 
Hello from child 2062

Hello from parent 2057 2068 
Hello from child 2068

Hello from parent 2057 2069 
Hello from child 2069

Hello from parent 2057 2070 
Hello from child 2070 
q

Things I understand:

  1. os.fork() is used to start another process in parallel to the current one.
  2. os.fork() creates a copy of the previous Python session and opens it in parallel.
  3. os.fork() returns the id of the new process.

Things I don't understand:

  1. How come the value of os.getpid() is never changed when the code runs?
  2. Why is the child() function ever called? Let's say that the value of newpid != 0, then the program will print out print('Hello from parent', os.getpid(), newpid). However, after that, it prints the line from child rather than asking for an input as the case is after the if statement.
  3. What is os._exit(0) doing?

Thanks a ton for your time. :)

like image 734
Always Learning Forever Avatar asked Jun 05 '14 05:06

Always Learning Forever


People also ask

What does OS fork () do?

fork() method in Python is used to create a child process. This method work by calling the underlying OS function fork(). This method returns 0 in the child process and child's process id in the parent process.

Is the OS fork () in python a system call?

fork is Unix system call which is used to create a new process.

What happens when the OS fork () function is executed?

Overview. This os. fork() method in Python provides the implementation of the fork() system, which creates a child or a forked process. Calling fork() results in the creation of an exact copy of the parent process.


2 Answers

1: How come the value of os.getpid() is never changed when the code runs?

The value of os.getpid() will never change for the parent since that's always the same process. The pid will change every time for the child since fork() always creates a brand new child process clone with its own PID.

2: Why is the child() function ever called? Let's say that the value of newpid != 0, then the program will print out print('Hello from parent', os.getpid(), newpid). However, after that, it prints the line from child rather than asking for an input as the case is after the if statement.

The child process is called because there are now two processes running. One called the child() function, the other called the print function. They are simply fighting for printing to the screen, and you saw the parent print "win" first in this case.

3: What is os._exit(0) doing?

See here: https://docs.python.org/2/library/os.html#os._exit

Exit the process with status n, without calling cleanup handlers, flushing stdio buffers, etc.

like image 190
14 revs, 12 users 16% Avatar answered Sep 21 '22 22:09

14 revs, 12 users 16%


  1. Because your parent process has always the same pid as long as it runs (while True:).
  2. The child is actually a copy of the parent. That's what os.fork does! And the function returns the pid of the child to the parent. So the parent runs the second part of the if else-statement, while all childs run the first part, because os.fork for child processes gives a 0 back.
  3. os._exit is essentially a stripped down version of os.exit(), and is meant for child processes.
like image 33
Don Question Avatar answered Sep 21 '22 22:09

Don Question