Linux设备驱动程序调用错误计数

时间:2016-09-12 02:10:31

标签: inputstream linux-device-driver iostream

我在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 ++流调用read,如下所示......

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调用驱动程序读取函数。写入函数的行为类似。

谷歌让我失望了。帮助

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

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