What are the possible values for file descriptors?

拥有回忆 提交于 2019-12-05 01:18:04

From the man page:

open() returns a file descriptor, a small, nonnegative integer.

and then:

open() and creat() return the new file descriptor, or -1 if an error occurred

When open fail, it returns -1, or 0xffffffff. It has no meaning but open failed:

Upon successful completion, the function shall open the file and return a non-negative integer representing the lowest numbered unused file descriptor. Otherwise, -1 shall be returned and errno set to indicate the error. No files shall be created or modified if the function returns -1.

The failure reason is stored in errno, you can read its value and check if it's one of the possible failures reasons EACCES, EEXIST, EINTR.. etc, or just use perror to print the error message.

Here's what a Linux manual page says:

open() and creat() return the new file descriptor, or -1 if an error occurred (in which case, errno is set appropriately).

Other systems may return other negative values in case of error.

premal

Range of possible values of file descriptors is from 0 to 1023 for Linux system (32-bit or 64-bit system).

You cannot create a file descriptor with value more then 1023. In case of file descriptor of value 1024, it will return an error of EBADF (bad file descriptor, error no-9).

When a negative value of file descriptor is returned it indicates that an error has occurred.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!