磁碟結構和讀寫

IO設備分為塊設備和字元設備。磁碟屬於前者,鍵盤和印表機屬於後者(以字元為單位傳輸)。

塊設備的基本特徵是將數據存儲到固定大小的塊中,每個塊都是可定址的,而且互相獨立於其他塊可進行讀寫。

存儲容量 = 磁頭數 × 磁軌(柱面)數 × 每道扇區數 × 每扇區位元組數

上圖磁碟是一個 3個圓盤6個磁頭,7個柱面(每個碟片7個磁軌) 的磁碟,圖2中每條磁軌有12個扇區,所以此磁碟的容量為6*7*12*512位元組(252KB)。

磁碟讀取時間 = 尋道時間 + 旋轉時間 + 傳輸時間

當需要從磁碟讀取數據時,系統會將數據邏輯地址傳給磁碟,磁碟的控制電路按照定址邏輯將邏輯地址翻譯成物理地址,即確定要讀的數據在哪個磁軌,哪個扇區。為了讀取這個扇區的數據,需要將磁頭放到這個扇區上方,為了實現這一點,磁頭需要移動對準相應磁軌,這個過程叫做尋道,所耗費時間叫做尋道時間,然後磁碟旋轉將目標扇區旋轉到磁頭下,這個過程耗費的時間叫做旋轉時間

根據局部性原理,如果某處用戶用到,那麼附近的數據不久後也會用到。所以磁碟IO會預讀取一定長度的數據。由於順序讀取效率很高,所以預讀取會提高IO效率。

預讀取的長度一般是頁的整數倍,操作系統中頁的大小是4K。內存和磁碟以頁作為數據交換的單位。磁碟找到數據後,會在起始位置讀取一個或多個頁的數據。

The speed of the disk drives determine how fast you can read and write data from and to it. The faster the better. The speed of a disk consists of two numbers: Search time and transfer time.

Search time tells how fast the disk can search to a certain position on the disk. The transfer time tells how fast the disk can transfer data once it is in the right position.

Some disks have a bit of read cache RAM to speed up reading of data from the disk.When a chunk of data is requested from the disk, the disk will read a bigger chunk into the cache, hoping that the next chunk requested will be within the data stored in the disk cache memory.

Since SSDs work like memory, the search time is very low. Every memory cell can be addressed directly the search time is very low.

順便對比一下網路延時而網路IO主要延時包括以下幾部分:

伺服器響應延時 + 帶寬限制 + 網路延時 + 跳轉路由延時 + 本地接收延時

(一般為幾十到幾千毫秒,受環境干擾極大)

參考文獻:Computer Architecture

推薦閱讀:

蘋果操作系統適用什麼人群?
聯想一體機WIN10系統換WIN7系統
Linux使用sftp傳輸文件
主存管理 | 段式存儲管理方式
Linux 系統安全啟動

TAG:磁碟 | IO | 操作系統 |