我有兴趣在c程序中使用readahead系统调用。目前我打开了一个文件,通过调用open()进行阅读。一旦我找到一个字符,比如字母'b',我想测试下面的字母是'ar'来组成字符串'bar'。字符串将是一个任意长度,所以我将不得不循环。我想的是:
f = open(file, readpermissions);
read(fdr, buf, size);
if (buf[0] == word[i]) {
lookahead(.....);
}
这些方面的东西。目前我的缓冲区一次读取一个字符加上NULL终止符(如果存在)。
正确使用readahead来完成此任务的任何提示?
答案 0 :(得分:0)
从手册页:
int posix_fadvise(int fd, off_t offset, off_t len, int advice);
建议类型:
POSIX_FADV_NORMAL Indicates that the application has no advice to give about its access pattern for the specified data. If no advice is given for an open file, this is the default assumption. POSIX_FADV_SEQUENTIAL The application expects to access the specified data sequentially (with lower offsets read before higher ones). POSIX_FADV_RANDOM The specified data will be accessed in random order. POSIX_FADV_NOREUSE The specified data will be accessed only once. POSIX_FADV_WILLNEED The specified data will be accessed in the near future. POSIX_FADV_DONTNEED The specified data will not be accessed in the near future.
因此对于整个文件,offset和len将为0。
对于您的示例,我假设POSIX_FADV_SEQUENTIAL
是您要使用的。