《现代操作系统》(第4版)文件系统 课后习题解答
寫在前面: 本文為學習《現代操作系統》(第4版)的記錄,如有錯誤,請指正交流。
文章目錄
- 上半部分 1-22題
- 下半部分 23~42題
- 編程題部分(略)
上半部分 1-22題
解答
You can go up and down the tree as often as you want using ‘‘…’’. Some of the many paths are
/etc/passwd
/./etc/passwd
/././etc/passwd
/./././etc/passwd
/etc/…/etc/passwd
/etc/…/etc/…/etc/passwd
/etc/…/etc/…/etc/…/etc/passwd
/etc/…/etc/…/etc/…/etc/…/etc/passwd
解答:
Windows的方法是使用文件擴展名。每個擴展名對應于一個文件類型和一些處理該類型的程序。
另一種方法是記住是哪個程序創建了文件并運行該程序。Macintosh就是這樣工作的。
The Windows way is to use the file extension. Each extension corresponds to a file type and to some program that handles that type. Another way is to remember which program created the file and run that program. The Macintosh works this way.
解答:
這些系統直接將程序加載到內存中,并從字0(魔數)開始執行。為了避免試圖將文件頭作為代碼執行,魔數是一個分支指令,目標地址正好位于文件頭的正上方。這樣就可以直接將二進制文件讀入新進程的地址空間,并在0運行它,甚至不用知道文件頭有多大。筆者注:換句話說,魔數為了跳過文件頭,直接跳轉到代碼部分執行。
These systems loaded the program directly in memory and began executing at word 0, which was the magic number. To avoid trying to execute the header as code, the magic number was a BRANCH instruction with a target address just above the header. In this way it was possible to read the binary file directly into the new process’ address space and run it at 0, without even knowing how big the header was.
解答:
首先,如果沒有打開,每次讀取時都需要指定要打開的文件的名稱。然后,系統將必須為它獲取i節點,盡管它可以被緩存。一個很快出現的問題是何時將i節點存回磁盤。不過,它可能會超時。這會有點笨拙,但可能會奏效。
筆者注: 每次讀取都要制定文件名稱,系統需要為它獲取i結點;存回磁盤可能超時。
這里復習一下open 調用的目的: 把文件屬性和磁盤地址表裝入內存,便于后續調用的快速訪問。
i結點的作用是什么呢? i結點 里出來文件屬性和文件塊的磁盤地址,給定i結點,就能找到文件的所有塊。
To start with, if there were no open, on every read it would be necessary to specify the name of the file to be opened. The system would then have to fetch the i-node for it, although that could be cached. One issue that quickly arises is when to flush the i-node back to disk. It could time out, however. It would be a bit clumsy, but it might work.
解答:不。如果你想再次讀取文件,只需隨機訪問字節0。
筆者補充:順序存儲有一個回繞操作,是因為要訪問前面的地址時,需要返回到起點(回繞)從頭開始遍歷。
No. If you want to read the file again, just randomly access byte 0.
解答:
rename調用不會更改創建時間或上次修改的時間,但創建新文件會使它獲得當前時間,即創建時間和上次修改時間都會發生改變。另外,如果磁盤快滿了,復制可能會失敗。
Yes. The rename call does not change the creation time or the time of last modification, but creating a new file causes it to get the current time as both the creation time and the time of last modification. Also, if the disk is nearly full, the copy might fail.
解答:(思考思考)
文件的映射部分必須從頁邊界開始,長度為整數頁。每個映射頁都將文件本身用作后備存儲。未映射內存使用暫存文件或分區作為備份存儲。
The mapped portion of the file must start at a page boundary and be an integral number of pages in length. Each mapped page uses the file itself as backing store. Unmapped memory uses a scratch file or partition as backing store.
解答:(思考思考)
文件命名的時候使用 路徑!!! 妙啊。
使用文件名,如/usr/ast/file。雖然它看起來像是一個層次化的路徑名,但實際上它只是一個包含嵌入斜杠的名稱。
Use file names such as /usr/ast/file. While it looks like a hierarchical path name, it is really just a single name containing embedded slashes.
解答:(思考思考)
一種方法是向read系統調用添加一個額外的參數,該參數指示要從哪個地址讀取。實際上,每次讀取都有可能在文件中執行查找。這種方案的缺點是(1)在每次讀取調用中都會增加一個參數,(2)要求用戶跟蹤文件指針的位置。
One way is to add an extra parameter to the read system call that tells what address to read from. In effect, every read then has a potential for doing a seek within the file. The disadvantages of this scheme are (1) an extra parameter in every read call, and (2) requiring the user to keep track of where the file pointer is.
解答
". ."組件將搜索移動到/usr,因此…/ast將其放入/usr/ast中。
因此…/ast/x與/usr/ast/x相同。
The dotdot component moves the search to /usr, so …/ast puts it in /usr/ast.
Thus …/ast/x is the same as /usr/ast/x.
11
解答
由于浪費的存儲在分配單元(文件)之間,而不是在它們內部,這是外部碎片。它與交換系統或使用純分段的系統發生的主存外部碎片化完全相似。
Since the wasted storage is between the allocation units (files), not inside them, this is external fragmentation. It is precisely analogous to the external fragmentation of main memory that occurs with a swapping system or a system using pure segmentation.
解答
如果一個數據塊在一個連續的分配系統中被損壞,那么只有這個塊會受到影響;文件的其余塊可以被讀取。在鏈接分配的情況下,無法讀取損壞的塊;此外,有關從該損壞塊開始的所有塊的位置數據也將丟失。在索引分配的情況下,只影響損壞的數據塊。
If a data block gets corrupted in a contiguous allocation system, then only this block is affected; the remainder of the file’s blocks can be read. In the case of linked allocation, the corrupted block cannot be read; also, location data about all blocks starting from this corrupted block is lost. In case of indexed allocation,only the corrupted data block is affected.
解答
開始傳輸需要9毫秒。以80 MB/秒的傳輸速率讀取2^13字節需要0.0977毫秒,總共9.0977毫秒。寫回去還需要9.0977毫秒。因此,復制一個文件需要18.1954毫秒。壓縮16 GB磁盤的一半需要復制8GB的存儲空間,即2^20個文件。在每個文件18.1954毫秒時,這需要19079.25秒,即5.3個小時。很明顯,在每次刪除文件后壓縮磁盤不是一個好主意。
It takes 9 msec to start the transfer. To read 2^13 bytes at a transfer rate of 80 MB/sec requires 0.0977 msec, for a total of 9.0977 msec. Writing it back takes another 9.0977 msec. Thus, copying a file takes 18.1954 msec. To compact half of a 16-GB disk would involve copying 8 GB of storage, which is 2^20 files. At 18.1954 msec per file, this takes 19,079.25 sec, which is 5.3 hours.Clearly, compacting the disk after every file removal is not a great idea.
解答:
如果做得對,是有用的。在壓縮時,每個文件都應該組織起來,使其所有的塊都是連續的,以便快速訪問。Windows有一個程序可以對磁盤進行碎片整理和重新組織。鼓勵用戶定期運行它以提高系統性能。但考慮到它需要多長時間,一個月運行一次可能是一個不錯的頻率。
If done right, yes. While compacting, each file should be organized so that all of its blocks are consecutive, for fast access. Windows has a program that defragments and reorganizes the disk. Users are encouraged to run it periodically to improve system performance. But given how long it takes, running once a month might be a good frequency.
解答
數碼照相機按順序在非易失性存儲介質(例如,閃存)上記錄一些照片。當相機復位時,介質被清空。此后,按順序一次記錄一張照片,直到介質已滿,此時將照片上載到硬盤上。對于這個應用來說,攝像機內部的一個連續的文件系統(例如圖片存儲介質)是理想的。
A digital still camera records some number of photographs in sequence on a nonvolatile storage medium (e.g., flash memory). When the camera is reset, the medium is emptied. Thereafter, pictures are recorded one at a time in sequence until the medium is full, at which time they are uploaded to a hard disk. For this application, a contiguous file system inside the camera (e.g., on the picture storage medium) is ideal.
解答:(沒看懂啊)
自己做:
10個直接地址, 1024KB /4B=256K個間接地址, 總共 256*1024+10=262154 個塊, 文件最大是 262154 * 1024KB =256GB
以下是原答案:
這題答案好像不對!!!數據不對。
間接塊可以保存128個磁盤地址。加上10個直接磁盤地址,最大文件有138個塊。因為每個塊是1MB,所以最大的文件是138MB。
The indirect block can hold 128 disk addresses. Together with the 10 direct disk addresses, the maximum file has 138 blocks. Since each block is 1 MB, the largest file is 138 MB.
解答
對于隨機訪問,table/indexed和continuous都是合適的,而linkedallocation則不是,因為它通常需要對一個給定的記錄進行多次磁盤讀取。
For random access, table/indexed and contiguous will be both appropriate, while linked allocation is not ,as it typically requires multiple disk reads for a given record.
解答
由于文件大小變化很大,連續分配將是低效的,當文件增大時需要重新分配磁盤空間,當文件縮小時壓縮空閑塊。鏈接和表/索引分配都是有效的;在這兩者之間,表/索引分配對于隨機訪問場景將更有效。
Since the file size changes a lot, contiguous allocation will be inefficient requiring reallocation of disk space as the file grows in size and compaction of free blocks as the file shrinks in size. Both linked and table/indexed allocation will be efficient; between the two, table/indexed allocation will be more efficient for random-access scenarios.
解答
必須有一種方法來指示地址塊指針保存數據。如果屬性中有一點剩余空間,就可以使用它。這就把所有九個指針都留給了數據。如果每個指針都是k字節,則存儲的文件最長可達9k字節。如果屬性中沒有剩余的位,則第一個磁盤地址可以保存一個無效地址,以將以下字節標記為數據而不是指針。在這種情況下,最大文件是8k字節。
There must be a way to signal that the address-block pointers hold data, rather than pointers. If there is a bit left over somewhere among the attributes, it can be used. This leaves all nine pointers for data. If the pointers are k bytes each, the stored file could be up to 9k bytes long. If no bit is left over among the attributes, the first disk address can hold an invalid address to mark the following bytes as data rather than pointers. In that case, the maximum file is 8k bytes.
解答
埃莉諾說得對。在表中同時擁有兩個i-node副本是一個災難,除非這兩個副本都是只讀的。最壞的情況是兩者同時更新。當i節點被寫回磁盤時,無論哪個最后寫入的節點都將刪除另一個節點所做的更改,磁盤塊將丟失。
Elinor is right. Having two copies of the i-node in the table at the same time is a disaster, unless both are read only. The worst case is when both are being updated simultaneously. When the i-nodes are written back to the disk, whichever one gets written last will erase the changes made by the other one, and disk blocks will be lost.
解答
硬鏈接不需要任何額外的磁盤空間,只需要i節點中的一個計數器來跟蹤有多少磁盤空間。符號鏈接需要空間來存儲指向的文件名。
符號鏈接可以指向其他計算機上的文件,甚至可以通過Internet。硬鏈接僅限于指向自己分區內的文件。
Hard links do not require any extra disk space, just a counter in the i-node to keep track of how many there are. Symbolic links need space to store the name of the file pointed to. Symbolic links can point to files on other machines,ev en over the Internet. Hard links are restricted to pointing to files within their own partition.
解答
給定文件的硬鏈接的所有目錄項都指向單個i節點。
在軟鏈接的情況下,為軟鏈接創建一個新的i節點,該i節點基本上指向要鏈接的原始文件。
A single i-node is pointed to by all directory entries of hard links for a given file. In the case of soft-links, a new i-node is created for the soft link and this i-node essentially points to the original file being linked.
下半部分 23~42題
解答
磁盤上的塊數=4 TB/4 KB=2^30個。因此,每個塊地址可以是32位(4字節),最接近的2次冪。因此,每個塊可以存儲4KB/4B=1024個地址。
筆者理解: 最接近的2次冪,是最小的能夠滿足尋址的位數。 算出來每個塊地址的大小是4B, 一個塊大小 是4KB , 問:一個塊可以存放多少地址? 小學數學。
The number of blocks on the disk = 4 TB / 4 KB = 2^30. Thus, each block address can be 32 bits (4 bytes), the nearest power of 2. Thus, each block can store 4 KB / 4 = 1024 addresses.
解答
位圖需要B位。自由列表需要DF位。如果DF<B,則空閑列表需要更少的比特。或者,如果F/B<1/D,則空閑列表更短,其中F/B是空閑塊的分數。對于16位磁盤地址,如果有6%(1/16=6.25%)或更少的磁盤可用,可用列表會更短。
評注:只要磁盤快滿的時候,空閑塊鏈表方案需要的塊才比位圖少。
The bitmap requires B bits. The free list requires DF bits. The free list requires fewer bits if DF < B. Alternatively, the free list is shorter if F/B < 1/D, where F/B is the fraction of blocks free. For 16-bit disk addresses, the free list is shorter if 6% or less of the disk is free.
解答
The beginning of the bitmap looks like:
(a) After writing file B: 1111 1111 1111 0000
(b) After deleting file A: 1000 0001 1111 0000
( c) After writing file C: 1111 1111 1111 1100
(d) After deleting file B: 1111 1110 0000 1100
評注:本題可以看出 每次需要寫入文件時,都是從最小編號的盤塊開始尋找空閑塊,比如這里的C.
解答
這根本不是一個嚴重的問題。修復很簡單,只是需要時間。恢復算法是將所有文件中的所有塊弄一個列表,并將剩下的地方作為新的空閑列表。
在UNIX中,這可以通過掃描所有i節點來完成。
在FAT文件系統中,由于沒有空閑列表,所以不會出現問題。但即使有,要恢復它所要做的就是掃描FAT,尋找空閑的條目。
It is not a serious problem at all. Repair is straightforward; it just takes time. The recovery algorithm is to make a list of all the blocks in all the files and take the complement as the new free list. In UNIX this can be done by scanning all the i-nodes. In the FAT file system, the problem cannot occur because there is no free list. But even if there were, all that would have to be done to recover it is to scan the FAT looking for free entries.
解答
奧利的論文可能沒有如他希望那樣被備份。備份程序可能跳過當前打開以供寫入的文件,因為該文件中的數據狀態可能不確定。
簡寫:正在打開的文件沒有被備份
Ollie’s thesis may not be backed up as reliably as he might wish. A backup program may pass over a file that is currently open for writing, as the state of the data in such a file may be indeterminate.
解答
它們必須跟蹤磁盤上的文件中最后一次轉儲的時間。每次轉儲時,都會向該文件追加一個條目。在最后一次讀取文件時記錄轉儲。從那時起更改的任何文件都將被轉儲。
They must keep track of the time of the last dump in a file on disk. At every dump, an entry is appended to this file. At dump time, the file is read and the time of the last entry noted. Any file changed since that time is dumped.
解答
In (a) and (b), 21 would not be marked. In ?, there would be no change. In
(d), 21 would not be marked.
解答
許多UNIX文件都很短。如果整個文件與i 結點放在同一個塊中,那么只需要一個磁盤訪問來讀取該文件,而不是像現在這樣需要兩個磁盤訪問。即使是較長的文件也會有收益,因為需要的磁盤訪問次數會減少一次。
Many UNIX files are short. If the entire file fits in the same block as the inode, only one disk access would be needed to read the file, instead of two, as is presently the case. Even for longer files there would be a gain, since one fewer disk accesses would be needed.
問題:這個問題如何糾正?
解答
它不應該發生,但由于某個地方的錯誤,它可能發生。這意味著某些塊出現在兩個文件中,也出現在空閑列表中兩次。
修復錯誤的第一步是從空閑列表中刪除這兩個副本。
下一步必須獲取一個空閑塊,并在那里復制出現問題的塊的內容。
最后,塊在其中一個文件中的出現應該更改為引用新獲取的塊副本。在這一點上,系統再次保持一致。
It should not happen, but due to a bug somewhere it could happen. It means that some block occurs in two files and also twice in the free list. The first step in repairing the error is to remove both copies from the free list. Next a free block has to be acquired and the contents of the sick block copied there. Finally, the occurrence of the block in one of the files should be changed to refer to the newly acquired copy of the block. At this point the system is once again consistent.
解答
分成兩部分即可,命中時花費時間h* 1ms,沒有命中時需要到磁盤上讀取 ,花費時間為(1-h)*40ms
The time needed is h + 40 × (1 ? h). The plot is just a straight line.
解答
在這種情況下,最好使用通寫高速緩存,因為它會在更新緩存的同時將數據寫入硬盤驅動器。這將確保更新的文件始終位于外部硬盤驅動器上,即使用戶在磁盤同步完成之前意外拔掉了硬盤驅動器(USB)。
In this case, it is better to use a write-through cache since it writes data to the hard drive while also updating the cache. This will ensure that the updated file is always on the external hard drive even if the user accidentally removes the hard drive before disk sync is completed.
解答
塊提前讀 不管用,因為 這種情況大多不是順序存儲。
The block read-ahead technique reads blocks sequentially, ahead of their use, in order to improve performance. In this application, the records will likely not be accessed sequentially since the user can input any student ID at a given instant. Thus, the read-ahead technique will not be very useful in this scenario.
解答
實質是兩個鏈表,以-1結尾。
The blocks allotted to f1 are: 22, 19, 15, 17, 21.
The blocks allotted to f2 are: 16, 23, 14, 18, 20.
解答:
磁盤讀取時間= 尋道時間+旋轉延遲+傳送時間 ,其中旋轉延遲= 1/2*轉一圈的時間
rpm是Revolutions Per Minute的縮寫,即轉每分,表示設備每分鐘的旋轉次數,題目中是15000 rpm(轉/分鐘), 除以60得到 250 轉/秒 則 1/250 *1000 = 4ms /轉
自己來做:
在15000轉/分時,磁盤需要4毫秒才能轉一圈。讀取k字節的平均訪問時間(毫秒)是8+2+(k/262144)×4。對于1kb、2kb和4kb的塊,訪問時間分別約為10.0153毫秒、10.0305毫秒和10.0610毫秒(幾乎沒有任何不同)。這些速率分別為99.8472kb/sec、199.3919kb/sec和397.5748kb/sec。
經過計算得到的結果,傳輸速率和磁盤塊大小正相關,多多少少符合下圖虛線 磁盤數據率–塊大小關系圖。
注:
答案給的解析:其中假設磁盤平均尋道時間是6ms,轉速是15000rpm,每道 1048576 字節,和這道題有出入,其實算是另外一道題目。
在15000轉/分時,磁盤需要4毫秒才能轉一圈。讀取k字節的平均訪問時間(毫秒)是6+2+(k/1048576)×4。對于1kb、2kb和4kb的塊,訪問時間分別約為6.0039毫秒、6.0078毫秒和6.0156毫秒(幾乎沒有任何不同)。這些速率分別為170.556kb/sec、340.890kb/sec和680.896kb/sec。
At 15,000 rpm, the disk takes 4 msec to go around once. The average access time (in msec) to read k bytes is then 6 + 2 + (k/1, 048, 576) × 4. For blocks of 1 KB, 2 KB, and 4 KB, the access times are about 6.0039 msec, 6.0078 msec, and 6.0156 msec, respectively (hardly any different). These give rates of about 170.556 KB/sec, 340.890 KB/sec, and 680.896 KB/sec, respectively.
解答
自己做:
50% ,真正的系統所浪費的空間比這個小,因為實際文件大小 存在很大大文件。
注:答案給的是其他的數據計算的結果,塊的大小為4KB。
如果所有文件都是1KB,那么每個4KB塊將包含一個文件和3KB浪費的空間。不允許嘗試將兩個文件放入一個塊中,因為用來跟蹤數據的單位是塊,而不是半塊。這導致75%浪費空間。實際上,每個文件系統都有大文件以及很多小文件,這些文件使用磁盤的效率更高。例如,考慮到空間效率,32769字節的文件將使用9個磁盤塊進行存儲其中32769/36864個,占89%。
If all files were 1 KB, then each 4-KB block would contain one file and 3 KB of wasted space. Trying to put two files in a block is not allowed because the unit used to keep track of data is the block, not the semiblock. This leads to 75% wasted space. In practice, every file system has large files as well as many small ones, and these files use the disk much more efficiently. For example, a 32,769-byte file would use 9 disk blocks for storage, given a space efficiency of 32,769/36,864, which is about 89%.
解答
間接塊可以保存1024個地址(每個間接塊 大小 4KB,指針地址值4B,一個間接塊可以有 4KB/4B=1K=1024個地址)。加上10個直接地址,總共有1034個地址。因為每個文件都指向一個4KB的磁盤塊,所以最大的文件是4235264字節
The indirect block can hold 1024 addresses. Added to the 10 direct addresses,there are 1034 addresses in all. Since each one points to a 4-KB disk block, the largest file is 4,235,264 bytes
解答
它將所有文件長度之和限制為不大于磁盤。這不是一個非常嚴重的限制。如果這些文件總的比磁盤大,就沒有地方將它們全部存儲在磁盤上。
It constrains the sum of all the file lengths to being no larger than the disk. This is not a very serious constraint. If the files were collectively larger than the disk, there would be no place to store all of them on the disk.
解答
i節點有10個指針。單個間接塊包含1024個指針。雙間接塊有1024 ^2個指針。三重間接塊有1024 ^3個指針。加起來,我們得到的最大文件大小為1074791434個塊,大約是4100.0039GB=4.0039TB
The i-node holds 10 pointers. The single indirect block holds 1024 pointers. The double indirect block is good for 1024^2 pointers. The triple indirect block is good for 1024^3 pointers. Adding these up, we get a maximum file size of 1,074,791,434 blocks, which is about 16.06 GB.
解答
The following disk reads are needed:
directory for /
i-node for /usr
directory for /usr
i-node for /usr/ast
directory for /usr/ast
i-node for /usr/ast/courses
directory for /usr/ast/courses
i-node for /usr/ast/courses/os
directory for /usr/ast/courses/os
i-node for /usr/ast/courses/os/handout.t
In total, 10 disk reads are required.
解答
一些優點如下。
首先,沒有磁盤空間浪費在未使用的i節點上。
其次,不可能耗盡i節點。
第三,由于i節點和初始數據可以在一次操作中讀取,所以需要較少的磁盤移動。
一些缺點如下。
首先,目錄條目現在需要一個32位的磁盤地址,而不是16位的i節點號。
其次,整個磁盤將用于不包含數據的文件(空文件、設備文件)。
第三,由于需要為每個i節點讀取整個塊,而且i節點將分散在整個磁盤上,因此文件系統完整性檢查的速度將變慢。
第四,由于i節點的原因,那些大小經過精心設計以適合塊大小的文件將不再適合塊大小,從而擾亂了性能
Some pros are as follows. First, no disk space is wasted on unused i-nodes. Second, it is not possible to run out of i-nodes. Third, less disk movement is needed since the i-node and the initial data can be read in one operation. Some cons are as follows. First, directory entries will now need a 32-bit disk address instead of a 16-bit i-node number. Second, an entire disk will be used even for files which contain no data (empty files, device files). Third, file system integrity checks will be slower because of the need to read an entire block for each i-node and because i-nodes will be scattered all over the disk.Fourth, files whose size has been carefully designed to fit the block size will no longer fit the block size due to the i-node, messing up performance
編程題部分(略)
總結
以上是生活随笔為你收集整理的《现代操作系统》(第4版)文件系统 课后习题解答的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一道时间复杂度的题目
- 下一篇: 《现代操作系统》第1章读书笔记-- 引论