既然没有'fd'的返回,它以后如何读/写?
例如:
void init(void)
{
....
(void) open("/dev/tty1",O_RDWR,0);
....
答案 0 :(得分:7)
open
在其中返回一个值。 The cast-to-void is used to signal the compiler that the return value is being deliberately ignored.
init
函数是当前线程准备在用户空间中执行init
程序的函数。 init
将期望打开标准输入,输出和错误描述符。完整的代码是这样的:
(void) open("/dev/tty1",O_RDWR,0);
(void) dup(0);
(void) dup(0);
不需要将返回值存储为任何值,因为保证open
使用最低的空闲描述符,并且在进入此函数之前,进程不会使用它们。 open
将返回0。返回最低可用空间的相同规则也适用于dup
。在这3次调用之后,所有描述符0、1和2共享相同的文件描述,这也意味着您可以写入标准错误并从标准错误中读取。
这也许是一个微优化,但是确实不需要使用变量并使编译器在知道open的返回值时生成次标准代码-毕竟是
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);
但是,实际的文件描述符返回值将被忽略。