分析断电时文件操作flush不能写入硬盘数据丢失问题

一般写文件时,为了确保程序退出时也能把文件写到硬盘,都要在程序中加flush强制把内存缓存写到硬盘中,但这个flush只对内存缓存起作用,当遇到突然断电,还是不能保证已经写到物理硬盘上了。

现象:比如写一个文本文件a.txt,程序调用了flush,用notepad直接打开文件a.txt,可以看到flush的内容已经写到a.txt了,然后关掉notepad切断电源,然后再同上电源开机打开a.txt,可能会发现flush的内容又没有了,但这种情况不是每次都出现,跟flush和断电的时间间隔有关系。

原因:flush只是强制把内存缓存写到硬盘中,不能保证真正写入物理硬盘,一般物理硬盘内部都有一定的磁盘缓存,当看到a.txt的flush内容时可能只是磁盘缓存。

解决办法:应该说没有完美的解决办法,如果把磁盘缓存关闭,会大大降低整个系统包括操作系统的运行效率。一般大型服务器使用磁盘阵列等存储设备时磁盘缓存会更大,根本不能关闭,所以他们是通过UPS等备用电源来保障不出现断电问题,即使真的完全断电,他们也没有办法,只能丢失增量部分数据;另外,对于普通客户端系统,可以考虑备份机制,最好用追加的方式写文件,不要对整个文件改写,这样即使断电也只丢失增量部分数据。

609人评论了“分析断电时文件操作flush不能写入硬盘数据丢失问题”

  1. I and also my pals have been studying the good information found on your website and then unexpectedly I got a horrible feeling I never thanked the site owner for those secrets. The guys came for that reason warmed to read all of them and have clearly been using those things. Appreciate your actually being very kind and also for settling on some useful ideas most people are really desperate to learn about. Our sincere regret for not saying thanks to you sooner.

  2. I wanted to construct a simple remark so as to express gratitude to you for all the stunning guidelines you are giving out here. My extensive internet look up has now been compensated with reasonable details to share with my visitors. I ‘d assert that most of us site visitors are rather blessed to exist in a notable network with so many awesome individuals with insightful techniques. I feel extremely blessed to have used your web site and look forward to really more amazing minutes reading here. Thank you again for everything.

  3. I wish to voice my gratitude for your kind-heartedness for individuals who really need assistance with this important area of interest. Your very own commitment to getting the solution all through turned out to be especially insightful and has continuously allowed those much like me to attain their aims. Your amazing informative publication signifies a great deal to me and a whole lot more to my peers. With thanks; from all of us.

  4. I simply had to thank you very much once more. I do not know the things I would have undertaken without the entire creative concepts documented by you about my area of interest. It previously was a challenging setting in my opinion, however , seeing your skilled mode you processed that made me to leap with fulfillment. Now i am happy for this support and even expect you comprehend what a powerful job you were carrying out educating people today via a blog. Probably you have never met all of us.

  5. I just wanted to write a simple remark to be able to say thanks to you for the unique guides you are giving out at this website. My time-consuming internet research has finally been rewarded with good quality concept to talk about with my neighbours. I ‘d assume that we website visitors are undoubtedly blessed to be in a perfect place with very many marvellous individuals with interesting ideas. I feel somewhat fortunate to have used the website and look forward to many more fabulous moments reading here. Thanks once more for everything.

  6. I as well as my buddies were actually viewing the great secrets and techniques from your web page and before long I got a horrible suspicion I had not thanked the web blog owner for them. Those guys were definitely as a consequence stimulated to learn them and now have actually been taking pleasure in these things. Thank you for turning out to be very thoughtful and for selecting these kinds of quality useful guides millions of individuals are really desperate to know about. Our sincere apologies for not expressing appreciation to earlier.

  7. I and my guys came checking the good techniques found on the website and so unexpectedly developed an awful feeling I never thanked the web blog owner for those strategies. My people were definitely totally passionate to learn them and already have surely been loving these things. Thank you for actually being very kind as well as for picking certain remarkable themes most people are really desirous to discover. My sincere regret for not saying thanks to you sooner.

  8. I happen to be commenting to make you be aware of of the remarkable encounter my princess gained using the blog. She picked up a good number of things, with the inclusion of what it is like to have a very effective helping mindset to make the mediocre ones really easily learn about specified specialized subject areas. You really did more than her expectations. Thanks for churning out those great, dependable, explanatory as well as easy tips on your topic to Jane.

发表评论

邮箱地址不会被公开。 必填项已用*标注