如何将 mmap 输入内存写入 O_DIRECT 输出文件? [英] How to write mmap input memory to O_DIRECT output file?
本文介绍了如何将 mmap 输入内存写入 O_DIRECT 输出文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么不遵循伪代码工作(O_DIRECT 结果为 EFAULT)
why doesn't following pseudo-code work (O_DIRECT results in EFAULT)
in_fd = open("/dev/mem");
in_mmap = mmap(in_fd);
out_fd = open("/tmp/file", O_DIRECT);
write(out_fd, in_mmap, PAGE_SIZE);
虽然跟随(没有 O_DIRECT)
while following does (no O_DIRECT)
in_fd = open("/dev/mem");
in_mmap = mmap(in_fd);
out_fd = open("/tmp/file");
write(out_fd, in_mmap, PAGE_SIZE);
我猜是虚拟内核页面到虚拟用户页面的问题,在写入调用中无法转换?
I guess it's something with virtual kernel pages to virtual user pages, which cannot be translated in the write call?
最好的问候,
弗里德里希
推荐答案
将 mmap() 与 O_DIRECT 一起使用很棘手.有一些限制.文件的输出应该是块对齐的.例如,如果您将 mmap() 中的偏移量设置为 0,您的代码将起作用.您必须检查文件系统的块大小才能正确设置该值.
Using mmap() with O_DIRECT is tricky. There are some restrictions. The output to the file should be block aligned. For example, if you set offset in mmap() to 0 your code will work. You have to check the block size of your filesystem to set that value properly.
这篇关于如何将 mmap 输入内存写入 O_DIRECT 输出文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文