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

# Nacitaju sa moduly
import os
import time
import sys

if len(sys.argv)<=1:
	print "Spust ma s parametrom meno_adresara"
	sys.exit(1)

# Zavolame fork
pid_chld=os.fork()
# child dostane vratene 0, parent dostane PID child procesu
if pid_chld==0:
	# Spusti sa ls pre prvy argument
	# Prve /bin/ls je co sa ma spustit
	# Druhe /bin/ls je argv[0] pre ls - tradicne to je meno programu
	time.sleep(0.5)
	os.execlp("/bin/ls","/bin/ls",sys.argv[1])
	print "Sem sa nikdy tok riadenia nedostane"
elif pid_chld<0:
	print "fork vratil chybu"
	sys.exit(1)
else:
	print "PID child = %d" % pid_chld

# Oba procesy bezia sucasne, sem sa ale teraz dostane iba parent
for i in range(20):
	print "parent"
	time.sleep(0.1)
	
# 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)

