##################################################
#https://maths.nuigalway.ie/~gettrick/teach/cs211/
##################################################
import os, time, sys
def printStyleOne(str):
	sys.stdout.write("\033[1;31m")
	print (str)
	sys.stdout.write("\033[0;0m")
def printStyleTwo(str):
	print ("\033[;7m",str,"\033[0;0m")
print ("The child will write something to a pipe, ")
print ("and the parent will read and print it.")
print ("Processes executing fork now...")

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
    printStyleOne("parent: reading")
    text = r.read()
    printStyleOne("parent: received message from child: "+text)
    printStyleOne("parent: sleeping for 6 seconds, then waiting for child, then exiting")
    time.sleep(6)
    #os.wait()
    printStyleOne("parent: exiting")
else:       # we are the child
    r.close()
    txt="\033[;7m France v. Ireland, Stade de France, Saint-Denis, 20h10, 5/2/2026\033[0;0m"
    ("I'll pass that on in 7 seconds")
    time.sleep(7)
    printStyleTwo("child: writing....")
    w.write(txt)
    w.close()
    printStyleTwo("child: sleeping for 13 sec")
    time.sleep(13)
    printStyleTwo("child: exiting")

