Why should one redirect STDIN, STDOUT, STDERR to /dev/null, during startup of daemon?

stdin, stdout and stderr are closed so that the daemon can detach successfully from the tty it was started from and also so that the daemon (or its child processes) won't write to the tty when its running.

If you attempt to read/write from a closed file descriptor, the operation will fail and errno will be set to EBADF ("fildes is not a valid file or socket descriptor open for reading"). Other than that, nothing untoward will happen.

Tags:

Unix

C

Daemon