gpt4 book ai didi

c - 为什么使用 loff_t *offp 而不是直接使用 filp->f_pos 的原因

转载 作者:行者123 更新时间:2023-12-02 01:49:50 24 4
gpt4 key购买 nike

在以下函数中,取自 LDD:

ssize_t read(struct file *filp, char __user *buff, size_t count, loff_t *offp);
ssize_t write(struct file *filp, const char __user *buff, size_t count, loff_t *offp);

为什么需要loff_t *offp?不能直接用filp更新f_pos吗?

此外,在第 54 页,作者说:

Read and write should update a position using the pointer they receive as the last argument instead of acting on filp->f_pos directly. The one exception to this...

好的,所以最好使用 offp 指针,但为什么呢?

最佳答案

filp->f_pos 是文件中的当前指针位置,而 offp 是用户访问文件的位置。你在成功的读/写操作上推进文件指针,如果你失败了你不应该改变文件指针。内核自己做,如果你成功读/写它会将 filp->f_pos 更改为 offp。引用 LDD3:

Whatever the amount of data the methods transfer, they should generally update the file position at *offp to represent the current file position after successful completion of the system call. The kernel then propagates the file position change back into the file structure when appropriate.

关于c - 为什么使用 loff_t *offp 而不是直接使用 filp->f_pos 的原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23501185/

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