Attachment 'fork.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 # Zavolame fork
  10 pid_chld=os.fork()
  11 # child dostane vratene 0, parent dostane PID child procesu
  12 if pid_chld==0:
  13         string="child"
  14 elif pid_chld<0:
  15         print "fork vratil chybu"
  16 else:
  17         print "PID child = %d" % pid_chld
  18         string="parent"
  19 
  20 # Oba procesy bezia sucasne
  21 for i in range(20):
  22         print string
  23         time.sleep(0.1)
  24 
  25 if pid_chld==0:
  26 # child vrati cislo 101
  27         print "child konci"
  28         sys.exit(101)
  29 else:
  30 # parent si preberie navratovu hodnotu od jadra
  31         ret_wait=os.wait()
  32 # os.wait vracia v Pythone dvojicu hodnot
  33         pid_chld_wait=ret_wait[0] # pid child procesu
  34         ret_chld=ret_wait[1] >> 8 # a nahratovu hodnotu (shiftnutu)
  35         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.