Download presentation
Presentation is loading. Please wait.
1
SYNCHRONIZATION IN LINUX
Presented By: Shubham Jaju ( ) Vikash Singh Baghel ( ) Ratna Prakirana ( ) Shubham Joshi ( ) Pushpendra Choudhary
2
Layered Approach to Synchronization
Hardware provides simple low-level atomic operations, upon which we can build high-level, synchronization primitives, upon which we can implement critical sections and build correct multi-process programs. Properly synchronized application High-level synchronization primitives Hardware-provided low-level atomic operations
3
Outline Low-level synchronization primitives in Linux Memory barrier
Atomic operations Synchronize with interrupts Spin locks High-level synchronization primitives in Linux Semaphore monitors Mutex Concurrent/parallel processing
4
Memory barrier definition
Memory Barriers: instructions to compiler and/or hardware to complete all pending accesses before issuing any more Prevent compiler/hardware reordering Read memory barriers: prevent reordering of read instructions Write memory barriers: prevent reordering of write instructions
5
Linux barrier operations
barrier – prevent only compiler reordering mb – prevents load and store reordering rmb – prevents load reordering wmb – prevents store reordering smp_mb – prevent load and store reordering only in SMP kernel smp_rmb – prevent load reordering only in SMP kernels smp_wmb – prevent store reordering only in SMP kernels set_mb – performs assignment and prevents load and store reordering include/asm-i386/system.h
6
Let's consider an example using mb() and rmb()
Let's consider an example using mb() and rmb(). The initial value of a is one and the initial value of b is two. Thread 1 Thread 2 a = 3; - mb(); b = 4; c = b; rmb(); d = a; Without using the memory barriers, on some processors it is possible that c receives the new value of b, whereas d receives the old value of a. For example, c could equal four (what you'd expect), yet d could equal one (not what you'd expect). Using the mb() ensured that a and b were written in the intended order, whereas the rmb() insured c and d were read in the intended order.
7
Signed 24-bit Integer lock
Atomic operations Some instructions not atomic in hardware Read-modify-write instructions that touch memory twice, e.g., inc, xchg Most hardware provides a way to make these instructions atomic Intel lock prefix: appears to lock the memory bus Execute at memory speed Layout of 32 bit atomic_t on SPARC Signed 24-bit Integer lock 31 7
8
Linux atomic operations
atomic_init – initialize an atomic_t variable atomic_read – examine value atomically atomic_set – change value atomically atomic_inc – increment value atomically atomic_dec – decrement value atomically atomic_add - add to value atomically atomic_sub – subtract from value atomically atomic_inc_and_test – increment value and test for zero atomic_dec_and_test – decrement from value and test for zero atomic_sub_and_test – subtract from value and test for zero atomic_set_mask – mask bits atomically atomic_clear_mask – clear bits atomically include/asm-i386/atomic.h
9
Example atomic_t v; /* define v */ atomic_t u = ATOMIC_INIT(0);
/* define u and initialize it to zero */ atomic_set(&v, 4); /* v = 4 (atomically) */ atomic_add(2, &v); /* v = v + 2 =6 atomic_inc(&v); /* v = v + 1 = 7 (atomically) */ All the operations implemented on a specific architecture can be found in <asm/atomic.h>.
10
Linux spin lock operations
spin_lock_init – initialize a spin lock before using it for the first time spin_lock – acquire a spin lock, spin waiting if it is not available spin_unlock – release a spin lock spin_unlock_wait – spin waiting for spin lock to become available, but don't acquire it spin_trylock – acquire a spin lock if it is currently free, otherwise return error spin_is_locked – return spin lock state include/asm-i386/spinlock.h and kernel/spinlock.c
11
Spin_lock Example int thread_fn1() { unsigned long j0,j1;
int delay = 60*HZ; j0 = jiffies; j1 = j0 + delay; spin_lock(&my_lock); while (time_before(jiffies, j1)) schedule(); //schedule( ) implements the scheduler. Its objective is to find a process in the runqueue list and then assign the CPU to it. printk(KERN_INFO "In thread1"); spin_unlock(&my_lock); return 0; }
12
int thread_fn2() { int ret=0; msleep(100); ret=spin_trylock(&my_lock); if(!ret) { printk(KERN_INFO "Unable to hold lock"); return 0; } else { printk(KERN_INFO "Lock acquired"); spin_unlock(&my_lock); }
13
Spin lock usage rules Spin locks should not be held for long periods because waiting tasks on other CPUs are spinning, and thus wasting CPU execution time Remember, don’t call blocking operations (any function that may call schedule()) when holding a spin lock
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.