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

前端 未结 2 1004
再見小時候
再見小時候 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:31

    It's because file descriptors 0, 1 and 2 are input, output and error respectively, and open will grab the first file descriptor available. Note that this will only work if file descriptors 0, 1 and 2 are not already being used.

    And you should be careful about the terms used, stdin, stdout and stderr are actually file handles (FILE*) rather than file descriptors, although there is a correlation between those and the file descriptors.

提交回复
热议问题