gpt4 book ai didi

Linux I2C 文件句柄 - 缓存安全吗?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:37:13 25 4
gpt4 key购买 nike

我刚刚开始研究(嵌入式)Linux(准确地说是 Beaglebone Black)上的 I2C 支持。既然是linux,一切都是文件,所以I2C也是。

int file = open( "/dev/i2c-0", O_RDWR );

然后通过ioctl() 选择总线上的实际地址。我的问题是 - 在应用程序执行期间缓存 file 是否安全,甚至合法?在我天真的眼中,打开资源以每 250 毫秒读取一次的开销对内核来说是不必要的压力。所以打开是有效的,然后在需要时使用 ioctl() 切换地址,或者我必须 close() 读写之间的描述符?

最佳答案

is it safe, or even legal, to cache file for the duration of application execution?

只要您的程序需要继续执行,文件描述符(从 open() 返回)就有效。

/dev 中的设备节点可能类似于文件名,但一旦您查看系统调用接口(interface),它们就会与文件系统条目区别对待。设备文件描述符上的 open() 或 read() 将调用设备驱动程序,而对于实际文件,将调用其文件系统,最终可能调用存储设备驱动程序。

It would seem to my naive eyes that the overheading of opening a resource to read every 250ms would be an unnecessary strain on the kernel.

是的,因为那些 open() 和 close() 系统调用是不必要的。

So it is valid to open, and then just use ioctl() to switch address whenever I need to,

是的,这才是正确的用法。

or must I close() the descriptor between reads and writes?

没有必要也不建议这样做。

关于Linux I2C 文件句柄 - 缓存安全吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26216377/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com