問題描述
在某些 linux 程序中關閉奇怪的描述符 (Strange descriptor closing in some linux programs)
在跟踪一些 linux 守護進程(例如 sendmail)時,我注意到其中一些會在開始時對多個描述符(通常從 3 到 255)調用 close()。這是故意做的還是做其他事情的某種副作用?
參考解法
方法 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.