问题描述
我史蒂芬的UNIX网络编程,他提到重定向建立一个守护进程时,这是需要标准输入,输出和错误。他确实与下面的C code
I Steven's UNIX Network Programming, he mentions redirecting stdin, stdout and stderr which is needed when setting up a daemon. He does with the following C code
/* redirect stdin, stdout, and stderr to /dev/null */
open("/dev/null", O_RDONLY);
open("/dev/null", O_RDWR);
open("/dev/null", O_RDWR);
我很困惑这三个如何知道它们被重定向三个STD *。特别是由于在过去的两个命令是相同的。有人能解释或点我在正确的方向?
谢谢
I'm confused how these three 'know' they are redirecting the three std*. Especially since the last two commands are the same. Could someone explain or point me in the right direction?Thanks
推荐答案
presumably文件描述符0,1和2已在该code执行时关闭,并且没有可能是其他线程分配新的文件描述符。在这种情况下,由于开
必须始终分配最小的可用文件描述符,这三个调用打开将产生文件描述符0,1,2,除非他们失败
Presumably file descriptors 0, 1, and 2 have already been closed when this code executes, and there are no other threads which might be allocating new file descriptors. In this case, since open
is required to always allocate the lowest available file descriptor number, these three calls to open will yield file descriptors 0, 1, and 2, unless they fail.
这篇关于重定向STDIN,STDOUT,STDERR到/ dev / null的用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!