CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Synchronization.

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

Global Environment Model. MUTUAL EXCLUSION PROBLEM The operations used by processes to access to common resources (critical sections) must be mutually.
Resource management and Synchronization Akos Ledeczi EECE 354, Fall 2010 Vanderbilt University.
Tutorial 3 - Linux Interrupt Handling -
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Mutual Exclusion.
Kernel Synchronization
CS 6560 Operating System Design Lecture 7: Kernel Synchronization Kernel Time Management.
Synchronization. Shared Memory Thread Synchronization Threads cooperate in multithreaded environments – User threads and kernel threads – Share resources.
Operating Systems ECE344 Ding Yuan Synchronization (I) -- Critical region and lock Lecture 5: Synchronization (I) -- Critical region and lock.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
CS533 Concepts of Operating Systems Class 4 Linux Kernel Locking Issues.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Objectives Understand.
CS510 Concurrent Systems Class 1
Synchronization Principles. Race Conditions Race Conditions: An Example spooler directory out in 4 7 somefile.txt list.c scores.txt Process.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
CS510 Concurrent Systems Class 1 Linux Kernel Locking Techniques.
Jonathan Walpole Computer Science Portland State University
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.
1 Race Conditions/Mutual Exclusion Segment of code of a process where a shared resource is accessed (changing global variables, writing files etc) is called.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Introduction to Embedded Systems
Concurrency and Race Conditions Sarah Diesburg COP 5641.
Cosc 4740 Chapter 6, Part 3 Process Synchronization.
Mutual Exclusion. Readings r Silbershatz: Chapter 6.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
CSE 451: Operating Systems Section 5 Midterm review.
Kernel Locking Techniques by Robert Love presented by Scott Price.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
© 2006 RightNow Technologies, Inc. Synchronization September 15, 2006 These people do not actually work at RightNow.
CS533 Concepts of Operating Systems Jonathan Walpole.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
CSC 660: Advanced Operating Systems
Kernel Synchronization in Linux Uni-processor and Multi-processor Environment By Kathryn Bean and Wafa’ Jaffal (Group A3)
Slides created by: Professor Ian G. Harris Operating Systems  Allow the processor to perform several tasks at virtually the same time Ex. Web Controlled.
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.
Implementing Mutual Exclusion Andy Wang Operating Systems COP 4610 / CGS 5765.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Kernel Synchronization David Ferry, Chris Gill CSE 522S - Advanced Operating Systems Washington University in St. Louis St. Louis, MO
Interprocess Communication Race Conditions
CSE 120 Principles of Operating
CS703 – Advanced Operating Systems
CSNB334 Advanced Operating Systems 4
Background on the need for Synchronization
Synchronization.
SYNCHRONIZATION IN LINUX
Jonathan Walpole Computer Science Portland State University
Lecture 2 Part 2 Process Synchronization
Top Half / Bottom Half Processing
Implementing Mutual Exclusion
CS510 Concurrent Systems Class 1a
Implementing Mutual Exclusion
Kernel Synchronization II
CSE 451: Operating Systems Autumn 2004 Module 6 Synchronization
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 451: Operating Systems Winter 2004 Module 6 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
CS510 Concurrent Systems Jonathan Walpole.
Linux Kernel Locking Techniques
CSE 542: Operating Systems
CSE 542: Operating Systems
Presentation transcript:

CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Synchronization

CSC 660: Advanced Operating SystemsSlide #2 Topics 1.Race Conditions 2.Critical Sections 3.Kernel Concurrency 4.What needs Protection? 5.Atomic Operations 6.Spin Locks 7.Semaphores 8.Deadlock

CSC 660: Advanced Operating SystemsSlide #3 Race Condition Example 1.Process A read in, stores in local var slot. 2.Timer interrupt. 3.Scheduler switches to process B. 4.Process B reads in, stores in local var slot. 5.Process B stores filename in slot 7 (slot). 6.Process B updates in to be 8. 7.Scheduler eventually switches to process A. 8.Process A writes filename in slot 7 (slot). 9.Process A computes in = slot Process A updates in to be 8.

CSC 660: Advanced Operating SystemsSlide #4 Race Condition Example abc.pdf prog.c as2.txt out = 4 in = 7 Process A Process B

CSC 660: Advanced Operating SystemsSlide #5 Critical Sections How can we prevent race conditions? Prohibit multiple processes from accessing shared resource at the same time. Critical section: code that accesses shared resource.

CSC 660: Advanced Operating SystemsSlide #6 Critical Sections 1.No two processes may be simultaneously inside their critical sections. 2.No assumptions may be made about speed or number of CPUs. 3.No process running outside its critical section may block other processes from entering the critical section. 4.No process should have to wait forever to enter its critical section.

CSC 660: Advanced Operating SystemsSlide #7 Critical Sections

CSC 660: Advanced Operating SystemsSlide #8 Why do we need atomicity? Process A read i(7) incr i(7 -> 8) - write i(8) Process B read i(7) - incr i(7 -> 8) - write i(8) Problem: Two processes incrementing i. A: read i(7) A: incr i(7 -> 8) B: read i(8) B: incr i(8 -> 9) Uniprocessor Version

CSC 660: Advanced Operating SystemsSlide #9 Atomic Operations Process A atomic_inc i (7->8) Process B - atomic_inc i (8->9) Atomic operations are indivisible. Provided by atomic_t in the kernel. x86 assembly: lock byte preceding opcode makes atomic.

CSC 660: Advanced Operating SystemsSlide #10 Atomicity doesn’t provide Ordering Process A atomic_inc i (7->8) Process B - atomic_inc i (8->9) One atomic order of operations: Another atomic order of operations: Process A - atomic_inc i (8->9) Process B atomic_inc i (7->8)

CSC 660: Advanced Operating SystemsSlide #11 Locking What if operation too complex to be atomic? CPU can’t have a dedicated instruction for every operation that might need to be atomic. Ex: adding a node to a doubly-linked list. Locking Require that thread lock the object before access. Other threads must wait until object unlocked. Locking is simple enough to be atomic. Ex: Acquire lock before modifying linked list.

CSC 660: Advanced Operating SystemsSlide #12 Locking Example with 5 Processes

CSC 660: Advanced Operating SystemsSlide #13 Deadlocks Process A down(A) down(B) Process B down(B) down(A) Single process deadlock: A: spin_lock(A) Two process deadlock:

CSC 660: Advanced Operating SystemsSlide #14 Deadlock Prevention Lock Ordering If you acquire multiple locks, you must always acquire them in the same order. Don’t double acquire a lock. Always use interrupt-disabling variants of spin_lock() in interrupt context. Always release locks. Be sure that your code will always make appropriate calls to release locks.

CSC 660: Advanced Operating SystemsSlide #15 Sources of Concurrency 1.Interrupts 2.Softirqs and tasklets 3.Kernel preemption 4.Sleeping 5.SMP

CSC 660: Advanced Operating SystemsSlide #16 Kernel Preemption Processes can be preempted while in kernel functions. Process A is running an exception handler. It can be preempted if a higher priority process B becomes runnable. Process A is running an exception handler and its time quantum expires. The scheduler will schedule another process to run. Preemption disabled when preempt_count > 0: Interrupt handlers. SoftIRQs and tasklets. Code that explicitly sets preempt_count.

CSC 660: Advanced Operating SystemsSlide #17 What needs Protection? What needs protection? Global kernel data structures. Shared data between process/interrupt context. Shared data between interrupt handlers. What doesn’t? Data local to executing thread (i.e., stack.) Local variables. Dynamically allocated data w/ only local ptrs. Per-CPU data doesn’t need SMP protection.

CSC 660: Advanced Operating SystemsSlide #18 Where is Synchronization Unnecessary? An interrupt handler cannot be interrupted by the same interrupt that it handles. Kernel control path performing interrupt handling cannot be interrupted by a bottom half or system call service routine. SoftIRQs and tasklets cannot be interleaved on the same CPU. The same tasklet cannot be executed simultaneously on multiple CPUs.

CSC 660: Advanced Operating SystemsSlide #19 Kernel Synchronization Techniques TechniqueDescriptionScope Per-CPU varsEach CPU has dataAll CPUs Atomic operation All CPUs Memory barrierAvoid re-orderingLocal or All CPUs Spin lockLock w/ busy waitAll CPUs SemaphoreLock w/ sleep waitAll CPUs SeqlocksLock on access ctrAll CPUs Interrupt disablingcli on a single CPULocal CPU SoftIRQ disablingForbid SoftIRQsLocal CPU Read-copy-update (RCU) Lock-free access to shared data via ptrs. All CPUs

CSC 660: Advanced Operating SystemsSlide #20 Per-CPU Variables One variable exists for each CPU in system. Avoid need for synchronization between CPUs. Potential sources of concurrency Interrupts Kernel-preemption Must disable kernel-preemption What if process pre-empted after initial read, then moved to another CPU before write?

CSC 660: Advanced Operating SystemsSlide #21 Per-CPU Variables int cpu; /* Disable kernel preemption and set cpu to current processor # */ cpu = get_cpu(); /* Manipulate Per-CPU data */ put_cpu();

CSC 660: Advanced Operating SystemsSlide #22 Atomic Operations atomic_t guarantees atomic operations atomic_t v; atomic_t u = ATOMIC_INIT(0); Atomic operations atomic_set(&v, 4); atomic_add(2, &v); atomic_inc(&v); printk(“%d\n”, atomic_read(&v)); atomic_dec_and_test(&v);

CSC 660: Advanced Operating SystemsSlide #23 Barriers provide Ordering Optimization Barriers Prevent compiler from re-ordering instructions. Compiler doesn’t know when interrupts or other processors may read/write your data. Kernel provides barrier() macro. Memory Barriers Read/write barriers prevent loads/stores from being re-ordered across barrier. Kernel provides rmb(), wmb() macros. All syncronization primitives act as barriers.

CSC 660: Advanced Operating SystemsSlide #24 Using a Spin Lock spinlock_t my_lock = SPIN_LOCK_UNLOCKED; spin_lock(&my_lock); /* critical section */ spin_unlock(&my_lock);

CSC 660: Advanced Operating SystemsSlide #25 Spin Locks If lock “open” Sets lock bit with atomic test-and-set. Continues into critical section. else lock “closed” Code “spins” in busy wait loop until available. Waits are typically much less than 1ms. Kernel-preemption can run other processes while task is busy waiting.

CSC 660: Advanced Operating SystemsSlide #26 spinlock_t slock Spin lock state. 1 is unlocked. <1 is locked. break_lock Flag signals that task is busy waiting for this lock.

CSC 660: Advanced Operating SystemsSlide #27 Inside a Spin Lock 1.Disables kernel pre-emption. 2.Atomic test-and-sets lock. 3.If old value positive Lock acquired. 4.Else Enables pre-emption. If break_lock is 0, sets to 1 to indicate a task is waiting. Busy wait loop while (spin_is_locked(lock)) cpu_relax(); # pause instruction on P4 Goto 1.

CSC 660: Advanced Operating SystemsSlide #28 Spin Lock Functions spin_lock_init(spinlock_t *lock) Initialize spin lock to 1 (unlocked). spin_lock(spinlock_t *lock) Spin until lock becomes 1, then set to 0 (locked). spin_lock_irqsave(spinlock_t *l, u flags) Like spin_lock() but disables and saves interrupts. Always use an IRQ disabling variant in interrupt context. spin_unlock(spinlock_t *lock) Set spin lock to 1 (unlocked). spin_lock_irqrestore(spinlock_t *l, u flags) Like spin_lock(), but restores interrupt status. spin_trylock(spinlock_t *lock) Set lock to 0 if unlocked and return 1; return 0 if locked.

CSC 660: Advanced Operating SystemsSlide #29 Read/Write Spinlocks Multiple readers can acquire lock simultaneously. Only one writer can have the lock at a time. Increase concurrency by allowing many readers. Example use: task list

CSC 660: Advanced Operating SystemsSlide #30 Seqlocks Read/write lock where writers never wait. Seqlock = spinlock + sequence counter. typedef struct { unsigned sequence; spinlock_t lock; } seqlock_t Writers gain immediate access. Readers have to check sequence before and after their read of the data to detect writes.

CSC 660: Advanced Operating SystemsSlide #31 Using Seqlocks Writers write_seqlock(); /*... critical section... */ write_sequnlock(); Readers unsigned int seq; do { seq = read_seqbegin(&seqlock); /*... critical section... */ } while (read_seqretry(&seqlock, seq));

CSC 660: Advanced Operating SystemsSlide #32 Semaphores If semaphore “open” Task acquires semaphore. else Task placed on wait queue and sleeps. Task awakened when semaphore released.

CSC 660: Advanced Operating SystemsSlide #33 Semaphores Integer value S with atomic access. If S>0, semaphore prevents access. Using a semaphore for mutual exclusion: down(S); /* critical section */ up(S);

CSC 660: Advanced Operating SystemsSlide #34 Semaphores Down (P): Request to enter critical region. If S > 0, decrements S, enters region. Else process sleeps until semaphore is released. Up (V): Request to exit critical region. Increments S. If S > 0, wakes sleeping processes.

CSC 660: Advanced Operating SystemsSlide #35 Linux Semaphores DECLARE_MUTEX(sem); Static declares a mutex semaphore. void init_MUTEX(struct semaphore *sem); Dynamic declaration of a mutex semaphore. void down(struct semaphore *sem); Decrements semaphore and sleeps. int down_interruptible(struct semaphore *sem); Same as down() but returns on user interrupt. int down_trylock(struct semaphore *sem); Same as down() but returns immediately if not available. void up(struct semaphore *sem); Releases semaphore.

CSC 660: Advanced Operating SystemsSlide #36 Linux Semaphores #include struct semaphore sem; init_MUTEX(&sem); if (down_interruptible(&sem)) return –ERESTARTSYS; /* user interrupt */ /* * critical section */ up(&sem);

CSC 660: Advanced Operating SystemsSlide #37 Read/Write Semaphores One writer or many readers can hold lock. static DECLARE_RWSEM(my_rwsem); down_read(&my_rwsem); /* critical section (read only) */ up_read(&my_rwsem); down_write(&my_rwsem); /* critical section (read/write) */ up_write(&my_rwsem);

CSC 660: Advanced Operating SystemsSlide #38 Spin Locks vs. Semaphores Spin Locks Busy waits waste CPU cycles. Can use in interrupt context, as does not sleep. Cannot use when code sleeps while holding lock. Use for locks held a short time. Semaphores Context switch on sleep is expensive. Sleeps, so cannot use in interrupt context. Can use when code sleeps while holding lock. Use for locks that held a long time.

CSC 660: Advanced Operating SystemsSlide #39 Read-Copy Update (RCU) Lock-free synchronization technique. Allows multiple readers and writers at once. RCU can only be used when All data structures are dynamically allocated and referenced by pointers. No kernel control path can sleep in critical region protected by RCU.

CSC 660: Advanced Operating SystemsSlide #40 Read-Copy Update (RCU) Reader rcu_read_lock(); /* disables kernel preemption */ /* Critical region (read only) */ rcu_read_unlock(); Writer Makes a copy of data structure. Modifies the copy. Switches pointer to point to copy. Deallocates old struct when all readers are done.

CSC 660: Advanced Operating SystemsSlide #41 Key Points Synchronization essential for using shared kernel data. –Sources of concurrency: interrupts, bottoms, kernel preemption, SMP –Atomic operations required to provide synchronization. Kernel provides two major types w/ RW variants –Spin Locks (non-sleeping busy wait lock) –Semaphores (sleeping lock) 2.6 kernel provides new synchronization constructs –Seqlocks –RCU System performance depends strongly on the type of synchronization used. Locks must be acquired in consistent order to avoid deadlocks.

CSC 660: Advanced Operating SystemsSlide #42 References 1.Daniel P. Bovet and Marco Cesati, Understanding the Linux Kernel, 3 rd edition, O’Reilly, Johnathan Corbet et. al., Linux Device Drivers, 3 rd edition, O’Reilly, Robert Love, Linux Kernel Development, 2 nd edition, Prentice-Hall, Claudia Rodriguez et al, The Linux Kernel Primer, Prentice-Hall, Peter Salzman et. al., Linux Kernel Module Programming Guide, version 2.6.1, Andrew S. Tanenbaum, Modern Operating Systems, 3 rd edition, Prentice-Hall, 2005.