أنظمة الإدخال والإخراج
دراسة شاملة لهيكلية أنظمة الإدخال والإخراج في أنظمة التشغيل، بما في ذلك الأجهزة، واجهات التطبيقات، النظام الفرعي للنواة، ودورة حياة طلبات الإدخال والإخراج.
I/O Systems
Comprehensive study of I/O system structures in operating systems, including hardware, application interfaces, kernel subsystems, and the life cycle of I/O requests.
أهداف التعلم
- استكشاف هيكل النظام الفرعي للإدخال والإخراج في نظام التشغيل.
- مناقشة مبادئ وتعقيدات أجهزة الإدخال والإخراج.
- شرح جوانب الأداء المتعلقة بأجهزة وبرمجيات الإدخال والإخراج.
- Explore the structure of an operating system’s I/O subsystem.
- Discuss the principles and complexities of I/O hardware.
- Explain the performance aspects of I/O hardware and software.
1 أساسيات أجهزة الإدخال والإخراج
1 I/O Hardware Fundamentals
تتواصل الأجهزة مع الحاسوب عبر المنافذ والنواقل ووحدات التحكم، تماماً مثل تواصل الأجهزة المنزلية بشبكة الكهرباء عبر المقابس والأسلاك.
Devices communicate with the computer via ports, buses, and controllers, much like home appliances connect to the power grid via sockets and wires.
تتنوع أجهزة الإدخال والإخراج بشكل كبير (تخزين، نقل، واجهة مستخدم). تتصل هذه الأجهزة بالحاسوب من خلال المنفذ (Port)، وتتشارك البيانات عبر الناقل (Bus) مثل PCIe. يتم إدارة كل جهاز بواسطة وحدة تحكم (Controller) تحتوي على معالج وذاكرة خاصة. تتواصل النواة مع وحدة التحكم عبر سجلات (Registers) محددة: سجل إدخال البيانات، سجل إخراج البيانات، سجل الحالة، وسجل التحكم.
I/O devices vary greatly (storage, transmission, human-interface). They connect to the computer through a Port, and share data via a Bus like PCIe. Each device is managed by a Controller (host adapter) containing a processor and private memory. The kernel communicates with the controller via specific registers: data-in, data-out, status, and control registers.
للتواصل مع هذه السجلات، تستخدم المعالجات إما تعليمات إدخال/إخراج مباشرة (Direct I/O instructions) أو الإدخال/الإخراج المعين بالذاكرة (Memory-mapped I/O)، حيث يتم تعيين سجلات الجهاز إلى مساحة عناوين المعالج، وهو أمر ضروري للأجهزة التي تتطلب مساحات عناوين كبيرة مثل بطاقات الرسوميات.
To communicate with these registers, processors use either Direct I/O instructions or Memory-mapped I/O, where device registers are mapped to the processor's address space. This is especially crucial for devices requiring large address spaces, like graphics controllers.
لماذا يُفضل استخدام الإدخال/الإخراج المعين بالذاكرة (Memory-mapped I/O) لبطاقات الرسوميات بدلاً من تعليمات الإدخال/الإخراج المباشرة؟ Why is Memory-mapped I/O preferred for graphics cards over direct I/O instructions?
لأن بطاقات الرسوميات تتطلب نقل كميات هائلة من البيانات، وتعيينها في الذاكرة يسمح للمعالج باستخدام تعليمات الذاكرة السريعة للوصول إليها بدلاً من تعليمات الإدخال/الإخراج البطيئة والمحدودة.
Because graphics cards require transferring massive amounts of data, and mapping them to memory allows the CPU to use fast memory instructions to access them rather than slow, limited I/O instructions.
2 الاستعلام والمقاطعات
2 Polling and Interrupts
الاستعلام هو أن تسأل الجهاز باستمرار 'هل انتهيت؟'، بينما المقاطعة هي أن يخبرك الجهاز 'لقد انتهيت!' عندما يجهز.
Polling is constantly asking the device 'Are you done?', while an Interrupt is the device tapping you on the shoulder saying 'I am done!'
في الاستعلام (Polling)، يقرأ المضيف بت الحالة (busy bit) بشكل متكرر حتى يصبح فارغاً. هذا الأسلوب فعال إذا كان الجهاز سريعاً جداً، ولكنه يهدر دورات المعالج إذا كان الجهاز بطيئاً. لحل هذه المشكلة، نستخدم المقاطعات (Interrupts)، حيث يرسل الجهاز إشارة إلى المعالج عبر خط طلب المقاطعة (Interrupt-request line) عند اكتمال المهمة. يتحقق المعالج من هذا الخط بعد كل تعليمة، وينتقل إلى معالج المقاطعات (Interrupt handler) عند الحاجة.
In Polling, the host repeatedly reads the busy bit until it clears. This is reasonable if the device is very fast but highly inefficient if it's slow, as it wastes CPU cycles. To solve this, we use Interrupts, where the device triggers a CPU interrupt-request line when done. The CPU checks this line after each instruction and transfers control to an interrupt handler via an interrupt vector.
تُستخدم آلية المقاطعات أيضاً للتعامل مع الاستثناءات (Exceptions) مثل أخطاء الذاكرة (Page faults) أو القسمة على صفر، ولتنفيذ استدعاءات النظام (System calls) عبر فخ (Trap). تدعم الأنظمة الحديثة مستويات متعددة من المقاطعات لتحديد الأولويات، وتسمح بتأجيل بعض المقاطعات (Maskable) بينما لا يمكن تجاهل أخرى (Nonmaskable).
The interrupt mechanism is also used for exceptions (like page faults or division by zero) and to execute system calls via a trap. Modern systems support multilevel interrupts to distinguish priorities, allowing some to be delayed (maskable) while critical ones cannot be ignored (nonmaskable).
| Polling | Interrupts | |
|---|---|---|
| استهلاك المعالج CPU Usage | يهدر دورات المعالج في الانتظار (Busy-wait) Wastes CPU cycles waiting (Busy-wait) | يحرر المعالج لمهام أخرى Frees CPU for other tasks |
| الأداء مع الأجهزة السريعة جداً Performance with very fast devices | ممتاز، لا يوجد عبء تبديل السياق Excellent, no context switch overhead | بطيء بسبب عبء تبديل السياق Slow due to context switch overhead |
متى يكون الاستعلام (Polling) أفضل من المقاطعات (Interrupts)؟ When is Polling better than Interrupts?
يكون الاستعلام أفضل عندما يكون الجهاز سريعاً جداً لدرجة أن وقت الانتظار أقل من الوقت المستغرق في تبديل السياق (Context Switch) اللازم لمعالجة المقاطعة.
Polling is better when the device is so fast that the wait time is less than the overhead of the context switch required to handle an interrupt.
3 الوصول المباشر للذاكرة (DMA)
3 Direct Memory Access (DMA)
الـ DMA هو مساعد ينقل كميات ضخمة من البيانات بين الجهاز والذاكرة مباشرة، تاركاً المعالج حراً للقيام بمهام أخرى.
DMA is an assistant that transfers massive amounts of data directly between a device and memory, leaving the CPU free to do other work.
يُستخدم الوصول المباشر للذاكرة (DMA) لتجنب الإدخال/الإخراج المبرمج (نقل بايت واحد في كل مرة بواسطة المعالج) عند نقل كميات كبيرة من البيانات. يتطلب ذلك وحدة تحكم DMA خاصة. يكتب نظام التشغيل كتلة أوامر DMA في الذاكرة (تحتوي على عناوين المصدر والوجهة، وضع القراءة/الكتابة، وعدد البايتات). تقوم وحدة تحكم DMA بنقل البيانات مباشرة متجاوزة المعالج، وعند الانتهاء ترسل مقاطعة واحدة فقط.
Direct Memory Access (DMA) is used to avoid programmed I/O (moving one byte at a time via CPU) for large data movements. It requires a DMA controller. The OS writes a DMA command block into memory (source/destination addresses, read/write mode, byte count). The DMA controller bypasses the CPU to transfer data directly, sending a single interrupt only when the entire transfer is complete.
للقيام بذلك، تستخدم وحدة تحكم DMA تقنية السيطرة على الناقل (Bus Mastering) وتأخذ الناقل من المعالج. هذا يؤدي إلى ما يسمى بسرقة الدورات (Cycle Stealing)، حيث يُحرم المعالج مؤقتاً من الوصول للذاكرة، ولكنه يظل أكثر كفاءة بكثير من قيام المعالج بنقل البيانات بنفسه. النسخة المتقدمة (DVMA) تدرك العناوين الافتراضية وتكون أكثر كفاءة.
To achieve this, the DMA controller uses Bus Mastering to grab the bus from the CPU. This causes Cycle Stealing, where the CPU is momentarily delayed from accessing memory, but it is still vastly more efficient than the CPU doing the transfer itself. A version aware of virtual addresses (DVMA) can be even more efficient.
هل يتوقف المعالج تماماً أثناء عمل الـ DMA؟ Does the CPU stop completely during a DMA transfer?
لا، المعالج يستمر في تنفيذ التعليمات من الذاكرة المخبئية (Cache)، ولكنه قد يتباطأ قليلاً إذا احتاج للوصول إلى الذاكرة الرئيسية بسبب 'سرقة الدورات' من قبل الـ DMA.
No, the CPU continues executing instructions from its cache, but it might slow down slightly if it needs main memory access due to 'cycle stealing' by the DMA controller.
4 واجهة تطبيقات الإدخال والإخراج
4 Application I/O Interface
تقوم النواة بإخفاء تعقيدات الأجهزة المختلفة عن التطبيقات من خلال توفير واجهات قياسية وموحدة.
The kernel hides the complexities of diverse hardware from applications by providing standard, uniform interfaces.
تغلف استدعاءات نظام الإدخال والإخراج سلوكيات الأجهزة في فئات عامة. تخفي طبقة برامج تشغيل الأجهزة (Device Drivers) الاختلافات بين وحدات التحكم عن النواة. تختلف الأجهزة في عدة أبعاد: نقل الكتل مقابل تدفق الحروف (Block vs Character)، الوصول التسلسلي مقابل العشوائي، المتزامن مقابل غير المتزامن، والقابل للمشاركة مقابل المخصص.
I/O system calls encapsulate device behaviors in generic classes. The Device-driver layer hides differences among I/O controllers from the kernel. Devices vary in many dimensions: Block vs Character-stream, Sequential vs Random-access, Synchronous vs Asynchronous, and Sharable vs Dedicated.
للتعامل مع خصائص الأجهزة الخاصة التي لا تتناسب مع الواجهات القياسية، توفر أنظمة التشغيل 'باباً خلفياً' مثل استدعاء ioctl() في نظام UNIX، والذي يسمح بإرسال أوامر تحكم عشوائية مباشرة إلى سجلات الجهاز. كما تستخدم أنظمة UNIX أرقام أجهزة 'رئيسية' (Major) لتحديد نوع الجهاز وبرنامج التشغيل، و'فرعية' (Minor) لتحديد النسخة المحددة من الجهاز.
To handle specific device characteristics that don't fit standard interfaces, OSes provide an 'escape hatch' like the ioctl() call in UNIX, allowing arbitrary control commands to be sent directly to device registers. UNIX/Linux also use a tuple of 'major' (identifies driver/type) and 'minor' (identifies specific instance) device numbers.
لماذا تُعتبر لوحة المفاتيح جهاز حروف (Character Device) بينما القرص الصلب جهاز كتل (Block Device)؟ Why is a keyboard considered a character device while a hard disk is a block device?
لأن لوحة المفاتيح تولد البيانات حرفاً بحرف بشكل غير متوقع، بينما القرص الصلب يقرأ ويكتب البيانات في كتل ذات حجم ثابت (مثل 512 بايت أو 4 كيلوبايت) لزيادة الكفاءة.
Because a keyboard generates data one keystroke (byte) at a time unpredictably, whereas a hard disk reads and writes data in fixed-size chunks (e.g., 512B or 4KB) for efficiency.
5 الإدخال والإخراج المحظور، غير المحظور، وغير المتزامن
5 Blocking, Non-blocking, and Asynchronous I/O
المحظور يوقفك حتى تنتهي المهمة، غير المحظور يعطيك ما توفر فوراً، وغير المتزامن يجعلك تكمل عملك ويخبرك لاحقاً عند الانتهاء.
Blocking stops you until done, Non-blocking gives you what's available immediately, and Asynchronous lets you keep working and notifies you later.
في الإدخال/الإخراج المحظور (Blocking)، يتم تعليق العملية حتى يكتمل الإدخال/الإخراج. إنه سهل الاستخدام ولكنه غير كافٍ لبعض الاحتياجات. في غير المحظور (Non-blocking)، يعود الاستدعاء فوراً بأي قدر متاح من البيانات (يُستخدم غالباً مع واجهات المستخدم أو عبر خيوط متعددة). أما في غير المتزامن (Asynchronous)، تستمر العملية في العمل بينما يتم تنفيذ الإدخال/الإخراج في الخلفية، ويقوم النظام بإرسال إشارة للعملية عند اكتمال المهمة.
In Blocking I/O, the process is suspended until the I/O completes. It's easy to use but insufficient for some needs. In Non-blocking I/O, the call returns quickly with whatever data is available (often used in UIs or via multi-threading). In Asynchronous I/O, the process runs while the I/O executes in the background, and the subsystem signals the process when the I/O is fully completed.
يُستخدم استدعاء select() لمعرفة ما إذا كانت البيانات جاهزة قبل إجراء استدعاء القراءة/الكتابة. كما توفر الأنظمة الإدخال/الإخراج الموجه (Vectored I/O) مثل readve() في Unix، والذي يسمح باستدعاء نظام واحد لإجراء عمليات إدخال/إخراج متعددة (Scatter-Gather)، مما يقلل من عبء تبديل السياق ويوفر الذرية (Atomicity).
The select() call is used to find if data is ready before calling read/write. Systems also provide Vectored I/O (like readve() in Unix), allowing one system call to perform multiple I/O operations (scatter-gather). This decreases context switching overhead and can provide atomicity.
| Synchronous I/O | Asynchronous I/O | |
|---|---|---|
| حالة العملية Process State | تُعلق (Blocked) حتى الانتهاء Suspended (Blocked) until completion | تستمر في التنفيذ Continues execution |
| سهولة الاستخدام Ease of Use | سهل الفهم والاستخدام Easy to use and understand | صعب الاستخدام والبرمجة Difficult to use and program |
ما هو الفرق الدقيق بين الإدخال/الإخراج غير المحظور (Non-blocking) وغير المتزامن (Asynchronous)؟ What is the exact difference between Non-blocking and Asynchronous I/O?
غير المحظور يعود فوراً بالبيانات المتاحة حالياً (حتى لو كانت صفر)، بينما غير المتزامن يبدأ العملية ويعود فوراً، ثم يرسل إشعاراً لاحقاً عندما تكتمل العملية بالكامل.
Non-blocking returns immediately with whatever data is currently available (even if zero), while Asynchronous initiates the operation, returns immediately, and sends a notification later when the entire operation is complete.
6 النظام الفرعي للإدخال والإخراج في النواة
6 Kernel I/O Subsystem
النواة هي المايسترو الذي ينظم طلبات الإدخال والإخراج عبر الجدولة، التخزين المؤقت، والتخزين المخبئي لضمان الأداء السلس.
The kernel is the maestro that orchestrates I/O requests via scheduling, buffering, and caching to ensure smooth performance.
يقدم النظام الفرعي للنواة خدمات حيوية تشمل: الجدولة (Scheduling) لترتيب الطلبات عبر طوابير الأجهزة لتحقيق العدالة وجودة الخدمة. التخزين المؤقت (Buffering) لتخزين البيانات في الذاكرة أثناء النقل للتعامل مع عدم تطابق سرعات الأجهزة أو أحجام النقل، وللحفاظ على 'دلالات النسخ' (Copy semantics). التخزين المخبئي (Caching) للاحتفاظ بنسخة من البيانات في جهاز أسرع لتحسين الأداء. التخزين المؤقت للمهام (Spooling) للأجهزة التي تخدم طلباً واحداً في كل مرة مثل الطابعات.
The kernel subsystem provides vital services: Scheduling to order requests via device queues for fairness and QoS. Buffering to store data in memory during transfer, coping with device speed/size mismatches and maintaining 'copy semantics'. Caching to hold a copy of data in a faster device for performance. Spooling to hold output for devices that serve only one request at a time, like printers.
تستخدم النواة تقنية التخزين المؤقت المزدوج (Double Buffering) حيث يتم استخدام نسختين من البيانات (واحدة تُعالج والأخرى تُستخدم) لفك الارتباط بين المنتج والمستهلك. كما تدير النواة حجز الأجهزة (Device Reservation) لتوفير وصول حصري، مع الحذر من حدوث حالات الجمود (Deadlock). بالإضافة إلى ذلك، تتعامل النواة مع إدارة الأخطاء (Error Handling) عبر إعادة المحاولة وتسجيل الأخطاء، وإدارة الطاقة (Power Management) خاصة في الأجهزة المحمولة.
The kernel uses Double Buffering (two copies of data: one being processed, one being used) to decouple producers and consumers. It also manages Device Reservation for exclusive access, watching out for deadlocks. Furthermore, it handles Error Handling (retries, error logs) and Power Management, which is a first-class OS aspect, especially in mobile computing.
ما هو الفرق الأساسي بين التخزين المؤقت (Buffering) والتخزين المخبئي (Caching)؟ What is the fundamental difference between Buffering and Caching?
التخزين المؤقت (Buffering) يحتفظ بالبيانات الأصلية الوحيدة أثناء انتقالها بين جهازين بسرعات مختلفة، بينما التخزين المخبئي (Caching) يحتفظ بنسخة مكررة من بيانات موجودة في مكان آخر لتسريع الوصول إليها.
Buffering holds the only instance of data while it is in transit between devices of different speeds, whereas Caching holds a duplicate copy of data that exists elsewhere to speed up access.
7 حماية الإدخال والإخراج ودورة حياة الطلب
7 I/O Protection and Request Lifecycle
الإدخال والإخراج عملية خطيرة، لذا يجب أن تمر جميع الطلبات عبر النواة كحارس أمن.
I/O is a dangerous operation, so all requests must pass through the kernel acting as a security guard.
لمنع عمليات المستخدم من تعطيل النظام (عن قصد أو بغير قصد)، يتم تعريف جميع تعليمات الإدخال والإخراج على أنها تعليمات ذات امتياز (Privileged Instructions). يجب أن يتم الإدخال والإخراج حصرياً عبر استدعاءات النظام (System Calls). تبدأ دورة حياة الطلب عندما يطلب المستخدم إدخال/إخراج عبر استدعاء نظام. تتحقق النواة مما إذا كان يمكن تلبية الطلب من الذاكرة المخبئية. إذا لم يكن كذلك، يتم إرسال الطلب إلى برنامج تشغيل الجهاز، الذي يرسل الأوامر إلى وحدة التحكم. ينفذ الجهاز الأمر، ويولد مقاطعة عند الانتهاء. يستلم معالج المقاطعات الإشارة، ويتم إرجاع البيانات والتحكم إلى العملية الطالبة.
To prevent user processes from disrupting the system, all I/O instructions are defined as Privileged Instructions. I/O must be performed exclusively via System Calls. The lifecycle of a request starts with a user system call. The kernel checks if it can satisfy the request from the cache. If not, it sends the request to the device driver, which issues commands to the controller. The device executes the command and generates an interrupt when done. The interrupt handler receives it, and data/control is returned to the requesting process.
بالإضافة إلى التعليمات المباشرة، يجب أيضاً حماية مواقع الذاكرة المستخدمة في الإدخال/الإخراج المعين بالذاكرة (Memory-mapped I/O) ومنافذ الإدخال/الإخراج بواسطة وحدة إدارة الذاكرة (MMU) لمنع الوصول غير المصرح به من قبل برامج المستخدم.
In addition to direct instructions, memory locations used for Memory-mapped I/O and I/O ports must also be protected by the Memory Management Unit (MMU) to prevent unauthorized access by user programs.
ماذا يحدث إذا حاول برنامج مستخدم تنفيذ تعليمة إدخال/إخراج مباشرة؟ What happens if a user program attempts to execute a direct I/O instruction?
سيقوم المعالج بتوليد استثناء (Trap) يسمى خطأ الحماية (Protection Fault)، وسيقوم نظام التشغيل بإنهاء البرنامج المعتدي.
The CPU will generate a trap (Protection Fault), and the operating system will terminate the offending program.