Chapter 28 Locks Chien-Chung Shen CIS, UD

Slides:



Advertisements
Similar presentations
More on Semaphores, and Classic Synchronization Problems CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
Advertisements

Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
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.
5.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 5: CPU Scheduling.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
1 Operating Systems, 122 Practical Session 5, Synchronization 1.
Mutual Exclusion.
CSE 451: Operating Systems Winter 2012 Synchronization Mark Zbikowski Gary Kimura.
CSE 451: Operating Systems Spring 2012 Module 7 Synchronization Ed Lazowska Allen Center 570.
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.
CS444/CS544 Operating Systems Synchronization 2/21/2006 Prof. Searleman
Lecture 11 PA2, lock, and CV. Lab 3: Demand Paging Implement the following syscalls xmmap, xmunmap, vcreate, vgetmem/vfreemem, srpolicy Deadline: March.
CS444/CS544 Operating Systems Synchronization 2/16/2007 Prof. Searleman
W4118 Operating Systems Instructor: Junfeng Yang.
Synchronization (other solutions …). Announcements Assignment 2 is graded Project 1 is due today.
Synchronization Solutions
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Synchronization CSCI 444/544 Operating Systems Fall 2008.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
9/8/2015cse synchronization-p1 © Perkins DW Johnson and University of Washington1 Synchronization Part 1 CSE 410, Spring 2008 Computer Systems.
Implementing Synchronization. Synchronization 101 Synchronization constrains the set of possible interleavings: Threads “agree” to stay out of each other’s.
1 Process Synchronization – Outline Why do processes need synchronization ? What is the critical-section problem ? Describe solutions to the critical-section.
COMP 111 Threads and concurrency Sept 28, Tufts University Computer Science2 Who is this guy? I am not Prof. Couch Obvious? Sam Guyer New assistant.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
Lecture 10 Locks.
Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 11: October 5, 2010 Instructor: Bhuvan Urgaonkar.
Lecture 12 CV. Last lecture Controlling interrupts Test and set (atomic exchange) Compare and swap Load linked and store conditional Fetch and add and.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
1 Synchronization Threads communicate to ensure consistency If not: race condition (non-deterministic result) Accomplished by synchronization operations.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
IT 344: Operating Systems Module 6 Synchronization Chia-Chi Teng CTB 265.
CSE 153 Design of Operating Systems Winter 2015 Lecture 5: Synchronization.
Implementing Lock. From the Previous Lecture  The “too much milk” example shows that writing concurrent programs directly with load and store instructions.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
Implementing Mutual Exclusion Andy Wang Operating Systems COP 4610 / CGS 5765.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
pThread synchronization
CSE 120 Principles of Operating
Operating Systems Concurrency ENCE 360.
Background on the need for Synchronization
Synchronization.
Chapter 5: Process Synchronization
Chien-Chung Shen CIS/UD
Concurrency: Locks Questions answered in this lecture:
Topic 6 (Textbook - Chapter 5) Process Synchronization
Jonathan Walpole Computer Science Portland State University
Mutual Exclusion.
UNIVERSITY of WISCONSIN-MADISON Computer Sciences Department
Coordination Lecture 5.
Implementing Mutual Exclusion
Implementing Mutual Exclusion
CSE 451: Operating Systems Winter 2007 Module 6 Synchronization
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
CSE 451: Operating Systems Winter 2007 Module 6 Synchronization
CSE 451: Operating Systems Autumn 2009 Module 7 Synchronization
Process/Thread Synchronization (Part 2)
Presentation transcript:

Chapter 28 Locks Chien-Chung Shen CIS, UD

Basic Ideas Problem on concurrent programming – like to execute a sequence of instructions atomically on single CPU with interrupts Solution: put locks around critical sections lock_t mutex; // some globally-allocated lock ’mutex’ … lock(&mutex); balance = balance + 1; // critlcal section unlock(&mutex);

Pthread Locks pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; Pthread_mutex_lock(&lock) ; // wrapper for pthread_mutex_lock() balance = balance + 1; Pthread_mutex_unlock(&lock); Course-grained locking vs. fine-grained locking –“Long” vs. “short” critical sections –example with file access

Evaluating Locks Three criteria –Mutual exclusion - correctness –Fairness – avoid starvation –Performance - overhead

Controlling Interrupts void lock () { disableInterrupt(); } void unlock() { enable Interrupt(); } Negatives: –Allow calling thread to perform privileged operation –Does not work on multiprocessors

First Attempt – use a variable typedef struct __lock_t { int flag; } lock_t; void init(lock_t *mutex) { mutex->flag = 0; // 0 -> lock is available, 1 -> held } void lock(lock_t *mutex) { while (mutex->flag == 1) // TEST the flag ; // spin-wait (do nothing) mutex->flag = 1; // now SET it! } void unlock(lock_t *mutex) { mutex->flag = 0; } What problems does this solution have?

Code Interleaving Thread 1 Thread 2 flag == 0 call lock() while (flag == 1) interrupt: switch to Thread 2 call lock() while (flag == 1) flag = 1; interrupt: switch to Thread 1 flag = 1; // set flag to 1 (too!) Problems: –correctness – no guarantee of mutual exclusion –Performance – spin-waiting

Test-and-Set Semantics int TestAndSet(int *ptr, int new) { int old = *ptr; // fetch old value at ptr *ptr = new; // store ’new’ into ptr return old; // return the old value } Returns the old value pointed to by ptr, and simultaneously updates said value to new Make “test” (of old lock value) and “set” (of new value) a single atomic operation SPARC – ldstub // load/store unsigned byte x86 – xchg // atomic exchange

Spin Lock with Test-and-Set typedef struct __lock_t { int flag; } lock_t; void init(lock_t *mutex) { mutex->flag = 0; // 0 -> lock is available, 1 -> held } void lock(lock_t *mutex) { while (TestAndSet(&lock->flag, 1) == 1) // TEST the flag ; // spin-wait (do nothing) } void unlock(lock_t *mutex) { mutex->flag = 0; } As long as the lock is held by another thread, TestAndSet() will repeatedly return 1, and thus the calling thread will spin-wait What kind of scheduler do we need on single processor? –preemptive scheduler (interrupt threads via timer)

Evaluation of Spin Lock Three criteria –Mutual exclusion – correctness yes –Fairness – avoid starvation no –Performance – overhead Bad on single CPU Reasonably well on multiple CPUs, assuming critical sections are short

Compare-and-Swap On x86 - compare-and-exchange cmpxchgl Semantics int CompareAndSwap(int *ptr, int expected, int new) { int actual = *ptr; if (actual == expected) *ptr = new; return actual; } Lock void lock(lock_t *lock) { while (CompareAndSwap(&lock->flag, 0, 1) == 1) ; // spin } More powerful than Test-and-Set

Ticket Lock with Fetch&Add int FetchAndAdd(int *ptr) { // semantics int old = *ptr; *ptr = old + 1; return old; } typedef struct __lock_t { int ticket; int turn; } lock_t; void lock_init(lock_t *lock) { lock->ticket = 0; lock->turn = 0; } void lock(lock_t *lock) { int myturn = FetchAndAdd(&lock->ticket); // get a ticket while (lock->turn != myturn) ; // spin if not my turn } void unlock(lock_t *lock) { FetchAndAdd(&lock->turn); // enable the next waiting thread } Anything good ? –ensure progress for all threads and fair

How to Avoid Spinning ? Need OS support, in addition to hardware void init() { flag = 0; } void lock() { while (TestAndSet(&flag, 1) == 1) // TEST the flag yield(); // give up CPU and move to READY state } void unlock() { flag = 0; } Another overhead ? (think 100 threads) –context switching overhead Still one problem not solved –starvation

Sleeping Instead of Spinning Explicitly exert some control over who gets to acquire the lock next after the current holder releases it What “data structure” would you use? –queue park() – put calling thread to sleep unpark() – wake up a thread

Queue and Yield/Wakeup typedef struct __lock_t { int flag; int guard; queue_t *q; } lock_t; void lock_init(lock_t *m) { m->flag = 0; m->guard = 0; queue_init(m->q); } void lock(lock_t *m) { while (TestAndSet(&m->guard, 1) == 1) ; //acquire guard lock by spinning if (m->flag == 0) { m->flag = 1; // lock is acquired m->guard = 0; } else { queue_add(m->q, gettid()); // added to the lock’s queue m->guard = 0; park(); // put calling thread to sleep } void unlock(lock_t *m) { while (TestAndSet(&m->guard, 1) == 1) ; //acquire guard lock by spinning if (queue_empty(m->q)) m->flag = 0; // let go of lock; no one wants it else unpark(queue_remove(m->q)); // hold lock (for next thread!) m->guard = 0; }

Questions void lock(lock_t *m) { while (TestAndSet(&m->guard, 1) == 1) ; //acquire guard lock by spinning if (m->flag == 0) { m->flag = 1; // lock is acquired m->guard = 0; } else { queue_add(m->q, gettid()); // added to the lock’s queue x: m->guard = 0; y: park(); // put calling thread to sleep } } Why is guard used? Can x and y be swapped ? void unlock(lock_t *m) { while (TestAndSet(&m->guard, 1) == 1) ; //acquire guard lock by spinning if (queue_empty(m->q)) m->flag = 0; // let go of lock; no one wants it else unpark(queue_remove(m->q)); // hold lock (for next thread!) m->guard = 0; } Why flag does not get set to 0 when another thread gets woken up? –the waking thread does not hold the guard