Linux 文件描述符的可能值是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18507057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:42:12  来源:igfitidea点击:

What are the possible values for file descriptors?

clinuxvalidationfile-descriptor

提问by humanityANDpeace

I am interested to know the valid values which I can expect for a file descriptor.

我很想知道我可以期待的文件描述符的有效值。

Please let me explain a bit. I know that, for instance, when I use #include <unistd.h>on my linux system then a call to open a file for reading:

请让我解释一下。我知道,例如,当我#include <unistd.h>在我的 linux 系统上使用时,然后调用打开一个文件进行读取:

int fileDescriptor;
fileDescriptor = open("/some/filename",O_RDONLY);

an error might occur and I receive -1 as a result.
Incidently the (-1) negative onemust have somewhat of a special meaning. Is it that all other values are valid file descriptors? i.e. also negative ones like -2 and -1023?

可能会发生错误,结果我收到 -1。
顺便说一下,(-1) 负数肯定有某种特殊的含义。是否所有其他值都是有效的文件描述符?即也像-2和-1023这样的负数?

Assuming that int is 4 bytes (sizeof(int)==4), then would

假设 int 是 4 个字节 ( sizeof(int)==4),那么

(-1) = 10000000 0000000 00000000 00000001

would be the only detectable invalid file descriptor? Would others like:

将是唯一可检测的无效文件描述符?其他人是否喜欢:

  • (0) = 00000000 0000000 00000000 00000000
  • (-2) = 10000000 0000000 00000000 00000010
  • (2) = 00000000 0000000 00000000 00000010
  • (0) = 00000000 0000000 00000000 00000000
  • (-2) = 10000000 0000000 00000000 00000010
  • (2) = 00000000 0000000 00000000 00000010

be ok? Since the file descriptor could store 4 bytes I could have therefore a maximum of (2^(8*4)-1) valid file descriptors and consequently this would be the maximum number of files I can have open, correct?

好吗?由于文件描述符可以存储 4 个字节,因此我最多可以拥有 (2^(8*4)-1) 个有效文件描述符,因此这将是我可以打开的最大文件数,对吗?

To put it plain again:

再说清楚一点:

What should I expect a (valid) file descriptor to be?

我应该期望(有效的)文件描述符是什么?

any value but -1?

除了 -1 之外的任何值?

采纳答案by Oliver Charlesworth

From the man page:

手册页

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

open()返回一个文件描述符,一个小的非负整数

and then:

进而:

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

open()creat()返回新的文件描述符,如果发生错误,则返回-1

回答by meaning-matters

Here's what a Linux manual pagesays:

以下是 Linux手册页的说明:

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

open()creat()返回新的文件描述符,或者-1如果发生错误(在这种情况下,errno设置适当)。

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

其他系统可能会在出错时返回其他负值。

回答by Oliver Charlesworth

When openfail, it returns -1, or 0xffffffff. It has no meaning but openfailed:

open失败时,它返回-1, 或0xffffffff。它没有意义但open失败了:

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.

成功完成后,该函数应打开文件并返回一个非负整数,表示编号最低的未使用文件描述符。否则,将返回 -1 并设置 errno 以指示错误。如果函数返回 -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 perrorto print the error message.

失败原因存储在 中errno,您可以读取其值并检查它是否是可能的失败原因之一EACCESEEXISTEINTR.. 等,或者仅用于perror打印错误消息。

回答by premal

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

对于 Linux 系统(32 位或 64 位系统),文件描述符的可能值范围是从 0 到 1023。

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).

您不能创建值大于 1023 的文件描述符。如果文件描述符值为 1024,它将返回 EBADF 错误(错误文件描述符,错误号 9)。

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

当返回文件描述符的负值时,表示发生了错误。