التزامن والتشغيل المتوازي في مساحة النواة
أدِر البيانات المشتركة بأمان داخل النواة باستخدام أقفال الدوران وكائنات المزامنة والعمليات الذرية، مع فهم الفرق بين سياق المقاطعة وسياق العملية.
التزامن والتشغيل المتوازي في مساحة النواة درس مجاني في Assembly Language & x86 Low-Level Systems Programming على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Assembly Language & x86 Low-Level Systems Programming، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Assembly Language & x86 Low-Level Systems Programming 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
The Concurrency Problem
Kernel code runs in a brutally concurrent environment: multiple CPUs, preemptible threads, and interrupts that fire anytime. Unprotected shared data leads to race conditions and corruption.
Process vs Interrupt Context
Kernel code runs in two contexts:
- Process context: on behalf of a syscall; can sleep
- Interrupt context: handling hardware; must NOT sleep
The context dictates which locking primitive is legal.
Atomic Operations
The simplest protection is an atomic operation that completes in a single uninterruptible step. The kernel offers types like atomic_t with helpers that map to lock-prefixed x86 instructions.
atomic_t counter = ATOMIC_INIT(0);
atomic_inc(&counter);
int v = atomic_read(&counter);How Atomics Work in Hardware
On x86 the lock prefix makes a read-modify-write instruction atomic across cores by asserting a cache-line lock.
lock inc dword [counter] ; atomic increment across CPUsSpinlocks
A spinlock busy-waits until the lock is free. It never sleeps, so it is the only choice in interrupt context. Hold it for the shortest time possible — spinning wastes CPU.
spinlock_t lock;
spin_lock(&lock);
// critical section
spin_unlock(&lock);Spinlocks and Interrupts
If an interrupt handler tries to take a spinlock already held on the same CPU, you deadlock. Use spin_lock_irqsave to disable local interrupts while holding the lock.
unsigned long flags;
spin_lock_irqsave(&lock, flags);
// safe even against IRQs
spin_unlock_irqrestore(&lock, flags);Mutexes and Semaphores
A mutex puts the waiting thread to sleep instead of spinning. It is efficient for longer critical sections but is only usable in process context, never in an interrupt handler.
struct mutex m;
mutex_init(&m);
mutex_lock(&m);
// may sleep here
mutex_unlock(&m);Choosing the Right Primitive
Quick decision guide:
- Short, may run in IRQ context -> spinlock
- Long, process context, can sleep -> mutex
- Single counter or flag -> atomic
Read-Copy-Update (RCU)
RCU allows lock-free reads of shared data while writers create a new copy and swap a pointer. Readers see either the old or new version, never a torn one. It scales superbly for read-mostly structures.
Memory Barriers
Compilers and CPUs reorder memory accesses. A memory barrier (smp_mb(), smp_wmb()) forces ordering so other cores observe writes in the intended sequence — vital for lock-free code.
Deadlock Avoidance
To prevent deadlock: always acquire multiple locks in a fixed global order, keep critical sections tiny, and never call a sleeping function while holding a spinlock or interrupts are disabled.
Quick Check
Test your kernel concurrency knowledge.
Recap
You learned kernel synchronization:
- Atomics protect single values via lock-prefixed instructions
- Spinlocks busy-wait and work in IRQ context; use irqsave variants
- Mutexes sleep and are process-context only
- RCU and memory barriers enable scalable lock-free reads
الأسئلة الشائعة
هل درس «التزامن والتشغيل المتوازي في مساحة النواة» مجاني؟
نعم — نص درس «التزامن والتشغيل المتوازي في مساحة النواة» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Assembly Language & x86 Low-Level Systems Programming، انتقل إلى CoddyKit PRO. تتضمن دورة Assembly Language & x86 Low-Level Systems Programming 4 دروس في المجموع.
ماذا ستتعلم في «التزامن والتشغيل المتوازي في مساحة النواة»؟
أدِر البيانات المشتركة بأمان داخل النواة باستخدام أقفال الدوران وكائنات المزامنة والعمليات الذرية، مع فهم الفرق بين سياق المقاطعة وسياق العملية. تتمرن على Assembly Language & x86 Low-Level Systems Programming مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Assembly Language & x86 Low-Level Systems Programming؟
لا تُشترط خبرة سابقة. Assembly Language & x86 Low-Level Systems Programming على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «التزامن والتشغيل المتوازي في مساحة النواة»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Assembly Language & x86 Low-Level Systems Programming هذا؟
نعم. كل درس في Assembly Language & x86 Low-Level Systems Programming يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- مقدمة إلى مساحة النواة
- كتابة برامج تشغيل أجهزة بسيطة
- التخاطب المباشر مع العتاد
- التزامن والتشغيل المتوازي في مساحة النواة