What's unexpected about it? Child processes inherit all of the open file descriptors of their parent. A socket is simply another open file descriptor. When your parent process exits, your child still holds a valid, open file descriptor.
<br><br>import sys<br>import socket<br>import os<br>import time<br><br>s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)<br>s.bind(('', 8888))<br><br>pid = os.fork()<br>if pid > 0:<br> time.sleep(100)<br>
elif pid == 0:<br> time.sleep(100)<br><br>$ lsof | grep python | grep jeff | grep 8888<br>python 13048 jeff 3u IPv4 212689 UDP *:8888 <br>python 13049 jeff 3u IPv4 212689 UDP *:8888
<br><br>-Jeff<br><br><div><span class="gmail_quote">On 7/19/07, <b class="gmail_sendername">alf</b> <<a href="mailto:ask@me.xs4all.nl">ask@me.xs4all.nl</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>Hi,<br><br>I need a help with explaining following behavior. Although it is not<br>python issue per say, python helped me to write sample programs and<br>originally I encountered the issue using python software. So let's
<br>assume we have two following programs:<br><br><br><br>[myhost] ~> cat ss.py<br>import socket<br>UDPSock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)<br>UDPSock.bind(("",12345))<br>import os<br>os.system
('python cc.py')<br><br>[myhost] ~> cat cc.py<br>import time<br>time.sleep(2036)<br><br><br><br>then I start master one, do ctrl-Z and bg.<br><br>[myhost] ~> python ss.py<br><br>Suspended<br>[myhost] ~> bg
<br>[1] python ss.py &<br>[myhost] ~> ps<br>UID PID PPID C STIME TTY TIME CMD<br>myuser00 3192 3189 0 14:57 pts/0 00:00:00 -tcsh<br>myuser00 3247 3192 0 14:57 pts/0 00:00:00 python
ss.py<br>myuser00 3248 3247 0 14:57 pts/0 00:00:00 python cc.py<br><br><br><br>[myhost] ~> netstat -uanp |grep 12345<br>(Not all processes could be identified, non-owned process info<br> will not be shown, you would have to be root to see it all.)
<br>udp 0 0 <a href="http://0.0.0.0:12345">0.0.0.0:12345</a> 0.0.0.0:*<br> 3247/python<br><br><br><br>As expected netstat indicates process 3247 having port allocated. Then I<br>kill the master process and interestingly the child inherits the port open.
<br><br><br>[myhost] ~> kill 3247<br>[myhost] ~> netstat -uanp | grep 12345<br>(Not all processes could be identified, non-owned process info<br> will not be shown, you would have to be root to see it all.)<br>udp 0 0
<a href="http://0.0.0.0:12345">0.0.0.0:12345</a> 0.0.0.0:*<br> 3248/python<br>[1] + Terminated python ss.py<br><br><br><br>Why it is happening? I know that os.system uses fork, but still this is
<br>something unexpected.<br><br><br>Thx, Alf<br><br><br><br>--<br><a href="http://mail.python.org/mailman/listinfo/python-list">http://mail.python.org/mailman/listinfo/python-list</a><br></blockquote></div><br>