歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux命令之sync - 強制將內存中的文件緩沖內容寫到磁盤

Linux命令之sync - 強制將內存中的文件緩沖內容寫到磁盤

日期:2017/2/28 16:17:17   编辑:Linux教程

用途說明
Linux內核為了達到最佳的磁盤操作效率,會把需要寫入到磁盤的數據現在內存中緩存起來,在合適的時候才真正寫入到磁盤中,這在絕大多數情況都是沒有任何問題的,而且提高了系統的效率,但是如果系統當機、掉電,就會有些文件內容不會保存下來。在Linux系統關機或者重啟時,會自動把緩沖區的內容自動同步到磁盤中。我們也可以手工去執行sync命令,強制將內存中的文件緩沖內容寫到磁盤,這個命令是通過調用sync系統調用來實現的。(flush file system buffers。Synchronize data on disk with memory。Force changed blocks to disk, update the super block.`sync' writes any data buffered in memory out to disk. This can include (but is not limited to) modified superblocks, modified inodes, and delayed reads and writes. This must be implemented by the kernel; The `sync' program does nothing but exercise the `sync' system call. The kernel keeps data in memory to avoid doing (relatively slow) disk reads and writes. This improves performance, but if the computer crashes, data may be lost or the file system corrupted as a result. The `sync' command ensures everything in memory is written to disk.)但是,我認為一般情況下我們是沒有必要去執行這個命令的,因為Linux內核會盡快讓內存中的數據自動同步到磁盤上去,而且,誰又會知道什麼時候會掉電或當機呢?

常用方式
格式:sync

強制將內存中的文件緩沖內容寫到磁盤。

使用示例
示例一
[[email protected] ~]# sync
[[email protected] ~]#

示例二
問題思考
1. 在以前使用Unix(SCO Unix, Sun OS)的時候,那些人教我們使用sync命令,所以記住了它,有些文章也說在關閉或重啟系統之前要多次執行sync命令,有的說要2此,有的說最好4次。各位看官,你們怎麼看待這個問題,歡迎留言賜教。

Copyright © Linux教程網 All Rights Reserved