Input/Output: I/O Systems الإدخال/الإخراج: أنظمة الـ I/O

Managing external devices. Polling, Interrupts, DMA, and the Kernel I/O Subsystem. إدارة الأجهزة الخارجية. الـ Polling، المقاطعات (Interrupts)، الوصول المباشر للذاكرة (DMA)، ونظام الـ Kernel I/O الفرعي.

Port-Mapped I/O Port-Mapped I/O Polling Polling (التصويت) Interrupts Interrupts (المقاطعات) DMA DMA Blocking/Non-blocking Blocking/Non-blocking

Hardware Interface واجهة الأجهزة (Hardware Interface)

A typical I/O device has a Port (connection point) and a Bus (daisy chain or shared access).
Device Controller: Electronics that operate the port/device. Has registers for data and control.
يحتوي جهاز الـ I/O النموذجي على منفذ (Port) (نقطة اتصال) و ناقل (Bus) (سلسلة متعاقبة أو وصول مشترك).
Device Controller (وحدة تحكم الجهاز): الإلكترونيات التي تشغل المنفذ/الجهاز. تحتوي على سجلات (Registers) للبيانات والتحكم.

Memory-Mapped I/O Memory-Mapped I/O Device registers are mapped to main memory addresses.
Access: Standard load/store instructions (`MOV`, `LD`).
Example: Video memory.
يتم ربط سجلات الجهاز بعناوين الذاكرة الرئيسية.
الوصول: تعليمات التحميل/التخزين القياسية (`MOV`, `LD`).
مثال: ذاكرة الفيديو.
Port-Mapped I/O Port-Mapped I/O Uses special I/O instructions (`IN`, `OUT`) to access separate I/O address space.
Example: Early x86 architecture.
يستخدم تعليمات I/O خاصة (`IN`, `OUT`) للوصول إلى مساحة عناوين I/O منفصلة.
مثال: معمارية x86 القديمة.

Communication Methods طرق التواصل (Communication Methods)

How does the CPU talk to the device? كيف يتحدث المعالج (CPU) مع الجهاز؟

1. Polling (Busy Wait) 1. Polling (الانتظار النشط)

CPU repeatedly checks the status register: "Are you ready? Are you ready?".
Pros: Simple. Fast if device is fast.
Cons: Wastes CPU cycles if device is slow.
يقوم الـ CPU بفحص سجل الحالة مراراً وتكراراً: "هل أنت جاهز؟ هل أنت جاهز؟".
المزايا: بسيط. سريع إذا كان الجهاز سريعاً.
العيوب: يضيع دورات المعالج إذا كان الجهاز بطيئاً.

while (*status_reg == BUSY); // Spin loop *data_reg = byte_to_write; *command_reg = GO;

2. Interrupt-Driven I/O 2. Interrupt-Driven I/O (المقاطعات)

CPU issues command and goes to do other work.
When device is done, it raises an Interrupt Request Line.
CPU stops, saves state, runs Interrupt Handler, restores state.
Benefit: CPU not tied up waiting.
يصدر الـ CPU الأمر ويذهب للقيام بعمل آخر.
عندما ينتهي الجهاز، يرفع إشارة Interrupt Request Line.
يتوقف الـ CPU، يحفظ الحالة، يشغل Interrupt Handler، ويستعيد الحالة.
الفائدة: لا ينشغل المعالج بالانتظار.

3. Direct Memory Access (DMA) 3. Direct Memory Access (DMA)

Used for high-speed devices (Disks, Network) to avoid interrupting CPU for every byte.
How: CPU programs DMA controller (Source, Dest, Count). DMA transfers block directly to memory.
Result: Only one interrupt per block.
يستخدم للأجهزة عالية السرعة (الأقراص، الشبكات) لتجنب مقاطعة المعالج لكل بايت.
كيف: يبرمج الـ CPU وحدة تحكم DMA (المصدر، الوجهة، العدد). ينقل الـ DMA الكتلة مباشرة للذاكرة.
النتيجة: مقاطعة واحدة فقط لكل كتلة.

graph LR CPU -->|Setup| DMA DMA -->|Data Transfer| MEM[Memory] DISK[Disk Drive] -->|Data| DMA DMA -->|"Interrupt (Done)"| CPU

Kernel I/O Subsystem نظام الـ Kernel I/O الفرعي

The kernel provides generic services to hide hardware complexity. يوفر الـ Kernel خدمات عامة لإخفاء تعقيدات الأجهزة.

I/O Scheduling جدولة الإدخال/الإخراج (I/O Scheduling) Reordering requests (queueing) to optimize performance (e.g., disk elevator). إعادة ترتيب الطلبات (Queueing) لتحسين الأداء (مثل مصعد القرص).
Buffering التخزين المؤقت (Buffering) Store data in memory while transferring. Handles speed mismatch (modem vs SSD) and data size mismatch. تخزين البيانات في الذاكرة أثناء النقل. يعالج عدم تطابق السرعة (Modem مقابل SSD) وحجم البيانات.
Caching التخزين المخبأ (Caching) Holding copies of data for faster access. (Buffer = copy, Cache = fast copy). الاحتفاظ بنسخ من البيانات للوصول السريع. (Buffer = نسخة، Cache = نسخة سريعة).
Spooling Spooling Holding output for a device that can only serve one request at a time (e.g., Printer). الاحتفاظ بالمخرجات لجهاز يمكنه خدمة طلب واحد فقط في كل مرة (مثل الطابعة).

Blocking vs Non-Blocking Blocking vs Non-Blocking

Blocking I/O Blocking I/O Process suspended until I/O completed. Easy to use/understand. يتم تعليق العملية حتى اكتمال الـ I/O. سهل الاستخدام/الفهم.
Non-Blocking I/O Non-Blocking I/O Call returns quickly with data count read (maybe 0). Process keeps running. Used in UIs/Video games. تعود الدالة بسرعة مع عدد البيانات المقروءة (ربما 0). تستمر العملية في العمل. يستخدم في واجهات المستخدم/الألعاب.
Asynchronous I/O Asynchronous I/O Process runs while I/O executes. Signal tells process when I/O is done. Hard to program. تعمل العملية أثناء تنفيذ الـ I/O. إشارة تخبر العملية عند انتهاء الـ I/O. صعب البرمجة.

Life Cycle of a Request دورة حياة الطلب (Life Cycle of a Request)

  1. User Process: Issues read() system call. عملية المستخدم: تصدر نداء النظام read().
  2. Kernel: Checks parameters. Checks if data in cache. If not, requests device driver. Kernel: يتحقق من المعاملات. يتحقق مما إذا كانت البيانات في الكاش. إذا لا، يطلب مشغل الجهاز (Device Driver).
  3. Device Driver: Issues commands to device controller registers. Blocks process. Device Driver: يصدر أوامر لسجلات وحدة تحكم الجهاز. يحظر العملية.
  4. Device Controller: Operates hardware. Transfers data. Generates Interrupt when done. Device Controller: يشغل الجهاز. ينقل البيانات. يولد Interrupt عند الانتهاء.
  5. Interrupt Handler: Saves data, signals driver. Interrupt Handler: يحفظ البيانات، يرسل إشارة للمشغل.
  6. Kernel: Moves data to user space. Wakes up process. Kernel: ينقل البيانات لمساحة المستخدم. يوقظ العملية.
  7. User Process: read() returns. عملية المستخدم: تعود الدالة read().

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

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

TRAP: Interrupt Overhead فخ: تكلفة المقاطعات (Overhead)

Interrupts are good but have overhead (Context switch, Pipeline flush).
For very high-speed networks (e.g., 10Gbps), interrupts happen too often ("Livelock").
Solution: Hybrid approach (Switch to Polling under high load).
المقاطعات جيدة لكن لها تكلفة (Context switch, Pipeline flush).
للشبكات عالية السرعة جداً (مثل 10Gbps)، تحدث المقاطعات كثيراً ("Livelock").
الحل: نهج هجين (التحول إلى Polling تحت الحمل العالي).

TRAP: Double Buffering فخ: التخزين المؤقت المزدوج (Double Buffering)

Often used in graphics/video. While one buffer is being displayed/written, the other is being filled. This decouples the producer and consumer rates. يستخدم غالباً في الرسوميات/الفيديو. بينما يتم عرض/كتابة أحد المخازن المؤقتة، يتم ملء الآخر. هذا يفصل معدلات المنتج والمستهلك.

SECRET: DMA & Caches سر: DMA و الكاش

DMA writes directly to Main Memory, bypassing the CPU Cache. This creates a Cache Coherency problem (CPU reads stale data from cache).
Fix: OS/Hardware must invalidate cache lines affected by DMA.
يكتب الـ DMA مباشرة إلى الذاكرة الرئيسية، متجاوزاً كاش المعالج. هذا يخلق مشكلة Cache Coherency (يقرأ المعالج بيانات قديمة من الكاش).
الحل: يجب على نظام التشغيل/الأجهزة إبطال خطوط الكاش المتأثرة بالـ DMA.

KEY CONCEPT: Vectored Interrupt مفهوم مفتاحي: Vectored Interrupt

Instead of a single "Interrupt" line where the CPU must poll every device to find who rang the bell, modern CPUs use an Interrupt Vector Table. The device sends an ID (Vector #), and CPU jumps directly to that device's specific handler. بدلاً من خط "Interrupt" واحد حيث يجب على المعالج استطلاع كل جهاز لمعرفة من أطلق الجرس، تستخدم المعالجات الحديثة Interrupt Vector Table. يرسل الجهاز معرفاً (Vector #)، ويقفز المعالج مباشرة إلى الـ Handler الخاص بذلك الجهاز.