مقدمة وخدمات نظم التشغيل
نظرة عامة شاملة على نظم التشغيل، هيكلة الحاسب، إدارة الموارد، نداءات النظام، والخدمات التي يقدمها نظام التشغيل.
Introduction and Operating-System Services
A comprehensive overview of operating systems, computer architecture, resource management, system calls, and OS services.
أهداف التعلم
- وصف تنظيم نظام الحاسب ودوره الأساسي.
- تحديد مكونات نظام الحاسب بما في ذلك أنظمة المعالجات المتعددة.
- شرح الانتقال بين وضع المستخدم ووضع النواة (User and Kernel mode).
- تحديد خدمات نظام التشغيل ونداءات النظام (System Calls).
- Describe computer system organization and role.
- Define the components of a computer system including multiprocessor computer systems.
- Explain user and kernel mode transition.
- Identify operating system services and system calls.
1 تعريف نظام التشغيل وأهدافه
1 Operating System Definition and Goals
برنامج يعمل كوسيط بين مستخدم الحاسب والأجهزة المادية (Hardware)، ويدير الموارد بكفاءة.
A program that acts as an intermediary between a user of a computer and the computer hardware.
نظام التشغيل هو مخصص للموارد (Resource Allocator) وبرنامج تحكم (Control Program).
أهدافه الرئيسية هي: تنفيذ برامج المستخدم وتسهيل حل المشكلات، جعل نظام الحاسب مريحاً للاستخدام من خلال توفير تجريد (Abstraction) للبرامج، واستخدام الأجهزة المادية بطريقة فعالة.
لا يوجد تعريف عالمي متفق عليه، ولكن غالباً ما يُشار إلى 'النواة' (Kernel) على أنها البرنامج الوحيد الذي يعمل طوال الوقت على الحاسب.
The operating system is a resource allocator and a control program.
Its main goals are to execute user programs and make solving user problems easier, make the computer system convenient to use by providing abstractions, and use the computer hardware in an efficient manner.
There is no universally accepted definition, but the 'Kernel' is considered the one program running at all times on the computer.
أنظمة التشغيل الحديثة لا تقتصر على النواة فقط، بل تشمل برامج النظام (System Programs) والبرمجيات الوسيطة (Middleware) التي توفر خدمات إضافية لمطوري التطبيقات مثل قواعد البيانات والوسائط المتعددة.
هذا التوسع ضروري لدعم التطبيقات المعقدة في الأجهزة المحمولة والحواسيب العامة.
Modern OSes for general purpose and mobile computing also include middleware—a set of software frameworks that provide additional services to application developers such as databases, multimedia, and graphics.
This expansion is necessary to support complex applications.
لماذا يُعتبر نظام التشغيل 'مخصصاً للموارد' (Resource Allocator)؟ Why is an operating system considered a 'Resource Allocator'?
لأنه يدير جميع الموارد (وقت المعالج، الذاكرة، مساحة التخزين، أجهزة الإدخال والإخراج) ويقرر كيفية تخصيصها للبرامج والمستخدمين المتعددين لضمان عمل النظام بكفاءة وعدالة.
Because it manages all resources (CPU time, memory, file storage, I/O devices) and decides how to allocate them to specific programs and users to ensure efficient and fair operation.
2 المقاطعات وعمليات الإدخال والإخراج
2 Interrupts and I/O Operations
إشارة تُرسل إلى المعالج (CPU) لإيقاف عمله الحالي والتعامل مع حدث مهم، مثل انتهاء عملية إدخال/إخراج.
A signal sent to the CPU to stop its current execution and handle an important event, such as the completion of an I/O operation.
تعمل أجهزة الإدخال والإخراج والمعالج بشكل متزامن. كل وحدة تحكم بجهاز (Device Controller) مسؤولة عن نوع معين من الأجهزة ولديها مخزن مؤقت محلي (Local Buffer).
عندما يطلب برنامج عملية إدخال/إخراج، يقوم متحكم الجهاز بنقل البيانات. بمجرد الانتهاء، يُعلم متحكم الجهاز المعالج عن طريق إحداث 'مقاطعة' (Interrupt).
يقوم نظام التشغيل بحفظ حالة المعالج (السجلات وعداد البرنامج) ثم ينقل التحكم إلى روتين خدمة المقاطعة (Interrupt Service Routine) المناسب.
I/O devices and the CPU can execute concurrently. Each device controller is in charge of a specific device type and has a local buffer.
When an I/O operation is complete, the device controller informs the CPU by causing an 'interrupt'.
The OS preserves the state of the CPU (registers and program counter) and transfers control to the appropriate interrupt service routine.
نظام التشغيل الحديث هو نظام 'مُقاد بالمقاطعات' (Interrupt Driven).
هناك نوعان رئيسيان: مقاطعة الأجهزة (Hardware Interrupt) ومقاطعة البرمجيات (Software Interrupt) والتي تُعرف أيضاً بـ Trap أو Exception، وتحدث بسبب خطأ (مثل القسمة على صفر) أو طلب خدمة من نظام التشغيل (System Call).
Modern operating systems are 'interrupt driven'.
There are hardware interrupts and software-generated interrupts (Traps or Exceptions), which are caused either by an error (e.g., division by zero) or a specific request from a user program (System Call).
ما الفرق بين المقاطعة (Interrupt) والفخ (Trap)؟ What is the difference between an Interrupt and a Trap?
المقاطعة تُولد بواسطة الأجهزة المادية (Hardware) للإبلاغ عن حدث خارجي (مثل انتهاء قراءة من القرص)، بينما الفخ يُولد بواسطة البرمجيات (Software) بسبب خطأ أو طلب خدمة (System Call).
An interrupt is hardware-generated to signal an external event (like I/O completion), whereas a trap is software-generated caused by an error or a user request (System Call).
3 هرمية التخزين والوصول المباشر للذاكرة (DMA)
3 Storage Hierarchy and Direct Memory Access (DMA)
تنظيم وسائط التخزين بناءً على السرعة، التكلفة، والتطاير. و DMA هي تقنية لنقل البيانات الكبيرة للذاكرة دون إشغال المعالج.
Organizing storage media based on speed, cost, and volatility. DMA is a technique to transfer large data to memory without occupying the CPU.
تُنظم أنظمة التخزين في هرمية: السجلات (Registers) هي الأسرع والأصغر، تليها الذاكرة المخبئية (Cache)، ثم الذاكرة الرئيسية (Main Memory)، ثم الأقراص الصلبة (Magnetic Disks).
التخزين المؤقت (Caching) هو نسخ المعلومات إلى نظام تخزين أسرع مؤقتاً.
بالنسبة لأجهزة الإدخال والإخراج السريعة، يُستخدم 'الوصول المباشر للذاكرة' (DMA)، حيث تقوم وحدة تحكم الجهاز بنقل كتل من البيانات مباشرة إلى الذاكرة الرئيسية دون تدخل المعالج، مما يولد مقاطعة واحدة لكل كتلة بدلاً من مقاطعة لكل بايت.
Storage systems are organized in a hierarchy: Registers (fastest, smallest) -> Cache -> Main Memory -> Solid-state disk -> Magnetic disk.
Caching involves copying information into a faster storage system temporarily.
For high-speed I/O devices, 'Direct Memory Access' (DMA) is used. The device controller transfers blocks of data directly to main memory without CPU intervention, generating only one interrupt per block rather than one per byte.
في بيئات المعالجة المتعددة (Multiprocessor)، يمثل التخزين المؤقت تحدياً يُعرف بـ 'تناسق الذاكرة المخبئية' (Cache Coherency)، حيث يجب التأكد من أن جميع المعالجات تمتلك أحدث قيمة للبيانات في ذاكرتها المخبئية الخاصة بها إذا تم تحديثها بواسطة معالج آخر.
In multiprocessor environments, caching introduces the challenge of 'Cache Coherency'.
Hardware must ensure that all CPUs have the most recent value of a datum in their local cache, especially if it was updated by another CPU.
لماذا تعتبر تقنية DMA ضرورية لأجهزة الإدخال والإخراج السريعة؟ Why is DMA essential for high-speed I/O devices?
لأنه بدون DMA، سيضطر المعالج للتعامل مع مقاطعة لكل بايت يتم نقله، مما سيستهلك كل وقت المعالج ويقلل من أداء النظام بشكل كبير. DMA يسمح بنقل كتل كاملة بمقاطعة واحدة فقط.
Without DMA, the CPU would have to handle an interrupt for every single byte transferred, consuming all CPU cycles and drastically reducing system performance. DMA allows transferring entire blocks with only one interrupt.
4 التشغيل المزدوج والمؤقت
4 Dual-Mode Operation and Timer
آلية حماية تعتمد على الأجهزة لفصل أوامر المستخدم (User Mode) عن أوامر النظام الحساسة (Kernel Mode).
A hardware-supported protection mechanism separating user commands (User Mode) from sensitive system commands (Kernel Mode).
يسمح التشغيل المزدوج (Dual-mode operation) لنظام التشغيل بحماية نفسه ومكونات النظام الأخرى.
يتم توفير 'بت الوضع' (Mode bit) بواسطة الأجهزة: وضع المستخدم (User mode = 1) ووضع النواة (Kernel mode = 0).
بعض التعليمات تُصنف كـ 'تعليمات ذات امتياز' (Privileged instructions) ولا يمكن تنفيذها إلا في وضع النواة. نداء النظام (System Call) يغير الوضع إلى النواة، والعودة من النداء تعيده إلى المستخدم.
بالإضافة إلى ذلك، يُستخدم 'المؤقت' (Timer) لمنع البرامج من الدخول في حلقة لا نهائية أو احتكار الموارد، حيث يولد مقاطعة بعد فترة زمنية محددة.
Dual-mode operation allows the OS to protect itself and other system components.
A 'mode bit' provided by hardware distinguishes User mode (1) and Kernel mode (0).
Certain instructions are designated as 'privileged' and are only executable in kernel mode. A system call changes the mode to kernel, and returning resets it to user.
Additionally, a 'Timer' is used to prevent infinite loops or resource hogging by interrupting the computer after a specified time period.
إعداد المؤقت بحد ذاته هو تعليمة ذات امتياز (Privileged instruction).
إذا حاول برنامج مستخدم تعديل المؤقت، سيرفض المعالج ذلك ويولد فخاً (Trap) لنظام التشغيل للتعامل مع هذا الانتهاك الأمني.
Setting the timer is itself a privileged instruction.
If a user program attempts to modify the timer, the hardware will refuse and trap to the operating system to handle the security violation.
| User Mode | Kernel Mode | |
|---|---|---|
| بت الوضع (Mode Bit)Mode Bit | 11 | 00 |
| تنفيذ التعليمات ذات الامتيازPrivileged Instructions Execution | غير مسموح (يولد فخ)Not allowed (causes trap) | مسموحAllowed |
| الوصول للمواردResource Access | مقيدRestricted | وصول كاملFull access |
كيف نضمن أن المستخدم لا يمكنه تغيير 'بت الوضع' (Mode bit) صراحةً إلى وضع النواة؟ How do we guarantee that a user does not explicitly set the mode bit to 'kernel'?
لأن تعليمة تغيير 'بت الوضع' هي بحد ذاتها تعليمة ذات امتياز (Privileged Instruction)، ولا يمكن تنفيذها إلا إذا كان النظام بالفعل في وضع النواة. الطريقة الوحيدة للمستخدم للانتقال هي عبر نداء النظام (System Call) الذي يتحكم فيه نظام التشغيل.
Because the instruction to change the mode bit is itself a privileged instruction, which can only be executed if the system is already in kernel mode. The only way for a user to transition is via a controlled System Call.
5 نداءات النظام وواجهات برمجة التطبيقات (APIs)
5 System Calls and APIs
الواجهة البرمجية التي تطلب من خلالها برامج المستخدم خدمات من نظام التشغيل.
The programming interface through which user programs request services from the operating system.
نداءات النظام (System Calls) توفر واجهة للخدمات التي يقدمها نظام التشغيل. تُكتب عادةً بلغات عالية المستوى (C أو C++).
بدلاً من استخدام نداءات النظام مباشرة، تصل البرامج إليها غالباً عبر واجهة برمجة التطبيقات (API) مثل Win32 لـ Windows، و POSIX لأنظمة UNIX/Linux، و Java API.
هناك ثلاث طرق رئيسية لتمرير المعلمات (Parameters) لنظام التشغيل: عبر السجلات (Registers)، عبر كتلة/جدول في الذاكرة (Block/Table) وتمرير عنوانها في سجل، أو دفعها في المكدس (Stack) وسحبها بواسطة نظام التشغيل.
System calls provide a programming interface to the services provided by the OS. They are typically written in C or C++.
Programs mostly access them via a high-level Application Programming Interface (API) like Win32, POSIX, or Java API, rather than direct system call use.
There are three general methods to pass parameters to the OS: in registers, stored in a block/table in memory with the address passed in a register, or pushed onto the stack by the program and popped off by the OS.
طريقتا الكتلة (Block) والمكدس (Stack) مفضلتان عندما يكون عدد المعلمات كبيراً، لأنهما لا تقيدان عدد أو طول المعلمات التي يتم تمريرها، على عكس السجلات المحدودة العدد في المعالج.
The block and stack methods do not limit the number or length of parameters being passed, making them preferable over registers when a system call requires a large amount of data or complex structures.
| Registers | Block/Table in Memory | Stack | |
|---|---|---|---|
| حدود حجم/عدد المعلماتParameter Size/Count Limits | محدود بعدد السجلاتLimited by number of registers | غير محدودUnlimited | غير محدودUnlimited |
| الآليةMechanism | تخزين مباشر في السجلDirect storage in register | تخزين في الذاكرة وتمرير العنوان في سجلStore in memory, pass address in register | دفع (Push) من البرنامج، سحب (Pop) من النظامPushed by program, popped by OS |
لماذا يفضل المبرمجون استخدام APIs بدلاً من استدعاء نداءات النظام مباشرة؟ Why do programmers prefer using APIs instead of invoking system calls directly?
لأن الـ APIs توفر قابلية النقل (Portability) عبر أنظمة تشغيل مختلفة، وتخفي التفاصيل المعقدة لواجهة نظام التشغيل، مما يجعل البرمجة أسهل وأقل عرضة للأخطاء.
Because APIs provide portability across different operating systems and hide the complex, low-level details of the OS interface, making programming easier and less error-prone.
6 هيكلة المعالجات المتعددة
6 Multiprocessor Architecture
أنظمة تحتوي على أكثر من معالج لزيادة الإنتاجية والموثوقية.
Systems with more than one processor to increase throughput and reliability.
تنمو أنظمة المعالجات المتعددة (Multiprocessors) في الاستخدام والأهمية. تُعرف أيضاً بالأنظمة المتوازية (Parallel systems).
مزاياها تشمل: زيادة الإنتاجية (Increased throughput)، اقتصاديات الحجم (Economy of scale)، وزيادة الموثوقية (التدهور الرشيق أو التسامح مع الأخطاء).
هناك نوعان: المعالجة المتعددة غير المتماثلة (Asymmetric Multiprocessing) حيث يُخصص لكل معالج مهمة محددة، والمعالجة المتعددة المتماثلة (Symmetric Multiprocessing - SMP) حيث يقوم كل معالج بتنفيذ جميع المهام.
Multiprocessor systems are growing in use. Also known as parallel systems or tightly-coupled systems.
Advantages include increased throughput, economy of scale, and increased reliability (graceful degradation or fault tolerance).
There are two types: Asymmetric Multiprocessing (each processor is assigned a specific task) and Symmetric Multiprocessing (SMP - each processor performs all tasks).
في تصميم SMP الحديث، نجد تصاميم ثنائية النواة (Dual-core) أو متعددة النوى (Multicore) حيث تتواجد عدة نوى معالجة على شريحة واحدة، وتتشارك في الذاكرة المخبئية من المستوى الثاني (L2 Cache) بينما تمتلك كل نواة سجلاتها وذاكرتها المخبئية من المستوى الأول (L1 Cache).
In modern SMP, we see dual-core or multicore designs where multiple compute cores reside on a single chip.
They typically share an L2 cache while maintaining their own registers and L1 cache, improving communication speed between cores.
ما هي الميزة الرئيسية لـ SMP مقارنة بالمعالجة غير المتماثلة؟ What is the main advantage of SMP over Asymmetric Multiprocessing?
في SMP، يتم توزيع عبء العمل ديناميكياً على جميع المعالجات، مما يمنع حالة يكون فيها معالج مثقلاً بالعمل بينما الآخر خامل، مما يحسن كفاءة النظام بشكل عام.
In SMP, the workload is dynamically distributed across all processors, preventing a situation where one processor is overloaded while another sits idle, thereby improving overall system efficiency.