Memory Managementإدارة الذاكرة
مقدمة الفصل
ماذا ستتعلم في هذا الفصل
- Why memory management is needed, and its five requirements.
- Memory partitioning: fixed (equal and unequal) and dynamic partitioning, fragmentation and placement algorithms.
- Relocation: logical, relative and physical addresses; base and bounds registers.
- Simple paging and simple segmentation.
أقسام الفصل
- 1The need for memory managementالحاجة إلى إدارة الذاكرة
- 2Memory management requirementsمتطلبات إدارة الذاكرة
- 3Memory management terms: frame, page, segmentمصطلحات: الإطار والصفحة والقطعة
- 4Partitioning and fixed partitioningالتقسيم والتقسيم الثابت
- 5Dynamic partitioning and external fragmentationالتقسيم الديناميكي والتجزئة الخارجية
- 6Placement algorithms: best-fit, first-fit, next-fitخوارزميات الوضع: الأنسب والأول والتالي
- 7Relocation, addresses and registersإعادة التوطين والعناوين والمسجلات
- 8Simple pagingالتصفيح البسيط
- 9Simple segmentationالتقطيع البسيط
تلميح: استخدم أسهم لوحة المفاتيح للتنقل بين الشرائح.
The need for memory managementالحاجة لإدارة الذاكرة
- Applications are demanding more and more memory — there is never enough.
- Memory management involves swapping blocks of data from secondary storage.
- Memory I/O is slow compared to a CPU → the OS must cleverly time the swapping to maximise the CPU's efficiency.
- Goal: a reasonable supply of ready processes to consume available processor time (slide 4).
Memory management requirementsالمتطلبات الخمسة
تذكّر معنى كل متطلب.
- Relocation
The program may be swapped to disk and return to main memory at a different location; memory references must be translated to the actual physical address.
- Protection
Processes must not reference another process' memory without permission; impossible to check absolute addresses at compile time — must be checked at run time.
- Sharing
Allow several processes to access the same portion of memory — the same copy of the program.
- Logical organisation
Programs are written in modules (compiled independently, different protection, shared) — segmentation helps here.
- Physical organisation
Cannot leave memory management to the programmer; overlaying lets modules share a region but is time consuming to program.
Figure 7.1 — addressing requirements for a process (slide 8)
- From low to high (increasing address values): Process Control Block (process control information), Program (entry point to program; a branch instruction refers within it), Data (a reference to data from the program), Stack (current top of stack).
Frame, page and segmentالإطار والصفحة والقطعة
Memory management terms: frame, page, segment
| Termالمصطلح | Description (Table 7.1, slide 7)الوصف |
|---|---|
| Frame | Fixed-length block of main memory |
| Page | Fixed-length block of data in secondary memory (e.g. on disk) |
| Segment | Variable-length block of data that resides in secondary memory |
Fixed partitioningالتقسيم الثابت
- Partitioning is an early method (pre-virtual memory, not used much now); virtual memory has evolved from the partitioning methods.
- Types (slide 14): fixed partitioning, dynamic partitioning, simple paging, simple segmentation, virtual memory paging, virtual memory segmentation.
Any process whose size is less than or equal to the partition size can be loaded into an available partition; the OS can swap a process out if none are in a ready or running state.
Problems (slide 16)
- A program may not fit in a partition → the programmer must design the program with overlays.
- Main memory use is inefficient: any program, no matter how small, occupies an entire partition → internal fragmentation.
Unequal partitions and placementالأقسام غير المتساوية
Partitioning and fixed partitioning
- Unequal-size partitions lessen both problems but don't solve them completely: programs up to 16M without overlay (Fig 7.3b); smaller programs in smaller partitions → less internal fragmentation.
(a) One process queue per partitionطابور لكل قسم
- New processes go into a separate queue for each partition.
(b) Single queueطابور واحد
- All new processes wait in one queue and are sent to any partition.
Equal-size
- Placement is trivial (no options)
Unequal-size
- Assign each process to the smallest partition within which it will fit
- A queue for each partition
- Minimize wasted memory within a partition
Remaining problems with fixed partitions (slide 20)
- The number of active processes is limited by the pre-determined number of partitions.
- A large number of very small processes will not use the space efficiently.
Dynamic partitioningالتقسيم الديناميكي
Partitions are of variable length and number; a process is allocated exactly as much memory as required.
Memory external to all processes is fragmented.
The OS moves processes so that they are contiguous — resolves external fragmentation but is time consuming and wastes CPU time.
Placement algorithmsخوارزميات الوضع
| Algorithmالخوارزمية | How it choosesطريقة الاختيار | Notes on the slidesملاحظات الشرائح |
|---|---|---|
| Best-fit | The block closest in size to the request |
|
| First-fit |
|
|
| Next-fit | Scans from the location of the last placement |
|
Relocation and types of addressإعادة التوطين
Relocation, addresses and registers
- When a program is loaded, the actual (absolute) memory locations are determined.
- A process may occupy different absolute memory locations during execution — because of swapping and compaction.
Reference to a memory location independent of the current assignment of data to memory.
Address expressed as a location relative to some known point.
The actual location in main memory.
Base and bounds registersالأساس والحدود
Relocation, addresses and registers
Starting address for the process.
Ending location of the process.
- These values are set when the process is loaded or when the process is swapped in.
During execution (slide 31)
- 1
The base register is added to a relative address to produce an absolute address.
- 2
The resulting address is compared with the bounds register.
- 3
If the address is not within bounds, an interrupt is generated to the operating system.
المسألة
Illustrative numbers (not from the slides): base register = 5000, bounds register = 5800, relative address = 300.
خطوات الحل
- Absolute address = base + relative = 5000 + 300 = 5300.
- Compare with bounds: 5300 ≤ 5800 → within bounds.
النتيجة
The access goes to absolute address 5300; with relative address 900 the result 5900 would exceed the bounds and generate an interrupt.
Simple pagingالتصفيح
Partition memory into small equal fixed-size chunks and divide each process into the same size chunks: process chunks are pages, memory chunks are frames; page size = frame size.
المسألة
Slide 32: program size 16 M, frame (page) size 8 M.
الصيغة
# of pages = program size / page size
خطوات الحل
- 16 / 8
النتيجة
2 pages.
| Item | Value |
|---|---|
| Logical address | 000001 0111011110 (page 1, offset 0111011110) |
| Process page table |
|
| Physical address | 000110 0111011110 (frame 000110 + the same offset) |
- The OS maintains a page table for each process: the frame location for each page.
- A memory address = page number + offset within the page.
Simple segmentationالتقطيع
- A program can be subdivided into segments; segments may vary in length; there is a maximum segment length.
- Addressing: a segment number and an offset.
- Segmentation is similar to dynamic partitioning.
Pagingالتصفيح
- Equal fixed-size chunks
- Address = page number + offset
Segmentationالتقطيع
- Segments may vary in length (with a maximum)
- Address = segment number + offset
- Similar to dynamic partitioning
الخلاصة
أنهيت الدرس
الخطوة التالية: ثبّت المعلومات بالبطاقات، ثم اختبر نفسك بالأسئلة.
ما راجعته في هذا الدرس
- The need for memory managementالحاجة إلى إدارة الذاكرة
- Memory management requirementsمتطلبات إدارة الذاكرة
- Memory management terms: frame, page, segmentمصطلحات: الإطار والصفحة والقطعة
- Partitioning and fixed partitioningالتقسيم والتقسيم الثابت
- Dynamic partitioning and external fragmentationالتقسيم الديناميكي والتجزئة الخارجية
- Placement algorithms: best-fit, first-fit, next-fitخوارزميات الوضع: الأنسب والأول والتالي
- Relocation, addresses and registersإعادة التوطين والعناوين والمسجلات
- Simple pagingالتصفيح البسيط
- Simple segmentationالتقطيع البسيط