Execution: Processes & Threads التنفيذ: العمليات والخيوط (Processes & Threads)

Understanding the life cycle of a process, scheduling, context switching, and the power of multithreading. فهم دورة حياة العملية، الجدولة، تبديل السياق (Context Switching)، وقوة تعدد الخيوط (Multithreading).

PCB PCB Context Switch Context Switch IPC (Pipes/Shared Mem) IPC (Pipes/Shared Mem) Threads Threads Amdahl's Law Amdahl's Law

The Process العملية (The Process)

A Process is a program in execution. It is the active entity, unlike a program (which is passive code on disk). الـ Process هي برنامج قيد التنفيذ. إنها الكيان النشط، على عكس البرنامج (الذي هو كود خامل على القرص).

Process in Memory العملية في الذاكرة

Stack (Function calls, local vars)
Free Space

Heap (Dynamic Allocation)
Data (Global Variables)
Text (Code Instructions)

Process Control Block (PCB) Process Control Block (PCB)

The data structure the OS uses to manage a process. هيكل البيانات الذي يستخدمه نظام التشغيل لإدارة العملية.

  • Process State:Process State: Running, Waiting, etc. قيد التشغيل، الانتظار، إلخ.
  • Program Counter:Program Counter: Address of next instruction. عنوان التعليمة التالية.
  • CPU Registers:CPU Registers: Accumulators, index registers. المراكم، سجلات الفهرسة.
  • CPU Scheduling Info:CPU Scheduling Info: Priority, pointers. الأولوية، المؤشرات.
  • Memory Mgmt Info:Memory Mgmt Info: Base/Limit registers. سجلات الأساس/الحد.
  • Accounting Info:Accounting Info: CPU time used. وقت CPU المستخدم.
  • I/O Status Info:I/O Status Info: List of open files. قائمة الملفات المفتوحة.

Process State Diagram مخطط حالة العملية

stateDiagram-v2 [*] --> New: Create New --> Ready: Admitted Ready --> Running: Scheduler Dispatch Running --> Ready: Interrupt Running --> Waiting: I/O or Event Wait Waiting --> Ready: I/O or Event Completion Running --> Terminated: Exit Terminated --> [*]

Scheduling الجدولة (Scheduling)

Context Switch تبديل السياق (Context Switch)

When CPU switches to another process, the system must save the state of the old process and load the saved state for the new process. عندما يتحول الـ CPU لعملية أخرى، يجب على النظام حفظ حالة العملية القديمة و تحميل الحالة المحفوظة للعملية الجديدة.

*Pure overhead. The system does no useful work while switching. *تكلفة تشغيلية بحتة (Overhead). النظام لا يقوم بأي عمل مفيد أثناء التبديل.

Operations العمليات

  • Creation:الإنشاء (Creation): Parent creates children, forming a tree. الأب ينشئ الأبناء، مشكلاً شجرة.
    fork(): Creates a duplicate process. ينشئ عملية مكررة.
    exec(): Loads a new binary into memory. يحمل كوداً ثنائياً جديداً للذاكرة.
  • Termination:الإنهاء (Termination): Process executes last statement or `exit()`. العملية تنفذ آخر جملة أو تستدعي `exit()`.
    Zombie:Zombie: Terminated but parent hasn't called `wait()`. انتهت ولكن الأب لم يستدعِ `wait()`.
    Orphan:Orphan: Parent terminated without invoking `wait()`. انتهى الأب دون استدعاء `wait()`.

Inter-Process Communication (IPC) الاتصال بين العمليات (IPC)

Processes can be Independent or Cooperating. Cooperating processes need IPC. يمكن أن تكون العمليات مستقلة أو متعاونة. العمليات المتعاونة تحتاج إلى IPC.

Shared MemoryShared Memory (الذاكرة المشتركة)
  • Fastest (memory speed).الأسرع (سرعة الذاكرة).
  • Convenient for large data.مناسبة للبيانات الكبيرة.
  • Complex synchronization required (locks).تتطلب مزامنة معقدة (أقفال).
Message PassingMessage Passing (تمرير الرسائل)
  • Implemented via OS Kernel (`send()`, `receive()`).تنفذ عبر نواة النظام (`send`، `receive`).
  • No conflict issues (easier sync).لا مشاكل تعارض (مزامنة أسهل).
  • Slower (System call overhead).أبطأ (تكلفة System call).
  • Good for distributed systems.جيدة للأنظمة الموزعة.

Threads الخيوط (Threads)

Why Threads? لماذا Threads؟

A thread is a basic unit of CPU utilization. It is "Lightweight" compared to a process. الخيط هو وحدة أساسية لاستخدام الـ CPU. يعتبر "خفيف الوزن" مقارنة بالعملية.

  • Responsiveness:الاستجابة: UI stays active even if backend is blocked. تظل الواجهة نشطة حتى لو توقفت الخلفية.
  • Resource Sharing:مشاركة الموارد: Threads share code/data by default. تشارك الخيوط الكود/البيانات افتراضياً.
  • Economy:الاقتصاد: Cheaper to create/switch than processes. أرخص في الإنشاء/التبديل من العمليات.
  • Scalability:القابلية للتوسع: Utilizing multicore architectures. استغلال المعالجات متعددة الأنوية.

Multithreading Models نماذج Multithreading

  • Many-to-One:Many-to-One: Many user threads mapped to 1 kernel thread. خيوط مستخدم متعددة ترتبط بخيط نواة واحد.
    Blocking one blocks all. No true parallel running. حجب واحد يحجب الجميع. لا توازي حقيقي.
  • One-to-One:One-to-One: Each user thread maps to a kernel thread. كل خيط مستخدم يرتبط بخيط نواة.
    True concurrency. Creating user thread creates kernel thread (overhead). Used by Linux/Windows. توازي حقيقي. إنشاء خيط مستخدم ينشئ خيط نواة (تكلفة). يستخدم في Linux/Windows.
  • Many-to-Many:Many-to-Many: Many user threads mapped to smaller/equal number of kernel threads. خيوط مستخدم متعددة ترتبط بعدد أقل/مساوٍ من خيوط النواة.
    Flexible but complex. مرن ولكنه معقد.

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

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

TRAP: Amdahl's Law فخ: Amdahl's Law

Adding cores does not mean linear speedup. The serial portion of the program limits the maximum speedup.
Formula: $Speedup \le \frac{1}{S + \frac{1-S}{N}}$
If 25% of code is serial ($S=0.25$), even with infinite cores, max speedup is 4x.
إضافة أنوية لا تعني تسريعاً خطياً. الجزء المتسلسل (Serial) يحد من السرعة القصوى.
المعادلة: $Speedup \le \frac{1}{S + \frac{1-S}{N}}$
إذا كان 25% من الكود متسلسلاً ($S=0.25$)، حتى مع عدد لا نهائي من الأنوية، السرعة القصوى هي 4x.

TRAP: Zombie vs Orphan فخ: Zombie vs Orphan

Zombie: Child finished, parent running but hasn't called wait(). (Entry still in process table).
Orphan: Parent finished without waiting, child still running. (Adopted by `init`). Do not confuse them!
Zombie: الابن انتهى، الأب يعمل لكنه لم يستدعِ wait(). (لا يزال السجل في جدول العمليات).
Orphan: الأب انتهى دون انتظار، الابن لا يزال يعمل. (يتبناه `init`). لا تخلط بينهم!

SECRET: Context Switch Cost سر: تكلفة Context Switch

Context Switching is pure overhead. The system does NO useful work during switching. It depends on memory speed, number of registers, and hardware support. تبديل السياق هو تكلفة إضافية بحتة. النظام لا يقوم بأي عمل مفيد أثناء التبديل. يعتمد على سرعة الذاكرة، عدد السجلات، ودعم الأجهزة.

KEY CONCEPT: Fork() Return Value مفهوم أساسي: قيمة إرجاع Fork()

`fork()` creates a clone.
It returns 0 to the Child Process.
It returns the Child's PID (Non-zero) to the Parent Process. This is how you distinguish who you are in the code.
`fork()` تنشئ نسخة.
ترجع 0 للعملية الابن (Child).
ترجع PID الابن (غير صفري) للعملية الأب (Parent). هكذا تميز "من أنت" داخل الكود.