为什么使用loff_t * offp而不是直接使用filp-> f_pos的原因 [英] Reason why use loff_t *offp instead of direct filp->f_pos usage
问题描述
在以下功能中,取自LDD:
In following functions, taken from 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
吗?
Why there is the need of loff_t *offp
? Can't I use directly filp
to update f_pos
?
此外,在第54页中,作者说:
Moreover in page 54 the author says:
读取和写入操作应使用它们接收的指针作为最后一个参数来更新位置,而不是直接对
filp-> f_pos
进行操作.这是一个例外...
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
指针,但是为什么呢?
OK, so it's better to use the offp
pointer, but why?
推荐答案
filp-> f_pos
是文件中的当前指针位置,而 offp
是用户访问的位置归档.如果读/写操作成功,则将文件指针前进,如果失败,则不应更改文件指针.内核会自己执行,如果您成功地进行了读/写操作,它将把 filp-> f_pos
更改为 offp
.引用LDD3:
filp->f_pos
is current pointer position in file, whereas offp
is where user does access to file. You advance file pointer on successful read/write operation, if you failed you should not change file pointer. Kernel does it itself, if you successfully did read/write it will change filp->f_pos
to offp
. Citing LDD3:
无论方法传输的数据量是多少,它们都应该通常更新* offp处的文件位置以表示当前位置成功完成系统调用后的文件位置.这然后内核将文件位置更改传播回文件适当的结构.
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.
这篇关于为什么使用loff_t * offp而不是直接使用filp-> f_pos的原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!