import os, time, sys
def myConeP(str):
	sys.stdout.write("\033[1;31m")
	print (str)
	sys.stdout.write("\033[0;0m")
def myCtwoP(str):
	print ("\033[;7m",str,"\033[0;0m")
print ("I'm going to fork now.")
print ("The child will write something to a pipe,")
print ("and the parent will read it back.")

r, w = os.pipe() # these are file descriptors, not file objects
r = os.fdopen(r, 'r') # turn r into a file object
w = os.fdopen(w, 'w')

id = os.fork()

if id:     # we are the parent
    w.close() # use os.close() to close a file descriptor
    myConeP("parent: reading")
    text = r.read()
    myConeP("parent received message from child: "+text)
    myConeP("parent sleeping for 4 seconds, waiting for child, then exiting")
    time.sleep(4)
    os.wait()
    myConeP("parent exiting")
else:       # we are the child
    r.close()
    txt="\033[;7m What message would you like to pass on?\033[0;0m"
    myCtwoP("I'll pass that on in 7 seconds")
    time.sleep(7)
    myCtwoP("passing it on now....")
    w.write(txt)
    w.close()
    myCtwoP("child: closing")

