Network Programming and file descriptor 0, 1, 2

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.