Redirecting STDIN, STDOUT, STDERR to /dev/null in C

前端 未结 2 999
再見小時候
再見小時候 2021-01-04 04:50

In Stevens\' UNIX Network Programming, he mentions redirecting stdin, stdout and stderr, which is needed when setting up a daemon. He does it with the following C code

2条回答
  •  再見小時候
    2021-01-04 05:18

    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.

提交回复
热议问题