一些 Linux 程序中奇怪的描述符关闭

发布于 2024-10-04 22:00:27 字数 110 浏览 0 评论 0原文

在跟踪一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幼儿园老大 2024-10-11 22:00:27

它通常作为使进程成为守护进程的一部分来完成。

所有文件描述符都被关闭,以便长时间运行的守护进程不会不必要地占用任何资源。例如,如果守护程序要继承一个打开的文件,并且该守护程序没有关闭该文件,则无法删除该文件(该文件的存储将保持分配状态直到关闭),并且无法卸载该文件所在的文件系统。

守护进程还将采取许多其他操作,但这些操作超出了本问题的范围。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文