gpt4 book ai didi

linux - 使用 sysfs show() 和 store() 函数

转载 作者:太空宇宙 更新时间:2023-11-04 10:04:40 24 4
gpt4 key购买 nike

关于使用这些功能,我有 2 个问题。我不完全理解编写的文档 here :

sysfs allocates a buffer of size (PAGE_SIZE) and passes it to the method. Sysfs will call the method exactly once for each read or write. This forces the following behavior on the method implementations:

  • On read(2), the show() method should fill the entire buffer. Recall that an attribute should only be exporting one value, or an array of similar values, so this shouldn't be that expensive.

    This allows userspace to do partial reads and forward seeks arbitrarily over the entire file at will. If userspace seeks back to zero or does a pread(2) with an offset of '0' the show() method will be called again, rearmed, to fill the buffer.

  • On write(2), sysfs expects the entire buffer to be passed during the first write. Sysfs then passes the entire buffer to the store() method. A terminating null is added after the data on stores. This makes functions like sysfs_streq() safe to use.

    When writing sysfs files, userspace processes should first read the entire file, modify the values it wishes to change, then write the entire buffer back.

首先,当我用read/write读/写sysfs属性文件时,我是否保证我读到的缓冲区/store函数中的缓冲区将包含我想在该函数中读取的所有字节,而不是在几个 block 中调用它?

还有,空字符是怎么加的?也就是说,假设我写了n个字节,函数参数中写的字节数是n,null char会放在n+1处吗?

谢谢

最佳答案

第一个问题的答案是 - 不支持部分写入,缓冲区总是在调用 show 方法时填充。

第二个问题的答案也是肯定的。请参阅 sysfs 使用的实现 kernfs_fop_write() - 它将分配最多 PAGE_SIZE+1 字节,以便有足够的空间容纳\0。

关于linux - 使用 sysfs show() 和 store() 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53415524/

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