我弄明白了我遇到的问题。为了子孙后代,我会记录下答案,而不仅仅是删除我的问题,因为答案不一定容易找到。
本质上,问题发生在FUSE中。FUSE默认不使用直接I/O(这绝对是正确的默认设置,请不要误会),这导致读取大小为4096的块
(these are the result of FUSE using a page cache of file contents [AKA a file content cache] in the kernel)
。对于我想要测试的内容(如问题中所解释的),我需要启用直接I/O。有一个
few ways of doing this
但我最简单的方法就是通过
-o direct_io
作为命令行参数。这对我有用,因为我使用
fuse_main
调用
main
我的程序的功能。
所以我的
主要的
函数如下所示:
int main(int argc, char *argv[])
{
return fuse_main(argc, argv, &my_operations_structure, NULL);
}
我可以这样调用我的程序(我使用
-d
选项
-o直接_io
选项,以便显示FUSE正在处理的系统调用和程序的输出/调试信息):
./prog_name -d -o direct_io test_directory
然后,我用以下简单的测试程序测试了我的程序(我知道我不做很多错误检查,但这个程序只用于一些快速和肮脏的测试):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
int main(int argc, char *argv[])
{
FILE * file;
char buf[4096];
int fd;
memset(&buf[0], 0, sizeof(buf));
if (argc != 4)
{
printf("usage: ./readTest [size] [offset] [filename]\n");
return 0;
}
file = fopen(argv[3], "r");
if (file == NULL)
{
printf("Couldn't open file\n");
return -1;
}
fd = fileno(file);
pread(fd, (void *) buf, atoi(argv[1]), (off_t) atoi(argv[2]));
printf("%s\n", buf);
return 0;
}