文件描述符的可能值是什么? [英] What are the possible values for file descriptors?

查看:119
本文介绍了文件描述符的可能值是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有兴趣知道文件描述符可以使用的有效值.

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

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

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:

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

可能会发生错误,结果我收到-1.
偶然地,(-1)否定词必须具有特殊的含义.是否所有其他值都是有效的文件描述符?也就是负数,例如-2和-1023?

an error might occur and I receive -1 as a result.
Incidently the (-1) negative one must 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?

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

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

(-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

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

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?

再次说明:

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

除-1以外的任何值?

推荐答案

来自手册页:

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

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

然后:

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

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

这篇关于文件描述符的可能值是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆