Attachment 'fork_exec.py'

Download

   1 #!/usr/bin/python
   2 # -*- coding: utf-8 -*-
   3 
   4 # Nacitaju sa moduly
   5 import os
   6 import time
   7 import sys
   8 
   9 if len(sys.argv)<=1:
  10         print "Spust ma s parametrom meno_adresara"
  11         sys.exit(1)
  12 
  13 # Zavolame fork
  14 pid_chld=os.fork()
  15 # child dostane vratene 0, parent dostane PID child procesu
  16 if pid_chld==0:
  17         # Spusti sa ls pre prvy argument
  18         # Prve /bin/ls je co sa ma spustit
  19         # Druhe /bin/ls je argv[0] pre ls - tradicne to je meno programu
  20         time.sleep(0.5)
  21         os.execlp("/bin/ls","/bin/ls",sys.argv[1])
  22         print "Sem sa nikdy tok riadenia nedostane"
  23 elif pid_chld<0:
  24         print "fork vratil chybu"
  25         sys.exit(1)
  26 else:
  27         print "PID child = %d" % pid_chld
  28 
  29 # Oba procesy bezia sucasne, sem sa ale teraz dostane iba parent
  30 for i in range(20):
  31         print "parent"
  32         time.sleep(0.1)
  33 
  34 # parent si preberie navratovu hodnotu od jadra
  35 ret_wait=os.wait()
  36 # os.wait vracia v Pythone dvojicu hodnot
  37 pid_chld_wait=ret_wait[0] # pid child procesu
  38 ret_chld=ret_wait[1] >> 8 # a nahratovu hodnotu (shiftnutu)
  39 print "child %d vratil %d" % (pid_chld_wait,ret_chld)

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.

You are not allowed to attach a file to this page.