%E5%90%8E%E7%B3%BB%E7%BB%9F%E4%B8%AD%E7%9A%84%E6%96%87%E4%BB%B6%E6%8F%8F%E8%BF%B0%E7%AC%A6%E6%80%BB%E6%95%B0%E6%B2%A1%E6%9C%89%E5%A2%9E%E5%8A%A0%EF%BC%9F.png)
我首先在程序中创建了很多文件描述符,并且我看到系统文件描述符的数量在增加:
# bash(1) before:
cat /proc/sys/fs/file-nr
1024 0 97861
# bash (2): create a lot of fds
>>> a = []
>>> while True:
... a.append(open('asdf', 'a'))
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
IOError: [Errno 24] Too many open files: 'asdf'
>>>
正如预期的那样,文件描述符的数量增加了:
# back to bash (1) output
cat /proc/sys/fs/file-nr
2048 0 97861
现在,如果我在 python 中 fork() ,我希望内核也将所有这些 fd 复制到子级中 - 但这似乎并没有增加file-nr
?中的数量。
# bash (2): more commands at python- fork a child
>>> import os
>>> import time
>>> if os.fork() == 0:
... time.sleep(1000)
... else:
... time.sleep(1000)
...