Space Management: Main Memory إدارة المساحة: الذاكرة الرئيسية

Bridging the gap between CPU and Storage. Contiguous Allocation, Fragmentation, Paging, and Translation Look-aside Buffers (TLB). سد الفجوة بين الـ CPU والتخزين. التخصيص المتجاور، التجزئة، الترحيل (Paging)، ومخازن الترجمة المؤقتة (TLB).

Logical vs Physical Address Logical vs Physical Address Fragmentation Fragmentation Paging Paging Page Table Page Table TLB TLB Swapping Swapping

Core Concepts المفاهيم الجوهرية

A program on disk is a passive file. To run, it must be loaded into memory.
Address Binding maps instructions to physical memory addresses at different times.
البرنامج على القرص هو ملف خامل. لكي يعمل، يجب تحميله إلى الذاكرة.
Address Binding (ربط العناوين) يعين التعليمات لعناوين الذاكرة المادية في أوقات مختلفة.

Compile Time Compile Time (وقت الترجمة) If memory location is known a priori, absolute code can be generated. Must recompile if location changes. إذا كان موقع الذاكرة معروفاً مسبقاً، يمكن توليد كود مطلق. يجب إعادة الترجمة إذا تغير الموقع.
Load Time Load Time (وقت التحميل) Compiler generates relocatable code. Final binding happens when loading. يولد المترجم كوداً قابلاً للنقل. الربط النهائي يحدث عند التحميل.
Execution Time Execution Time (وقت التنفيذ) Binding delayed until run time. Needs hardware support (MMU). Most modern OSs use this. يتأخر الربط حتى وقت التشغيل. يتطلب دعم الأجهزة (MMU). تستخدمه معظم أنظمة التشغيل الحديثة.

Memory Management Unit (MMU) وحدة إدارة الذاكرة (MMU)

A hardware device that maps Logical to Physical addresses at run time. جهاز يحول العناوين المنطقية (Logical) إلى عناوين مادية (Physical) في وقت التشغيل.

Relocation Register Logic: منطق سجل النقل (Relocation Register):

User program deals with Logical Addresses (0 to max).
MMU adds the value in the Relocation Register to generate the Physical Address.
Physical = Logical + Relocation Register
يتعامل برنامج المستخدم مع العناوين المنطقية (من 0 إلى الحد الأقصى).
تضيف الـ MMU القيمة الموجودة في سجل النقل لتوليد العنوان المادي.
المادي = المنطقي + سجل النقل

flowchart LR CPU([CPU]) -- "Logical Addr" --> MMU(MMU) MMU -- "Physical Addr" --> MEM[(Memory)] MMU -. "Relocation Reg" .-> ADD((+)) style CPU fill:#3b82f6,color:#fff,stroke:#2563eb,stroke-width:2px style MMU fill:#8b5cf6,color:#fff,stroke:#7c3aed,stroke-width:2px style MEM fill:#10b981,color:#fff,stroke:#059669,stroke-width:2px style ADD fill:#f59e0b,color:#fff,stroke:#d97706,stroke-width:2px

Contiguous Allocation التخصيص المتجاور (Contiguous Allocation)

Each process is contained in a single contiguous section of memory.
Dynamic Storage-Allocation Problem: How to satisfy a request of size n from a list of free holes?
تتواجد كل عملية في قسم واحد متصل من الذاكرة.
مشكلة تخصيص التخزين الديناميكي: كيف تلبي طلباً بحجم n من قائمة الفجوات الحرة؟

  • First-Fit:First-Fit: Allocate the first hole that is big enough. (Fast). تخصيص أول فجوة كبيرة بما يكفي. (سريع).
  • Best-Fit:Best-Fit: Allocate the smallest hole that is big enough. (Leaves tiny holes). تخصيص أصغر فجوة كبيرة بما يكفي. (يترك فجوات صغيرة جداً).
  • Worst-Fit:Worst-Fit: Allocate the largest hole. (Leaves largest remaining hole). تخصيص أكبر فجوة. (يترك أكبر فجوة متبقية).

External Fragmentation External Fragmentation (تجزئة خارجية)

Total memory space exists to satisfy a request, but it is not contiguous.
Solution: Compaction (Shuffle memory contents to place free memory together). Expensive.
مساحة الذاكرة الإجمالية موجودة لتلبية الطلب، لكنها ليست متصلة.
الحل: Compaction (خلط محتويات الذاكرة لتجميع المساحة الحرة). مكلف.

Internal Fragmentation Internal Fragmentation (تجزئة داخلية)

Allocated memory may be slightly larger than requested memory. The difference is unused memory internal to a partition.
Common in Paging.
الذاكرة المخصصة قد تكون أكبر قليلاً من المطلوبة. الفرق هو ذاكرة غير مستخدمة داخل القسم.
شائع في Paging.

Paging Paging (الترحيل)

Physical memory is divided into fixed-sized blocks called Frames.
Logical memory is divided into blocks of same size called Pages.
Benefit: Eliminates External Fragmentation. Allows non-contiguous allocation.
تقسم الذاكرة المادية إلى كتل ثابتة الحجم تسمى Frames (إطارات).
تقسم الذاكرة المنطقية إلى كتل بنفس الحجم تسمى Pages (صفحات).
الفائدة: يلغي التجزئة الخارجية (External Fragmentation). يسمح بالتخصيص غير المتجاور.

Logical Address Structure هيكل العنوان المنطقي
Page Number (p)Page Number (p)
Page Offset (d)Page Offset (d)

$p$ is index into Page Table.
$d$ is displacement within the page/frame.
$p$ هو فهرس في جدول الصفحات.
$d$ هو الإزاحة داخل الصفحة/الإطار.

Page Table Page Table (جدول الصفحات)
Maps p $\to$ f يربط p $\to$ f
Physical Addr = (f $\times$ Size) + d Physical Addr = (f $\times$ Size) + d

Translation Look-aside Buffer (TLB) Translation Look-aside Buffer (TLB)

The Page Table is stored in main memory. This means every data access requires two memory accesses (one for table, one for data). Too slow!
Solution: TLB. A fast, special hardware cache for page translations.
يخزن جدول الصفحات في الذاكرة الرئيسية. هذا يعني أن كل وصول للبيانات يتطلب وصولين للذاكرة (واحد للجدول، وواحد للبيانات). بطيء جداً!
الحل: TLB. ذاكرة تخزين مؤقت سريعة للأجهزة لترجمة الصفحات.

TLB Hit TLB Hit Translation found in fast cache. No extra memory access. تم العثور على الترجمة في الكاش السريع. لا وصول إضافي للذاكرة.
TLB Miss TLB Miss Must go to memory to fetch page table entry. Add to TLB for next time. يجب الذهاب للذاكرة لجلب إدخال الجدول. أضفه لـ TLB للمرة القادمة.

Advanced Structures الهياكل المتقدمة

For large address spaces ($2^{64}$), the page table itself becomes too large to fit in contiguous memory. لمساحات العناوين الكبيرة ($2^{64}$)، يصبح جدول الصفحات نفسه كبيراً جداً بحيث لا يتسع في ذاكرة متجاورة.

Hierarchical Paging Hierarchical Paging

"Page the Page Table". Break the page table into smaller pieces.
e.g., Two-Level Paging. Outer page table maps to inner page tables.
"ترحيل جدول الصفحات". تقسيم الجدول إلى قطع أصغر.
مثال: Two-Level Paging. جدول خارجي يشير لجداول داخلية.

Hashed Page Tables Hashed Page Tables

Common for address spaces > 32 bits.
Virtual page number is hashed into a chain of elements.
شائعة لمساحات العناوين > 32 بت.
يتم عمل Hash لرقم الصفحة الافتراضية إلى سلسلة عناصر.

Inverted Page Tables Inverted Page Tables

One entry for each physical frame (not virtual page).
Saves memory, but searching is harder (needs hashing).
إدخال واحد لكل إطار مادي (ليس صفحة افتراضية).
يوفر الذاكرة، لكن البحث أصعب (يحتاج Hashing).

The Exam Vault خزنة الاختبار

Professor's Secrets & Trap Avoidance أسرار البروفيسور وتجنب الفخاخ

TRAP: EAT Calculation فخ: حساب EAT

Effective Access Time (EAT) with TLB:
$EAT = (HitRate \times Time_{mem}) + (MissRate \times 2 \times Time_{mem})$
Note: Miss usually costs 2 memory accesses (Table + Data). Don't forget the '2'!
Effective Access Time (EAT) مع TLB:
$EAT = (HitRate \times Time_{mem}) + (MissRate \times 2 \times Time_{mem})$
ملاحظة: الـ Miss يكلف عادة وصولين للذاكرة (الجدول + البيانات). لا تنسَ الرقم '2'!

TRAP: Fragmentation Types فخ: أنواع التجزئة (Fragmentation)

Segmentation suffers from External Fragmentation.

Paging suffers from Internal Fragmentation (last frame may not be full).
Do not mix them up.
Segmentation تعاني من التجزئة الخارجية (External).

Paging يعاني من التجزئة الداخلية (Internal) (الإطار الأخير قد لا يمتلئ).
لا تخلط بينهما.

SECRET: Context Switch & TLB سر: Context Switch و TLB

When switching processes, the TLB must be flushed (cleared) because the virtual-to-physical mapping changes completely. This adds to the cost of context switching. Some TLBs use ASIDs (Address Space IDs) to avoid flushing. عند تبديل العمليات، يجب مسح (flush) الـ TLB لأن ربط العناوين الافتراضية بالمادية يتغير تماماً. هذا يضيف لتكلفة تبديل السياق. بعض TLBs تستخدم ASIDs لتجنب المسح.

KEY CONCEPT: Shared Pages مفهوم أساسي: الصفحات المشتركة (Shared Pages)

Paging allows sharing common code (e.g., standard C library, text editors).
The code must be Reentrant (read-only, no self-modification).
Multiple processes can map their virtual pages to the same physical frame holding the shared code.
يسمح الـ Paging بمشاركة الكود المشترك (مثل مكتبة C القياسية).
يجب أن يكون الكود Reentrant (للقراءة فقط، لا يعدل نفسه).
يمكن لعدة عمليات ربط صفحاتها الافتراضية بـ نفس الإطار المادي الذي يحمل الكود المشترك.