linux中mmap文件到内存中,该进程发生错误被挂掉后mmap映射的内存能否写回到文件中的问题

在Tokyo Cabinet中hashDB中的hash索引是通过mmap将数据库文件的一部分映射到内存中的,
之前把Tokyo Cabinet移植到手机淘宝客户端当做一个通用的KV数据库来使用,因为各种手机
的环境千差万别,手淘某些机型中得crash率很高. Tokyo Cabinet数据库文件总是不完整.

因为是手机客户端又不方便像在server端一样使用一个单独的线程定时同步mmap内存到文件中去....
手机淘宝的突然crash是导致mmap的数据不能及时写到文件,使得数据库文件被破坏的原因吗?

突然想到一个问题假设是进程中得其他lib引起crash,当crash时Tokyo Cabinet使用mmap打开的文件
在内存中的修改能否同步到硬盘中呢..搜了一些资料基本得出一个结论:
进程崩溃时,mmap的内存内核是会帮你写回到磁盘的
参照:
http://stackoverflow.com/questions/5902629/mmap-msync-and-linux-process-termination
I found a comment from Linus Torvalds that answers this question

http://www.realworldtech.com/forum/?threadid=113923&curpostid=114068
The mapped pages are part of the filesystem cache, which means that even if the user process that made a change to that page dies, the page is still managed by the kernel and as all concurrent accesses to that file will go through the kernel, other processes will get served from that cache. In some old Linux kernels it was different, that's the reason why some kernel documents still tell to force msync.


从内核代码中能基本看到这部分的处理逻辑


进程收到信号异常终止是会调用do_exit()释放资源, do_exit() 里面有ummap的调用:


mongodb貌似也试用了mmap. 总感觉mmap不太可靠.还是建议mmap使用者显示的调用msync进行文件的同步, 不要过分依赖内核的逻辑吧



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值