一些 Linux 程序中奇怪的描述符关闭
在跟踪一些 Linux 守护进程(例如 sendmail)时,我注意到其中一些守护进程会在开始时对多个描述符(通常范围从 3 到 255)调用 close()。这是故意这样做的,还是做其他事情的某种副作用?
While stracing some linux daemons (eg. sendmail) I noticed that some of them will call close() on a number of descriptors (usually ranging from 3 to 255) right at the beginning. Is this being done on purpose or is this some sort of a side effect of doing something else?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它通常作为使进程成为守护进程的一部分来完成。
所有文件描述符都被关闭,以便长时间运行的守护进程不会不必要地占用任何资源。例如,如果守护程序要继承一个打开的文件,并且该守护程序没有关闭该文件,则无法删除该文件(该文件的存储将保持分配状态直到关闭),并且无法卸载该文件所在的文件系统。
守护进程还将采取许多其他操作,但这些操作超出了本问题的范围。
It is usually done as part of making a process a daemon.
All file descriptors are closed so that the long-running daemon does not unnecessarily hold any resources. For example, if a daemon were to inherit an open file and the daemon did not close it then the file could not be deleted (the storage for it would remain allocated until close) and the filesystem that the file is on could not be unmounted.
Daemonizing a process will also take a number of other actions, but those actions are beyond the scope of this question.