总有人间一两风,填我十万八千梦

Linux刷新文件缓存,将文件写入到磁盘中

Linux C/C++ Zero、J 11190℃ 0评论

ddreadector由于Linux LVM的缓存问题,使用 ls 命令可以看到的文件,在磁盘扇区中并不存在,这个时候读取的扇区数据就是无效的,特么的还在缓存里面,没写到磁盘里面,这还读取个毛啊、、、经过一番搜索找到了相关的解决的方法。

原理:通过修改proc系统的 drop_caches 来清理 free 的 cache,下面是相关的文档信息:

Writing to this will cause the kernel to drop clean caches, dentries and inodes from memory, causing that memory to become free.
To free pagecache:
* echo 1 > /proc/sys/vm/drop_caches
To free dentries and inodes:
* echo 2 > /proc/sys/vm/drop_caches
To free pagecache, dentries and inodes:
* echo 3 > /proc/sys/vm/drop_caches
As this is a non-destructive operation, and dirty objects are notfreeable, the user should run “sync” first in order to make sure allcached objects are freed.
This tunable was added in 2.6.16.

如果想要把缓存写入到磁盘,你应该这样子写

 system("sync");
 system("echo 3 > /proc/sys/vm/drop_caches");

通过刷新缓存后,读取的就是实时的硬盘数据了。

转载请注明:悠然品鉴 » Linux刷新文件缓存,将文件写入到磁盘中

喜欢 (3)or分享 (0)
发表我的评论
取消评论

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址