การซิงโครไนซ์และการทำงานพร้อมกันในพื้นที่เคอร์เนล
จัดการข้อมูลที่ใช้ร่วมกันในเคอร์เนลอย่างปลอดภัยด้วยสปินล็อก มิวเท็กซ์ การดำเนินการแบบอะตอมิก และความเข้าใจความแตกต่างระหว่างบริบทอินเทอร์รัพท์กับบริบทโพรเซส
การซิงโครไนซ์และการทำงานพร้อมกันในพื้นที่เคอร์เนล เป็นบทเรียน Assembly Language & x86 Low-Level Systems Programming ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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
เรียนรู้ Assembly ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 48
คำถามที่พบบ่อย
บทเรียน “การซิงโครไนซ์และการทำงานพร้อมกันในพื้นที่เคอร์เนล” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การซิงโครไนซ์และการทำงานพร้อมกันในพื้นที่เคอร์เนล” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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 ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 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 ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- แนะนำพื้นที่เคอร์เนล
- การเขียนไดรเวอร์อุปกรณ์อย่างง่าย
- การเชื่อมต่อกับฮาร์ดแวร์โดยตรง
- การซิงโครไนซ์และการทำงานพร้อมกันในพื้นที่เคอร์เนล