As mentioned in chapter 13th daemon (pp. 343-344) of advanced Unix programming, you can use the following method to enable/dev/null for daemon, make it have file descriptors 0, 1, and 2.
/* Close */
For (I = 0; I <open_max; I ++)
{
Close (I );
}
Fd0 = open ("/dev/null", o_rdonly );
Fd1 = DUP (0 );
Fd2 = DUP (0 );
If a process is run from the shell, three file descriptors (0, 1, 2) exist by default. 0 is associated with the standard input of the process, 1 is associated with the standard output of the Process, 2 is associated with the standard error output of the process.
The file descriptor returned by open must be the smallest unused descriptor value. In network programming, if the service process is not a daemon and does not occupy file descriptors, 2, then the Socket socket may use file descriptors, 2, this will cause errors when the server receives and sends messages, resulting in service failure.