NivaarExam PrepOfficial exam papers ↗

25-Comp-A5 Operating Systems · May 2017

Question 4 of 7: File Allocation and Free-Space Management

Nivaar worked solution (AI-drafted; not reviewed by a licensed engineer)

Notes on this paper

98-COMP A-5 Operating Systems — National Examinations, May 2017. 3 hours, closed book (one approved pocket calculator only). Candidates were instructed to answer any five of the seven questions; all seven are answered below as a complete study resource.

Reference texts: Silberschatz, Galvin & Gagne, Operating System Concepts (10th ed.) — CPU scheduling (ch. 5), process synchronization/monitors (ch. 6–7), deadlocks (ch. 8), memory management/paging (ch. 9–10), mass-storage/file-system implementation and disk scheduling (ch. 11–12); Tanenbaum, Modern Operating Systems (5th ed.), corroborating chapters on scheduling, virtual memory and file systems.

Question 4: File Allocation and Free-Space Management (20 marks)

Question text not reproduced: the examination questions are © Engineers and Geoscientists BC. Open the official past paper (linked at the top of this page) to read the question, then follow the worked solution below.

(a) Given. A contiguously-allocated file of 120 blocks (numbered 1–120); directory resident in memory; room to grow only at the end. Find. Minimum disk operations for cases A–D. Approach. Under contiguous allocation, block $k$'s disk location is computable directly from the file's known start address (no traversal needed), so a simple read/write of an UNMOVED block costs exactly one disk operation each; inserting or deleting a block, however, forces every block after the insertion/deletion point to physically shift by one position (since the blocks must remain contiguous), each shifted block costing one read + one write.

  1. (A) Read three unrelated blocks. Blocks 101, 97, 102 are each read independently with no shifting required (a pure read case). $$\boxed{\text{Case A}=3\ \text{disk operations}}$$
  2. (B) Exchange block 100 and block 95. Read block 100, read block 95 (2 reads), then write block 95's original content into block 100's location and block 100's original content into block 95's location (2 writes). $$\boxed{\text{Case B}=2+2=4\ \text{disk operations}}$$
  3. (C) Insert a new block after block 70 (content = a copy of block 100's content). Because there is no room to grow at the beginning, every block from 71 through 120 (that's $120-71+1=50$ blocks) must shift one position to the right (71→72, 72→73, …, 120→121) to open up position 71 for the new block, and this shift must proceed from the HIGHEST index down to 71 to avoid overwriting a block before it has been read. That is 50 reads + 50 writes. The content needed for the new block (a copy of the ORIGINAL block 100) is already produced for free as a side effect of the shift step "read block 100 → write to block 101," so writing that same already-in-memory content into the new position 71 costs one more write, with no extra read. $$\boxed{\text{Case C}=(50\ \text{reads}+50\ \text{writes})+1\ \text{write}=101\ \text{disk operations}}$$
  4. (D) Delete block 75. Blocks 76 through 120 (that's $120-76+1=45$ blocks) must each shift one position to the LEFT (76→75, 77→76, …, 120→119) to close the gap left by the deletion, proceeding from the LOWEST index up. That is 45 reads + 45 writes. $$\boxed{\text{Case D}=45+45=90\ \text{disk operations}}$$
Final Results — Q4(a)
CaseOperationMinimum disk operations
ARead blocks 101, 97, 1023
BExchange blocks 100 ↔ 954
CInsert new block after block 70101
DDelete block 7590

(b) An acyclic graph directory generalizes the strict tree-structured directory by allowing a file (or subdirectory) to have MULTIPLE parent directory entries pointing to it — i.e. the same underlying file can appear, under possibly different names, in several directories at once — while still forbidding cycles (a directory can never be its own descendant, which would break traversal and space-reclamation algorithms). It is implemented via links (a directory entry that names another file/directory rather than containing an independent copy) or by having multiple directory entries share one underlying inode/file-control-block. Use for file sharing: two users collaborating on a project can each have the shared file appear in their own directory under their own convenient name, with edits made through either link visible through the other, since both entries reference the same underlying data — no wasteful duplication of the file's content. Merits: avoids duplicate storage of shared data, keeps a single edit consistent everywhere the file is referenced, and lets each user organize shared files within their own naming scheme. Overheads: file creation is simple (just add a link), but file deletion is genuinely harder than in a strict tree: the OS must not physically free the data merely because ONE link to it is removed, since other directories may still reference it, so a reference count (or equivalent, e.g. periodic garbage collection) must be maintained per file and the actual disk space reclaimed only once the last reference is removed — naive unlink-and-free logic would leave other directories with dangling pointers. Every traversal or size-accounting utility must also guard explicitly against acyclic graphs still double-counting a multiply-linked file's size, and against the graph's cycle-freedom being violated by pathological link creation.

(c) The bit vector (bitmap) technique for free-space management represents the disk's blocks as a single array of bits, one bit per block, where a $1$ conventionally means the block is free and $0$ means it is allocated (or vice versa by convention). To allocate a block, the OS scans the bitmap for the first (or a suitably-sized run of) set bit(s), clears it, and returns that block number; to free a block, it simply sets the corresponding bit back. Advantages: the representation is extremely compact (one bit per block — a 1 TB disk with 4 KB blocks needs only about 32 MB of bitmap), finding contiguous runs of free blocks (useful for contiguous allocation, or for reducing fragmentation) is straightforward by scanning for consecutive set bits, and simple bitwise operations make the scan for a free block fast on modern hardware (e.g. finding the first set bit within a word using a hardware instruction). Shortcomings: a linear scan over the whole bitmap can still be slow on a nearly-full disk (long runs of zero-bits to skip over before finding a free block), and the ENTIRE bitmap generally needs to be resident in memory for fast access, consuming memory proportional to disk size even though most of it is never touched at once. Must it be stored on disk? Yes — keeping the bitmap only in volatile memory would lose all free-space information on a crash or power loss, so the authoritative copy must be persisted to disk (updated as allocations/frees occur, or reconstructed at boot by scanning the file system), with an in-memory cached copy used for fast day-to-day allocation decisions.