驱动学习过程中的疑问:read函数的参数loff_t * p是怎么回事?
linux驱动框架里的read函数原型是:ssize_t (*read) (struct file * filp, char __user * buffer, size_t size , loff_t *p);在这个函数里有个参数 loff_t *p,表示读的位置相对于文件开头的偏移。但是我们在应用层的read函数为:size_t read(int fildes,const void *buf,size_t nbytes);里面没有参数 loff_t *p。那么这个应用层read函数在调用驱动层read函数时,这个偏移量的值是多少?由什么决定?大家可以讨论下。 各路大神来帮小弟的忙啊。小弟先出去演小品了,等明天在来看回帖! 我想用这个系统调用时用到,还有个pwritessize_t pread(int fd, void *buf, size_t count, off_t offset);
编辑本段用法返回值:成功,返回成功读取数据的字节数;失败,返回-1;
参数:
(1) fd:要读取数据的文件描述符
(2) buf:数据缓存区指针,存放读取出来的数据
(3) count:读取数据的字节数
(4) offset:读取的起始地址的偏移量,读取地址=文件开始+offset。注意,执行后,文件偏移指针不变
百度的例子:
#include <unistd.h>
#include <stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
void main()
{
int fd;
int count = 128;
int offset = 32;
int ret;
char buf;
char pathname = "/tmp/1.txt";
fd = open( pathname, O_RDONLY);
if((ret = pread(fd, buf, count, offset))=-1)
{
printf("pread error\n");
exit(1);
}
else
{
printf("pread success\n");
printf("the read data is:%s", buf);
}
}
回复 2# xifanlover
顺便问一下,你不会是赵本山吧!:lol:
回复 4# wwxxxxll
谢谢大神解惑,你的解释很有道理,估计底层驱动read可以被好几个应用层的函数调用。pread估计也是调用read的。十分感谢。cu真是个好地方。我不是本山,哈哈。只是党支部有个节目而已。
页:
[1]