#!/usr/bin/python
# -*- coding: utf-8 -*-

# Nacitaju sa moduly
import os
import time
import sys

# Zavolame fork
pid_chld=os.fork()
# child dostane vratene 0, parent dostane PID child procesu
if pid_chld==0:
	string="child"
elif pid_chld<0:
	print "fork vratil chybu"
else:
	print "PID child = %d" % pid_chld
	string="parent"

# Oba procesy bezia sucasne
for i in range(20):
	print string
	time.sleep(0.1)
	
if pid_chld==0:
# child vrati cislo 101
	print "child konci"
	sys.exit(101)
else:
# parent si preberie navratovu hodnotu od jadra
	ret_wait=os.wait()
# os.wait vracia v Pythone dvojicu hodnot
	pid_chld_wait=ret_wait[0] # pid child procesu
	ret_chld=ret_wait[1] >> 8 # a nahratovu hodnotu (shiftnutu)
	print "child %d vratil %d" % (pid_chld_wait,ret_chld)
