安全的編程方式:fopen() -> fflush() -> fsync() -> fclose()
確保數(shù)據(jù)落盤的幾種方法:
1.open(O_DIRECT):write/read時,文件系統(tǒng)的IO會繞過cache直接操作磁盤IO;
2.open(O_SYNC):確保每一筆IO都是同步落盤的,相當(dāng)于是每次write后自動調(diào)用fsync;
3.open(O_DSYNC):相當(dāng)于是每次write后自動調(diào)用fdatasync;
4.fsync: write后調(diào)用fsync刷新文件數(shù)據(jù)+元數(shù)據(jù)緩沖區(qū);
5.fdatasync: write后調(diào)用fdatasync刷新文件數(shù)據(jù);
6.sync: 標(biāo)準(zhǔn)IO后調(diào)用sync強制刷新內(nèi)核緩沖區(qū)到磁盤,linux是文件真正落盤后才會返回;
7.msync: 通過mmap函數(shù)把文件映射到進(jìn)程的地址空間,讀寫進(jìn)程內(nèi)存的地址的數(shù)據(jù)其實是轉(zhuǎn)發(fā)到磁盤上去讀寫,write后調(diào)用msync強制刷盤。
Ensuring data reaches disk https://lwn.net/Articles/457667/
深入理解 Linux的I/O 系統(tǒng)
https://view.inews.qq.com/a/20211216A01H5600
怎么才能保證 IO 數(shù)據(jù)的安全
https://blog.csdn.net/FL63Zv9Zou86950w/article/details/116505686
緩存技術(shù)及算法策略簡介
http://www.xupifu.com/2017/01/19/cache-introduction/