import os
def child():
print('A new child ', os.getpid())
os._exit(0)
def parent():
while True:
newpid = os.fork()
if newpid == 0:
child()
else:
pids = (os.getpid(), newpid)
print("parent: %d, child: %d" % pids)
if input( ) == 'q':
break
parent()
import os
def child():
print('A new child ', os.getpid())
os._exit(0)
def parent():
while True:
newpid = os.fork()
if newpid == 0:
child()
else:
pids = (os.getpid(), newpid)
print("parent: %d, child: %d" % pids)
if input( ) == 'q':
break
parent()
Parent process continues after the fork() with the if statement.
newpid is unequal to 0
Child process starts with the
if statement.
newpid is equal 0
exit() is necessary, because the child process won't end without it