🎓 BookMCQ
← Back to 9. Virtual Memory

📝 Memory mapped I/O (10 MCQs)

Accessing Device Registers Through Memory Addresses

Memory-mapped I/O is a hardware and operating system technique in which device registers are assigned addresses within the processor's address space. The CPU communicates with a device by performing loads and stores to those addresses. The memory controller or interconnect routes the transactions to the appropriate device rather than ordinary RAM. The kernel configures access permissions and memory attributes for safe device communication.

Why Device Registers Need a CPU-Accessible Interface

Peripheral devices must receive commands, provide status information, and exchange data with processors. Memory-mapped I/O offers a consistent address-based interface for these operations. It allows device registers to be accessed using ordinary processor load and store instructions, although device-specific rules still apply. The operating system must prevent untrusted applications from modifying privileged device-control locations.

Register Access and Hardware Transaction Sequence

The device driver obtains an appropriate device-register mapping and accesses documented register offsets. A store may issue a command or provide data, while a load may retrieve status or input information. The hardware routes the transaction to the device. Correct operation may require memory barriers, ordering rules, and specific mapping attributes to prevent unsafe caching or reordering.

Writing a Command to a Device Register

Suppose a device exposes a control register at mapped address 0xF0001000. A driver writes a command value to that register to initiate an operation. The device interprets the write according to its register specification, and the driver may later read a status register to determine completion. These addresses identify hardware registers rather than ordinary stored application data.

Device Communication and Memory-Ordering Constraints

Memory-mapped I/O simplifies hardware interfaces and supports efficient register access. However, device registers can have side effects, making ordinary cache behaviour inappropriate. Drivers must use suitable memory attributes and ordering barriers where required by the architecture. Incorrect accesses can issue unintended commands or observe stale status. Port-mapped I/O remains a separate alternative on architectures that support distinct I/O address spaces.

Common Misconceptions About Memory-Mapped Devices

'Every memory-mapped address refers to ordinary RAM' is incorrect because device addresses can access hardware registers. 'Device-register writes can always be cached safely' is incorrect because registers may have immediate side effects. 'Memory-mapped I/O and memory-mapped files are identical' is incorrect because one accesses device registers while the other maps file contents.

Exam Focus: Device Registers and I/O Addressing

Explain how processor loads and stores can communicate with device registers. Distinguish memory-mapped I/O from port-mapped I/O and ordinary memory-mapped files. Identify why device drivers require suitable access permissions, memory attributes, and ordering barriers. Given a register address and command value, trace the transaction between the CPU and peripheral controller.

Key Terms of Memory-Mapped I/O

  • MMIO - Accesses devices through memory addresses.
  • Device register - Hardware control or status location.
  • Device driver - Controls peripheral operations.
  • Memory barrier - Enforces operation ordering.
  • Port-mapped I/O - Uses a separate I/O address space.
  • Memory attribute - Defines access and caching behaviour.
2
Easy
5
Medium
3
Hard

📝 All Memory mapped I/O MCQs

Q1. A child process created by fork() needs to write to a stack page that is marked copy‑on‑write. Which action ensures the parent’s stack remains unchanged?

A.Modify the original page directly
B.The OS allocates a new zero‑filled page and maps it to the child ✅
C.The child shares the same physical page
D.The OS disables copy‑on‑write for that page
💡 Difficulty: easy | ✅ Correct: B

📖 Explanation: When a page is copy‑on‑write, the operating system creates a fresh zero‑filled page for the writing process. Mapping this new page to the child allows the child to modify it without affecting the parent’s copy. Option B describes this behavior. Option A would alter the shared page, breaking isolation. Option C keeps the same page, so changes would appear in both processes. Option D removes the copy‑on‑write protection, which is unnecessary and would not preserve the parent’s data.

Q2. A system has a limited pool of free pages for copy‑on‑write operations. If two processes simultaneously fork, how should the OS allocate pages to avoid running out of free pages?

A.Allocate one shared free page for both children
B.Allocate separate free pages from the pool for each child
C.Delay the second fork until the first child releases its page
D.Reuse the parent’s original pages for the children ✅
💡 Difficulty: medium | ✅ Correct: D

📖 Explanation: The OS must keep a separate physical page for each child that modifies a copy‑on‑write page, so it allocates distinct pages from the free pool (Option D). Reusing the parent’s pages would break isolation, and sharing a single page would cause both children to see each other’s changes. Delaying the fork is unnecessary; the OS can manage the pool efficiently. Thus, allocating separate pages preserves correctness and avoids depletion.

Q3. During a vfork() call, the child shares the parent’s address space until exec or exit. Which scenario correctly describes a safe use of vfork()?

A.The child writes to a read‑only code page
B.The child modifies a copy‑on‑write stack page ✅
C.The child performs I/O on a shared file descriptor
D.The child allocates a new heap page before exec
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: vfork() is designed for efficiency, but the child must not modify shared pages that could affect the parent. However, modifying a copy‑on‑write stack page triggers the OS to create a private copy, making it safe (Option B). Writing to a read‑only code page would cause a fault, and performing I/O could interfere with the parent’s state. Allocating a new heap page without exec would still share the parent’s memory, risking corruption.

Q4. Assume a system uses zero‑fill‑on‑demand for new pages. If a process requests a new stack page, what initial content will the page contain?

A.All bits set to 1
B.A random pattern from previous memory
C.All bits set to 0 ✅
D.The contents of the parent’s stack page
💡 Difficulty: hard | ✅ Correct: C

📖 Explanation: Zero‑fill‑on‑demand guarantees that any newly allocated physical page is cleared to zeros before use, providing a clean slate for the process. Therefore the page will contain all zeros (Option C). A page filled with ones or a random pattern would expose previous data, violating security. Copying the parent’s stack page would defeat the purpose of zero‑fill and could leak information.

Q5. A kernel must decide whether to mark a newly loaded page as copy‑on‑write or read‑only. Which property of the page determines the correct marking?

A.Whether the page resides in the heap
B.Whether the page contains executable code ✅
C.Whether the page is larger than 4 KB
D.Whether the page has been accessed recently
💡 Difficulty: hard | ✅ Correct: B

📖 Explanation: Pages that contain executable code are immutable during normal execution, so they can be shared as read‑only, avoiding unnecessary copies. Thus, the presence of executable code (Option B) dictates a read‑only marking. Heap pages are typically writable and may need copy‑on‑write. Page size and recent access do not directly influence the copy‑on‑write decision.

Q6. Why does copy‑on‑write improve memory utilization when a process forks?

A.It duplicates all pages immediately
B.It shares unmodified pages between parent and child ✅
C.It compresses pages to save space
D.It writes pages to disk before sharing
💡 Difficulty: easy | ✅ Correct: B

📖 Explanation: Copy‑on‑write allows the parent and child to share the same physical pages until one of them writes to a page. Unmodified pages remain shared, reducing the number of duplicated pages and conserving memory. Option B captures this benefit. Immediate duplication (Option A) would double memory usage, compression (Option C) is unrelated, and writing pages to disk (Option D) would degrade performance.

Q7. How does the zero‑fill‑on‑demand technique contribute to system security?

A.It encrypts newly allocated pages ✅
B.It ensures no residual data from previous processes remains
C.It speeds up page allocation
D.It allows pages to be shared across processes
💡 Difficulty: medium | ✅ Correct: A

📖 Explanation: Zero‑fill‑on‑demand wipes a page to all zeros before handing it to a process, preventing any leftover data from prior owners from being exposed. This protects confidentiality. Encryption (Option A) is not part of zero‑fill. While it may slightly affect allocation speed (Option C), the primary security benefit is the removal of residual data, making Option B correct.

Q8. What is the main reason that executable code pages are not marked copy‑on‑write after a fork?

A.They are never accessed by the child
B.They are read‑only and do not need duplication ✅
C.They are larger than data pages
D.They are stored in a special cache
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: Executable code pages are inherently read‑only during execution, so there is no risk of modification. Marking them copy‑on‑write would be unnecessary overhead. Therefore, they are simply shared as read‑only (Option B). The child may still execute the code (contradicting Option A). Page size (Option C) and special caches (Option D) are irrelevant to the copy‑on‑write decision for code.

Q9. If a copy‑on‑write page is mistakenly marked as writable in both parent and child, what adverse effect is most likely?

A.Both processes will see identical updates
B.The parent’s memory may be corrupted by the child’s writes ✅
C.The child will be unable to write to the page
D.The system will automatically correct the marking
💡 Difficulty: hard | ✅ Correct: B

📖 Explanation: When a page intended for copy‑on‑write is incorrectly writable, the child’s modifications affect the same physical page as the parent, leading to unintended sharing and possible corruption of the parent’s data. Option B describes this risk. Identical updates (Option A) are expected under correct copy‑on‑write, not a problem. The child’s inability to write (Option C) contradicts the mistaken writable marking, and the OS does not automatically fix such errors (Option D).

Q10. Which term describes the allocation method that provides a fresh page filled with zeros before it is mapped into a process’s address space?

A.Lazy allocation ✅
B.Zero‑fill‑on‑demand
C.Demand paging
D.Page swapping
💡 Difficulty: medium | ✅ Correct: A

📖 Explanation: Zero‑fill‑on‑demand explicitly refers to the practice of zeroing out a newly allocated physical page before it becomes visible to a process. This ensures no leftover data is exposed. Lazy allocation (Option A) delays allocation, demand paging (Option C) brings pages from storage, and page swapping (Option D) moves pages to disk. Only Option B matches the definition.

🔗 Related Topics (MCQs)