正在创建两个文件为同一文件描述符明确定义? [英] Is creating two FILEs for the same file descriptor well-defined?

查看:190
本文介绍了正在创建两个文件为同一文件描述符明确定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

POSIX指定创建 功能文件的文件描述符。 POSIX还指定了一个的fileno 函数,返回文件描述符的 FILE 。总之,这两个可用于创建第二个文件访问相同的底层的文件描述符与现有文件:

POSIX specifies an fdopen function that creates a FILE for a file descriptor. POSIX also specifies a fileno function that returns the file descriptor for a FILE. Together, these two could be used to create a second FILE accessing the same underlying file descriptor as an existing file:

FILE *secondfile(FILE *f, const char *mode)
{
    int fd = fileno(f);

    return fd >= 0 ? fdopen(fd, mode) : NULL;
}

这是POSIX下一个定义良好的操作?当我访问这两个原始文件第二文件,会发生什么我在同一文件描述符做?在互动中指定?如果是的话,怎么样?

Is this a well-defined operation under POSIX? What happens when I access both the original FILE and the second FILE I made for the same file descriptor? Is the interaction specified? If yes, how?

历史上,UNIX系统中使用文件结构的固定台的20个文件,你可以打开。调用 fdopen()上已经有一个文件相关联的文件描述符将因此破坏现有的文件和产量未定义的行为。我不知道如果这样的实施 STDIO 仍然由POSIX,这就是为什么我问这个问题允许的。

Historically, Unices used a fixed table of FILE structures for the 20 files you could open. Calling fdopen() on a file descriptor that has already been associated with a FILE would thus corrupt the existing file and yield undefined behaviour. I am not sure if such an implementation of stdio is still allowed by POSIX, which is why I am asking this question.

推荐答案

POSIX的明确允许多个手柄要与同一标的开放式文件说明,其中手柄可以是文件描述符或数据流同时有关。虽然没有具体解决通过 fdopen()在同一个文件描述符打开多个流,我看不出有任何理由认为,这些将受到比任何更多的或不同的要求用相同的打开文件描述相关的两个数据流一般都需经过。

POSIX explicitly permits multiple "handles" to be associated simultaneously with the same underlying "open file description", where handles can be either file descriptors or streams. Although it does not specifically address multiple streams opened via fdopen() on the same file descriptor, I see no reason to suppose that these would be subject to more or different requirements than any two streams associated with the same open file description are generally subject to.

POSIX如何两个把手定义了同一个打开的文件的描述约束可能为了使用以避免发生不可预料的行为。这是与此有关,这些约束是少数确实是文件描述符句柄;几乎所有的人都适用于流,他们主要是围绕组织相关的缓冲条件。唯一的例外是与定位。

POSIX defines constraints on how two handles on the same open file description may be used in order to avoid undefined behavior. It is relevant here that those constraints are few indeed for handles that are file descriptors; almost all of them apply to streams, and they are organized mainly around conditions related to buffering. The exceptions are related to positioning.

如果您使用流与这些约束相一致的方式 - 主要是,但不完全,通过确保输出在一个流不会被缓冲不成文的,当你切换到使用其他的 - 你可以期望流I / O功能表现为记录。否则,该行为被明确定义。

If you use your streams in a manner consistent with those constraints -- mostly, but not exclusively, by ensuring that output is not buffered unwritten in one stream when you switch to using the other -- you can expect the stream I/O functions to behave as documented. Otherwise, the behavior is explicitly undefined.

这篇关于正在创建两个文件为同一文件描述符明确定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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