Sinkronisasi dan Konkruensi di Ruang Kernel
Kelola data bersama dengan aman di kernel menggunakan spinlock, mutex, operasi atomik, serta pemahaman tentang konteks interupsi dibandingkan konteks proses.
Sinkronisasi dan Konkruensi di Ruang Kernel adalah pelajaran Assembly Language & x86 Low-Level Systems Programming gratis di CoddyKit. Ini adalah pelajaran 4 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar Assembly Language & x86 Low-Level Systems Programming, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus Assembly Language & x86 Low-Level Systems Programming mencakup 4 pelajaran total.
Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.
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
Belajar Assembly dengan tutor AI — gratis
Tulis dan jalankan kode asli di browser kamu, dapatkan bantuan instan dari tutor AI 24/7, dan lanjutkan di mana kamu tinggalkan di web atau aplikasi.
- Kursus
- 12
- Pelajaran
- 48
Pertanyaan yang Sering Diajukan
Apakah pelajaran “Sinkronisasi dan Konkruensi di Ruang Kernel” gratis?
Ya — teks lengkap “Sinkronisasi dan Konkruensi di Ruang Kernel” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus Assembly Language & x86 Low-Level Systems Programming, upgrade ke CoddyKit PRO. Kursus Assembly Language & x86 Low-Level Systems Programming mencakup 4 pelajaran total.
Apa yang akan aku pelajari di “Sinkronisasi dan Konkruensi di Ruang Kernel”?
Kelola data bersama dengan aman di kernel menggunakan spinlock, mutex, operasi atomik, serta pemahaman tentang konteks interupsi dibandingkan konteks proses. Kamu berlatih Assembly Language & x86 Low-Level Systems Programming dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.
Apakah aku perlu pengalaman untuk memulai Assembly Language & x86 Low-Level Systems Programming?
Tidak diperlukan pengalaman sebelumnya. Assembly Language & x86 Low-Level Systems Programming di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 4 dari 4.
Berapa lama pelajaran “Sinkronisasi dan Konkruensi di Ruang Kernel” memakan waktu?
Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.
Bisakah aku menulis dan menjalankan kode dalam pelajaran Assembly Language & x86 Low-Level Systems Programming ini?
Ya. Setiap pelajaran Assembly Language & x86 Low-Level Systems Programming menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.
Semua pelajaran dalam kursus ini
- Pengenalan Ruang Kernel
- Menulis Driver Perangkat Sederhana
- Berinteraksi Langsung dengan Perangkat Keras
- Sinkronisasi dan Konkruensi di Ruang Kernel