Attachment 'fork_exec_sigchld.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 import signal
9
10 # Funkcia zabezpecujuca obsluhu signalu SIGCHLD
11 # frame je interna pythonovska zalezitost, ignorujme
12 def handler(signum,frame):
13 print "Zavolal sa handler, signal = %d" % (signum)
14 # parent si preberie navratovu hodnotu od jadra
15 ret_wait=os.wait()
16 # os.wait vracia v Pythone dvojicu hodnot
17 pid_chld_wait=ret_wait[0] # pid child procesu
18 ret_chld=ret_wait[1] >> 8 # a nahratovu hodnotu (shiftnutu)
19 print "child %d vratil %d" % (pid_chld_wait,ret_chld)
20
21
22 if len(sys.argv)<=1:
23 print "Spust ma s parametrom meno_adresara"
24 sys.exit(1)
25
26 # Nastavime si osetrovanie signalu o tom, ze ls skoncil
27 signal.signal(signal.SIGCHLD, handler)
28
29 # Zavolame fork
30 pid_chld=os.fork()
31 # child dostane vratene 0, parent dostane PID child procesu
32 if pid_chld==0:
33 # Spusti sa ls pre prvy argument
34 # Prve /bin/ls je co sa ma spustit
35 # Druhe /bin/ls je argv[0] pre ls - tradicne to je meno programu
36 time.sleep(1)
37 print "Este zijem"
38 os.execlp("/bin/ls","/bin/ls",sys.argv[1])
39 print "Sem sa nikdy tok riadenia nedostane"
40 elif pid_chld<0:
41 print "fork vratil chybu"
42 sys.exit(1)
43 else:
44 print "PID child = %d" % pid_chld
45 string="parent"
46
47 # Oba procesy bezia sucasne, sem sa ale teraz dostane iba parent
48 for i in range(20):
49 print string
50 time.sleep(0.1)
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.- [get | view] (2008-04-25 09:01:27, 0.7 KB) [[attachment:fork.py]]
- [get | view] (2008-04-25 09:01:27, 1.0 KB) [[attachment:fork_exec.py]]
- [get | view] (2008-04-25 09:01:27, 1.3 KB) [[attachment:fork_exec_sigchld.py]]
You are not allowed to attach a file to this page.