MP4

Disk.h:
Data structure to emulate a physical disk.
Accept (one at once) requests to R/W a disk sector.
When the request is satisfied, the CPU gets an interrupt. And the next request can be sent to the disk.

Disk中的資料在machine crash後仍可維持
但若FS operation做到一半更動的資料可能會遺失

Addressing is by sector number -- each sector on the disk is givena unique number:
track * SectorsPerTrack + offset within a track.

As with other I/O devices, the raw physical disk is an asynchronous device --
requests to read or write portions of the disk return immediately, and an interrupt is invoked later to signal that the operation completed.

***
Disk 的模擬是透過UNIX file
也就是說NachOS的Disk是Unix的一個file
在NachOS上要把資料存到disk上
其實就是把該資料存到UNIX中那一個叫做disk的file之中
***



 我覺得這個Class比較像是一個interface
在NachOS run 的時候
來開啟UNIX上叫做disk的file(此file模擬NachOS的disk 也就是所有NachOS資料要存的地方)
並透過各種function進行NachOS上file的操作讀寫

disk.cc
//      Routines to simulate a physical disk device; reading and writing
//      to the disk is simulated as reading and writing to a UNIX file.


Disk類別的物件呼叫建構函式的時候
會去開啟叫做diskname的file(此file就是UNIX上的file)
若有拿到fd則會再去檢查該file的第一筆數字是不是MagicNumber
若是則表示沒問題 這個file是當作NachOS disk storage的檔案沒有錯(否則ASSERTION)

若沒拿到fd則去create一個UNIX file
然後先在這個file寫上一個MagicNumber
並用Lseek跳到該file第DiskSize-sizeof(int)的位置
寫一個0





去呼叫ComputeLatency是要去模擬disk去做R/W要花多久時間
由於這是一個asynchronous的disk
ReadRequest這個function跑完CPU就會去做其他事情
我們要用interrupt來notify CPU甚麼時候IO會完成
故會call kernel->interrupt->Schedule(this, ticks, DiskInt)

用Lseek去找要R/W的sector然後去做R/W

interrupt發起會去call callbackobject 將 active設定成FALSE


bitmap.h
用bitmap這個data structure來管理disk上共有那些sector被用那些sector是free的
由於C這個語言沒有bit這個type
故我們用unsigned int這個type中的每一個bit來做
依照這個邏輯來做我們會有numBits = numWords*BitsInWord
numBits跟sector的數量一樣


pbitmap.h

繼承bitmap


filesys.cc


sector 0和1用來擺放bitmap 跟 directory的file header
bitmap的filesize為128/8 = 16bytes(因為現在disk上面只有128個sector又bitmap適用unsigned int來表示bit array 共需要4個unsigned int來表示)
目前檔案系統只能有十個directory 也就是只能有十個檔案
(why?)

留言

熱門文章