SYNCHRONIZATION IN LINUX

Slides:



Advertisements
Similar presentations
1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of.
Advertisements

Synchronization NOTE to instructors: it is helpful to walk through an example such as readers/writers locks for illustrating the use of condition variables.
More on Semaphores, and Classic Synchronization Problems CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
Global Environment Model. MUTUAL EXCLUSION PROBLEM The operations used by processes to access to common resources (critical sections) must be mutually.
Chapter 6: Process Synchronization
Background Concurrent access to shared data can lead to inconsistencies Maintaining data consistency among cooperating processes is critical What is wrong.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
CS 6560 Operating System Design Lecture 7: Kernel Synchronization Kernel Time Management.
COMS W6998 Spring 2010 Erich Nahum
Synchronization. Shared Memory Thread Synchronization Threads cooperate in multithreaded environments – User threads and kernel threads – Share resources.
CS533 Concepts of Operating Systems Class 4 Linux Kernel Locking Issues.
CS510 Concurrent Systems Class 1
Semaphores CSCI 444/544 Operating Systems Fall 2008.
CS510 Concurrent Systems Class 1 Linux Kernel Locking Techniques.
CS533 Concepts of Operating Systems Class 17 Linux Kernel Locking Techniques.
CS533 Concepts of Operating Systems Linux Kernel Locking Techniques.
Synchronization CSCI 444/544 Operating Systems Fall 2008.
Discussion Week 3 TA: Kyle Dewey. Overview Concurrency overview Synchronization primitives Semaphores Locks Conditions Project #1.
CS510 Concurrent Systems Introduction to Concurrency.
Cosc 4740 Chapter 6, Part 3 Process Synchronization.
Implementing Synchronization. Synchronization 101 Synchronization constrains the set of possible interleavings: Threads “agree” to stay out of each other’s.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Synchronization.
Kernel Locking Techniques by Robert Love presented by Scott Price.
Synchronization in Linux COMS W4118 Spring Kernel Synchronization Can think of the kernel as a server Concurrent requests are possible Synchronization.
CS533 Concepts of Operating Systems Jonathan Walpole.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
1 Previous Lecture Overview  semaphores provide the first high-level synchronization abstraction that is possible to implement efficiently in OS. This.
Implementing Lock. From the Previous Lecture  The “too much milk” example shows that writing concurrent programs directly with load and store instructions.
CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Synchronization.
CS510 Concurrent Systems Jonathan Walpole. Introduction to Concurrency.
Implementing Mutual Exclusion Andy Wang Operating Systems COP 4610 / CGS 5765.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
W4118 Operating Systems Instructor: Junfeng Yang.
Multiprocessors – Locks
Kernel Synchronization David Ferry, Chris Gill CSE 522S - Advanced Operating Systems Washington University in St. Louis St. Louis, MO
Symmetric Multiprocessors: Synchronization and Sequential Consistency
CS703 – Advanced Operating Systems
Chapter 5: Process Synchronization – Part 3
CSNB334 Advanced Operating Systems 4
Background on the need for Synchronization
Kernel Synchronization
Synchronization.
Atomic Operations in Hardware
Linux Synchronization
Symmetric Multiprocessors: Synchronization and Sequential Consistency
Symmetric Multiprocessors: Synchronization and Sequential Consistency
COP 4600 Operating Systems Fall 2010
Threading And Parallel Programming Constructs
COT 5611 Operating Systems Design Principles Spring 2012
UNIVERSITY of WISCONSIN-MADISON Computer Sciences Department
Coordination Lecture 5.
Implementing Mutual Exclusion
CS510 Concurrent Systems Class 1a
Implementing Mutual Exclusion
Kernel Synchronization II
Userspace Synchronization
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 451: Operating Systems Winter 2003 Lecture 7 Synchronization
CSE 153 Design of Operating Systems Winter 19
CS333 Intro to Operating Systems
Chapter 6: Synchronization Tools
Lecture 20: Synchronization
CS510 Concurrent Systems Jonathan Walpole.
Linux Kernel Locking Techniques
CSE 542: Operating Systems
CSE 542: Operating Systems
Kernel Synchronization
Presentation transcript:

SYNCHRONIZATION IN LINUX Presented By: Shubham Jaju (2011226) Vikash Singh Baghel (2011265) Ratna Prakirana (2011211) Shubham Joshi (2011227) Pushpendra Choudhary

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

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

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

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

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.

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

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

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>.

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

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; }

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); }

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