【问题标题】:Linux device driver called with wrong count调用错误计数的 Linux 设备驱动程序
【发布时间】:2016-09-12 02:10:31
【问题描述】:

我在 CentOS 7 上实现了一个字符设备驱动程序。当从 C 程序调用该驱动程序时,它运行正常,因此...

char bytes[8];
int fd = open("/dev/fortuna", O_RDONLY);
if (fd < 0) {
    perror("File open error: ");
    return -1;
}
int in = read(fd, bytes, 8);
if (in < 0) {
    perror("File read error: ");
}
close(fd);

使用 count = 8 调用驱动程序读取函数,程序完成且没有错误。驱动函数原型是……

static ssize_t fortuna_read(struct file *filp, char *buff, size_t count, loff_t *offp);

但是,如果从 C++ 流中调用读取,则如下...

char bytes[8];
std::ifstream in("/dev/fortuna");
if (in.good()) {
    in.read(bytes, 8);
}
in.close();

驱动读取函数调用count = 8191。如果驱动是从Java FileReader调用的,如下...

File file = new File("/dev/fortuna");
file.setReadOnly();
FileReader reader = new FileReader(file);
char[] cbuf = new char[8];
int read = reader.read(cbuf);
reader.close();

使用 count = 8192 调用驱动程序读取函数。写入函数的行为类似。

Google 让我失望了。帮忙?

【问题讨论】:

    标签: inputstream linux-device-driver iostream


    【解决方案1】:

    这与缓冲有关。 我很确定如果你一直使用 stdio.h 和 fopen() I/O 系列而不是 open(),你会在 C 中看到同样的问题。

    所以你应该禁用缓冲区。 对于 C++,有这个答案: How to disable buffering on a stream?

    【讨论】:

    • 谢谢您,先生。您的回答也让我想到了 Java 解决方案。
    【解决方案2】:

    感谢 A.B,我还找到了 Java 解决方案。 FileReader 是一个缓冲阅读器。要使用的正确 Java 类是 FileInputStream。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-22
      • 1970-01-01
      • 1970-01-01
      • 2013-04-13
      • 2018-05-19
      • 1970-01-01
      • 2023-03-03
      相关资源
      最近更新 更多