为什么 open() 在 linux 1.0 中没有 'fd' return?

Why open() has no 'fd' return in linux 1.0?

既然没有'fd'的return,那以后怎么read/write呢?

例如:

void init(void)
{
 ....
 (void) open("/dev/tty1",O_RDWR,0);
 ....

open return 是其中的一个值。 The cast-to-void is used to signal the compiler that the return value is being deliberately ignored.

init函数就是当前线程准备执行user-space中的init程序的函数。 init 将期望打开标准输入、输出和错误描述符。完整代码是这样的:

(void) open("/dev/tty1",O_RDWR,0);
(void) dup(0);
(void) dup(0);

不需要将 return 值存储到任何东西,因为 open 保证使用 最低的可用描述符 和 none 在进入此函数之前被进程使用,因此 open 将 return 0。相同的规则 returning the lowest free 也适用于 dup。在这 3 次调用之后,所有描述符 0、1 和 2 共享相同的 文件描述 这也意味着您可以写入标准输入并从标准错误中读取。

这也许是一个微优化,但确实没有必要使用变量并在 open 的 return 值已知时让编译器生成不合标准的代码 - 它 毕竟类似于

int fd = open("/dev/tty1",O_RDWR,0);
assert(fd == 0);
(void) dup(fd);
(void) dup(fd);

In the current revision 那里 一个断言,检查 open 没有失败:

/* Open the /dev/console on the rootfs, this should never fail */
if (ksys_open((const char __user *) "/dev/console", O_RDWR, 0) < 0)
    pr_err("Warning: unable to open an initial console.\n");

(void) ksys_dup(0);
(void) ksys_dup(0);

但是,实际的文件描述符 return 值将被忽略。