【问题标题】:How is the correct way to call read function of file operations?调用文件操作的读取函数的正确方法是什么?
【发布时间】:2013-07-02 21:22:12
【问题描述】:

在 struct file_operations 中,定义在第 1517 行 from:

http://lxr.free-electrons.com/source/include/linux/fs.h?a=arm

1520行有读取操作的方法:

  ssize_t (* read) (struct file *, char __ user *, size_t, loff_t *);

从应用程序调用此方法的正确方法是什么? 我在将"loff_t *"作为参数传递时遇到问题@

【问题讨论】:

  • loff_t outoff; ssize_t readb = read(fp, buf, how_many, &outoff);
  • 谢谢,但我从驱动程序打印值并作为 0 printk(KERN_ALERT "Offset: %lld\n", *offset); “dmesg”中的结果始终为 0

标签: c linux-kernel kernel linux-device-driver device-driver


【解决方案1】:

我相信,要在“loff_t *”参数中获得非零值并首先从您的应用程序中读取,您需要先调用“lseek”。

喜欢:

#include <unistd.h>
...
offset = ...
pos = lseek(fh, (off_t) offset, SEEK_SET);
read(fh, buffer, bytesToRead);

这里的偏移量将通过“loff_t* 参数”传递给内核。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-24
    • 1970-01-01
    • 1970-01-01
    • 2014-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多