[英]Why is it necessary to close standard input/output/error when writing a daemon?
为什么在编写(unix)守护进程时必须关闭标准输入/输出/错误?
Not only stdin, stdout and stderr should be closed, but all open files. 不仅应关闭stdin,stdout和stderr,还应关闭所有打开的文件。
From "Advanced Programming in the UNIX Environment", W. Richard Stevens, Addison-Weseley, 18th Printing, 1999, page 417. 来自“UNIX环境中的高级编程”,W。Richard Stevens,Addison-Weseley,第18期印刷,1999年,第417页。
Unneeded file descriptors should be closed.
应该关闭不需要的文件描述符。 This prevents the daemon from holding open any descriptors that is may have inherited from its parent (which could be a shell or some other process).
这可以防止守护进程保持打开任何可能从其父级继承的描述符(可能是shell或其他进程)。
Mr. Stevens proposal is to get the maximum file descriptor and close all files up to that value. 史蒂文斯先生的建议是获取最大文件描述符并关闭所有文件到该值。
The cited chapter is about 'Daemon Processes'. 引用的章节是关于'守护进程'。 Please note that closing file descriptors is only one point from five when writing daemons.
请注意,在编写守护进程时,关闭文件描述符只是五分之一。
You don't have to definitely close standard output/error, but you have to be sure where your daemon (and child) are going to writing to. 您不必绝对关闭标准输出/错误,但您必须确定您的守护程序(和子项)将要写入的位置。 Redirecting the output to a log file is a common practice.
将输出重定向到日志文件是一种常见做法。
Closing standard input is required so that the daemon (and/or child) doesn't get stuck trying to read some input from the user. 需要关闭标准输入,以便守护程序(和/或子项)不会因尝试读取用户的某些输入而卡住。
But if you're going to detach you daemon from a controlling TTY using setsid()
, the standard input/output/error are going to be invalid . 但是,如果您要使用
setsid()
从控制TTY中分离守护进程,则标准输入/输出/错误将无效 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.