Virtual memory
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
Every running program believes it has the computer's memory to itself, starting at address 0. That private view is virtual memory. The MMU, a part of the processor, translates each virtual address into a real place in RAM: it cuts memory into 4 KiB pages, looks each page up in the program's own page table, and keeps recent answers in a tiny cache called the TLB. Watch two programs use the very same address and land in different frames, a missing page come in from disk after a page fault, and a write to a read-only page get stopped. Then see why this keeps programs apart and lets them use more memory than the RAM has.
5 أسئلة سريعة
اختر إجابة لتعرف السبب. تُحفَظ نتيجتك مع تقدّمك.
01Programs A and B both read virtual address 0x1234. What happens?
Each program has its own page table. A's table maps page 1 to frame 3 (0x3234) and B's maps page 1 to frame 2 (0x2234), so the same virtual address lands in different real memory.
02Pages are 4 KiB (4,096 bytes). What are the page number and the offset of virtual address 0x2010?
4,096 = 2¹², so the low 12 bits (the last three hex digits, 0x010) are the offset and the rest (2) is the page number.
03What does the TLB store?
The TLB (translation lookaside buffer) is a small, fast cache of recent page-to-frame translations, so most accesses skip the trip to the page table in RAM.
04A program reads a page whose table entry says it is not in RAM. What happens next?
The MMU raises a page fault. The operating system copies the page from disk into a free frame, writes that frame into the table entry and runs the instruction again, which now succeeds.
05Why can't program B read program A's memory?
A program can only reach frames that its own page table points to. B's table never points at A's frames, and protection bits limit what B may do even with its own pages.
المسرد
- Virtual address
- An address as a program sees it, in its own private address space. The MMU translates it to a physical address in RAM.
- Physical address
- A real location in RAM.
- Page and frame
- Memory is managed in fixed-size blocks, usually 4 KiB (4,096 bytes): a page is a block of virtual memory, a frame is a block of RAM of the same size.
- Page table
- A list kept in RAM by the operating system, one per program, that says which frame each of the program's pages is in (or that it is on disk), plus its protection bits.
- MMU
- Memory management unit: the part of the processor that translates every virtual address into a physical address.
- TLB
- Translation lookaside buffer: a small, fast cache inside the MMU that keeps recent page-to-frame translations.
- Page fault
- The alarm the MMU raises when a page is not in RAM (or an access is not allowed); the operating system then handles it.
- Protection bits
- Bits in each page-table entry that say whether the page may be read, written or run as code.
الملاحظات والمصادر
- Each process has its own virtual address space and its own page table; memory is managed in fixed-size pages (virtual) and frames (physical), and the page offset is copied unchanged while the page number is translated (Patterson and Hennessy, Computer Organization and Design, section 5.7; Arpaci-Dusseau, Operating Systems: Three Easy Pieces, chapter 18).
- x86-64 processors use 4-KByte pages as their base page size (Intel SDM, volume 3A, chapter 4), and Linux uses 4 KiB pages on x86-64. Python reports the size as mmap.PAGESIZE. Some systems use larger pages.
- A TLB is a small cache of recent translations. Typical values: 16 to 512 entries, a hit in 0.5 to 1 clock cycle, a miss costing 10 to 100 cycles, and a miss rate of 0.01% to 1% (Patterson and Hennessy, section 5.7).
- When a page-table entry is marked not present, the access raises a page fault; the operating system loads the page from disk (swap or the program's file), updates the entry and restarts the instruction. A disk access takes millions of clock cycles, so page faults must be rare (Patterson and Hennessy, section 5.7; Three Easy Pieces, chapter 21).
- Page-table entries carry permission bits; on x86-64 they include read/write, user/supervisor and execute-disable. An access that breaks them raises a page-fault exception, and on Linux the program is usually stopped with a segmentation fault (Intel SDM, volume 3A, sections 4.6 and 4.7).
- Patterson and Hennessy, Computer Organization and Design, RISC-V Edition (section 5.7: virtual memory)
- Arpaci-Dusseau, Operating Systems: Three Easy Pieces, chapter 18: Paging: Introduction
- Arpaci-Dusseau, Operating Systems: Three Easy Pieces, chapter 19: Paging: Faster Translations (TLBs)
- Arpaci-Dusseau, Operating Systems: Three Easy Pieces, chapter 21: Beyond Physical Memory: Mechanisms
- Intel 64 and IA-32 Architectures Software Developer's Manual, volume 3A, chapter 4: Paging
- The Linux kernel documentation: memory management concepts overview
- Python documentation: mmap (mmap.PAGESIZE)
- MIT OpenCourseWare 6.004 Computation Structures (virtual memory)
اختبار يتاح بعد الخطوة الأخيرة.