Virtual memory, paging and segmentation
แนะนำใหม่
13:59
ทรัพยากร คอมไพเลอร์ และ RPN
เปิดเบราว์เซอร์Playersเพลง และเกม คุณมีโปรเซสเซอร์เพียงตัวเดียว — หรือบางทีก็หลายคอร์ — แต่ทั้งหมดดูเหมือนจะทำงานพร้อมกัน และทั้งหมดต้องการมากกว่านั้น…
บรรยายภาษาอังกฤษ · สลับคำบรรยายภาษาอังกฤษและภาษาจีนได้
ในบทเรียนนี้
| English | ความหมาย |
|---|---|
| disk/dɪsk/ | A storage device that holds programs and data even when power is off. |
| applications/ˌæplɪˈkeɪʃnz/ | Programs designed to help users carry out particular tasks. |
| memory/ˈmeməri/ | The part of a computer that holds data and programs currently in use. |
| processor/ˈprəʊsesə/ | The component that executes instructions and performs the operations of a computer or similar system. |
| pages/ˈpeɪdʒɪz/ | หน้า |
| virtual memory/ˈvɜːtʃuːəl ˈmeməri/ | A memory-management system that maps virtual addresses to physical storage and may move pages between RAM and secondary storage. |
| paging/ˈpeɪdʒɪŋ/ | การแบ่งหน้า |
| segmentation/ˌseɡmənˈteɪʃn/ | การแบ่งส่วน |
| virtual address space/ˈvɜːtʃuːəl əˈdres speɪs/ | พื้นที่ที่อยู่ทางเสมือน |
| process/ˈprəʊses/ | An instance of a program in execution together with its execution state and allocated resources. |
| addresses | Numbers that identify specific locations in a computer's memory. |
| OS | The software that manages a computer's hardware, memory and running programs. |
| physical RAM | The actual memory hardware that holds data and instructions currently in use. |
| physical memory | The actual memory hardware that holds data and programs currently in use. |
| frames/freɪmz/ | เฟรมหน้า |
| page table/peɪdʒ ˈteɪbl/ | ตารางหน้า |
| hardware/ˈhɑːdweə/ | The physical parts of a computer system that can be touched. |
| page fault/peɪdʒ fɒlt/ | ความผิดพลาดของหน้า |
| swap file/swɒp faɪl/ | ไฟล์สลับ |
| interrupt/ˈɪntərʌpt/ | A signal or event that causes the processor to suspend normal execution and run an appropriate service routine. |
| blocked/blɒkt/ | A process state in which execution waits for an event or resource, such as I/O completion, rather than merely waiting for the CPU. |
| ready state | The state of a process that can run when the processor becomes available. |
| instruction/ɪnˈstrʌkʃn/ | A command that tells the processor what action to perform. |
| thrashing/ˈθræʃɪŋ/ | การกระเจิง |
| working set | The instructions and data a program is currently using or likely to need. |
| swapping | The movement of pages between physical memory and disk as they are needed. |
| disk thrashing/dɪsk ˈθræʃɪŋ/ | A state where the system spends more time moving pages than executing instructions. |
| logical segments | Parts of a program's memory that represent meaningful units and can differ in size. |
| code/kəʊd/ | Instructions written in a programming language for a computer to follow. |
| stack/stæk/ | A last-in, first-out data structure in which items are added and removed at the same end. |
| heap/hiːp/ | An area of memory used for data created while a program runs. |
| permissions | Rules that decide what a user can access or change. |
| code segment/kəʊd ˈseɡmənt/ | The part of a program's memory that holds its executable instructions. |
| access permissions | Rules that control whether a process can read, write or execute a memory segment. |
ไฟดิสก์ที่หมายถึงเครื่องยอมแพ้
- เปิดแอปพลิเคชันมากเกินไปในเครื่องที่มีหน่วยความจำไม่เพียงพอ จะเกิดเหตุการณ์เฉพาะตัวขึ้น Everything ช้าลงจนเกือบหยุดนิ่ง และไฟแสดงสถานะดิสก์จะดับการกระพริบและติด เปิด
- โปรเซสเซอร์ไม่ได้ทำงานหนัก มันกำลังรออยู่ เพราะหน่วยความจำที่แต่ละโปรแกรมต้องการอยู่บนดิสก์ และการดึงหน้าหนึ่งออกอาจทำให้หน้าอื่นที่ต้องใช้ทันทีถูกทิ้งไป
- เครื่องกำลังทำอะไรเลยนอกจากการย้ายหน้า มีงานพอและมีความจำพอที่จะทำทั้งสองอย่างแต่ก็ทำไม่ได้เลย
- บทเรียนนี้เกี่ยวกับ หน่วยความจำเสมือน ว่า การจัดหน้า และ การแบ่งส่วน จัดหามันอย่างไร และกลไกเดียวกันที่ทำให้หน่วยความจำดูใหญ่ขึ้นก็อาจทำให้เครื่องไร้ประโยชน์ได้
พื้นที่ที่อยู่แบบเสมือน
- แต่ละโปรเซสส์ได้รับ พื้นที่ที่อยู่แบบเสมือน (virtual address space) ของตัวเอง: ช่วงที่อยู่ต่อเนื่องกันที่สะอาด เริ่มจากศูนย์ ซึ่งระบบปฏิบัติการจะแมปไปยังตำแหน่งจริงของข้อมูล
- สิ่งที่เกิดขึ้นตามมาก็คือ Each process sees a simple private space and need not know what else is running. Processes are protected from each other, since one cannot name an address in another's space. And the total memory in use can exceed the physical RAM, because part of it lives on disk.
- That isolation is a security boundary as much as a convenience.

A benefit of virtual memory is that it:
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Virtual memory gives each process a private space and lets the working set exceed installed RAM by using disk.
What does giving each process a virtual address space achieve? Select ทั้งหมด that apply.
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Virtual memory buys simplicity, isolation and capacity. It costs speed, since part of the memory is really on disk.
การแบ่งหน้า (Paging)
- พื้นที่เสมือนถูกแบ่งเป็น หน้า (pages) ขนาดคงที่; หน่วยความจำฟิสิกซ์ถูกแบ่งเป็น เฟรม (frames) ขนาดเดียวกัน A page table บันทึกว่าแต่ละหน้าปัจจุบันอยู่ที่เฟรมใด
- เมื่อโปรเซสใช้อ้างอิงที่อยู่ Hardware จะค้นหาหน้าที่อยู่ในตารางและพบเฟรม ถ้าหน้านั้น ไม่อยู่ใน RAM จะเกิด ข้อผิดพลาดหน้า
- เมื่อเกิด page fault OS จะอ่านหน้าจาก swap file บนดิสก์เข้าสู่เฟรมว่าง หากไม่มีเฟรมว่าง ต้อง evict หน้าหนึ่งออกก่อน แล้วเขียนกลับลงดิสก์หากมีการแก้ไข

หน้าและเฟรมขนาดเท่ากัน เชื่อมต่อกันโดยตาราง
In paging, memory is divided into:
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Paging uses fixed-size pages and frames, linked by a page table. (Variable sizes are segmentation.)
A page fault occurs when:
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
The accessed page isn't in a frame, so the OS fetches it from the swap file (evicting another page if needed).
ตัวอย่างปฏิบัติ: เกิดอะไรขึ้นเมื่อเกิด page fault
- A process accesses an address whose page is not in RAM. Describe what the OS does. [4]
- Hardware ตรวจจับได้ว่าหน้าที่ไม่มีอยู่และยก สัญญาณรบกวนข้อผิดพลาดหน้า ขึ้น และโปรเซสจะถูก กักกัน
- OS finds a free frame; if there is none, it selects a page to replace, and writes it back to the swap file if it has been modified.
- It reads the required page from the swap file into that frame and updates the page table.
- The process is returned to the ready state and the instruction is retried. Four steps, four marks, and note the process was blocked throughout.
What happens on a page fault
Step through a page fault. When the program touches a page that isn't in RAM, the OS quietly fetches it from disk and updates the page table — so the program sees more memory than physically exists.
Put the OS's response to a page fault in order.
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Detect and block, make room, load, record, retry. A page fault is the normal mechanism, not an error.
การกระเจิง (Thrashing)
- การเลือกหน้าที่จะแทนที่ Matters, เพราะการเลือกผิดหมายความว่าหน้านั้นจะถูกต้องการอีกครั้งทันที
- เมื่อมี RAM น้อยเกินไปสำหรับ working set ของโปรเซสส์ที่กำลังทำงาน Almost every access causes a page fault, and every fault evicts a page that is wanted again at once.
- System then spends nearly all its time swapping pages instead of executing instructions. That is thrashing, or disk thrashing, and it is the state described at the top of this lesson.
- The cure is more RAM, or fewer processes; a faster processor does not help at all, because the processor is not the thing that is busy.
A machine slows almost to a stop and the disk runs constantly. What will fix it?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
This is thrashing: nearly every access faults and the processor waits. The processor is not the busy component, so speeding it up changes nothing.
การแบ่งส่วน (Segmentation)
- Segmentation แบ่งหน่วยความจำออกเป็น logical segments ขนาดแปรผันที่ตรงกับโครงสร้างของโปรแกรมเอง: code, stack, heap, หรือ data array ขนาดใหญ่
- เนื่องจาก segment เป็น logical unit สามารถมี permissions ของตัวเองได้: code segment อาจเป็น read-only และ executable, stack เป็น read-write และไม่ executable
- ความแตกต่างที่ข้อสอบต้องการ: Pages are fixed-size and have no meaning to the program; segments are variable-sized and correspond to logical parts of it. Many real systems combine the two, paging within segments.

Segments are the program's own parts, not equal slices
Match each memory-management term to its meaning.
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Paging = fixed pages; segmentation = logical variable units; a page fault triggers a swap; too many faults = thrashing.
Segmentation divides memory into variable-sized logical units (code, stack, heap), each with its own permissions, whereas paging uses fixed-size pages.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Segments match the program's logical structure; pages are uniform fixed-size blocks — some systems combine both.
Unlike pages, which are all the same size, segments are of ____ size and match the program's logical parts.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
A segment is the code, the stack or the heap, so it can carry its own permissions. A page is a fixed-size division of virtual memory with no inherent meaning to the program.
ตัวอย่างปฏิบัติ: paging เทียบกับ segmentation
- State two differences between paging and segmentation. [2]
- Pages are all of a fixed size, decided by the system; segments are of variable size, decided by the logical parts of the program.
- A page is a purely physical division with no meaning to the program; a segment corresponds to a logical unit such as the code or the stack, and can be given its own access permissions.
- A third if needed: paging can leave unused space inside the last page of an allocation, while segmentation leaves gaps of unusable space between segments.

คะแนนที่หลุดหายไป
- Virtual memory does not create memory. It lets the total in use exceed physical RAM by keeping part of it on disk, at the cost of disk accesses.
- A page fault is not an error. It is the normal mechanism by which a page is brought in.
- Thrashing is spending more time swapping than executing, and the fix is more RAM or fewer processes, never a faster processor.
- Fixed size versus variable size, logically meaningful is the difference between paging and segmentation. Say both halves.
คุณเข้าใจแล้ว
- A virtual address space gives each process a private contiguous range, protects processes from each other, and lets memory in use exceed physical RAM
- Paging: fixed-size pages mapped to frames by a page table; a page fault blocks the process while the OS evicts a page if necessary, loads from the swap file, updates the table and retries
- Thrashing is more time spent swapping than executing; cure it with more RAM or fewer processes
- การแบ่งส่วน ใช้ ส่วนที่มีขนาดแปรผัน ที่ตรงกับส่วนทางตรรกะของโปรแกรม แต่ละส่วนมีสิทธิ์การใช้งานของตัวเอง; ระบบจำนวนมากมีการจัดหน้าภายในส่วน
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้