forとipc


#!/usr/bin/python
# Filename:using_fork.py
 
import os, sys
 
print "I'm going to fork now"
 
r, w = os.pipe()
 
pid = os.fork()
 
if pid:
    # parent
    os.close(w)
    r = os.fdopen(r)
 
    print "parent: reading"
    txt = r.read()
    os.waitpid(pid, 0)
 
else:
    # child
    os.close(r)
    w = os.fdopen(w, 'w')
    print "child:writing"
    w.write("here's some text from the child")
    w.close()
    print "child:closing"
    sys.exit(0)
jobin@jobin-desktop:~/work/python/fork$ python using_fork.py
I'm going to fork now
parent: reading
child:writing
child:closing