Try the code below with: python fork.py
and with: python fork.py 1
to see what it does.
#!/usr/bin/env python2
import os
import sys
child_exit_status = 0
if len(sys.argv) > 1:
child_exit_status = int(sys.argv[1])
pid = os.fork()
if pid == 0:
print "This is the child"
if child_exit_status == 0:
os.execl('/usr/bin/whoami')
else:
os._exit(child_exit_status)
else:
print "This is the parent"
(child_pid, child_status) = os.wait()
print "Our child %s exited with status %s" % (child_pid, child_status)
Question: How come the child process can do 'print' and it still gets outputted to the same place as the parent process?
(Am using Python 2.6 on Ubuntu 10.04)