简体   繁体   English

为什么在编写守护进程时必须关闭标准输入/输出/错误?

[英]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.

相关问题 处理守护进程的输入和 Output - Handling Input and Output of a Daemon 为什么在编写linux守护进程时必须从tty中分离出来? - Why MUST detach from tty when writing a linux daemon? 为什么我不能重定向标准输出/错误以登录Linux - Why I can NOT redirect standard output/error to log in Linux 在 linux 终端中重定向标准输出时是否需要使用“1>”? - Is it necessary to use '1>' while redirecting standard output in linux terminal? 在Android上通过ADB将标准输入写入文件 - Writing standard input to file via adb on android 为什么我尝试设置串口时,function 返回“输入/输出错误”? - Why when I try set serial port, function return “Input/output error”? 为什么当我将 glibc 升级到 2.29 然后我尝试设置串口时,函数返回“输入/输出错误”? - Why when I upgrade glibc to 2.29 then i try set serial port, function return "Input/output error"? 为什么在我的 fuse 文件系统中使用 ls 命令时会出现输入/输出错误? - Why Input/Output Error occurs when use ls command in my fuse filesystem? 捕获标准错误和标准输出的好方法 - A good way to capture standard error and standard output 启动makefile时,将标准错误重定向到文件并将标准输出保留到屏幕 - Redirecting standard error to file and leaving standard output to screen when launching makefile
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM