1 Synchronization Threads communicate to ensure consistency If not: race condition (non-deterministic result) Accomplished by synchronization operations.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of.
Synchronization NOTE to instructors: it is helpful to walk through an example such as readers/writers locks for illustrating the use of condition variables.
Ch 7 B.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Chapter 6: Process Synchronization
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.
Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 91 Today’s class Mutual exclusion and synchronization 
Enforcing Mutual Exclusion, Semaphores. Four different approaches Hardware support Disable interrupts Special instructions Software-defined approaches.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts Amherst Operating Systems CMPSCI 377 Lecture.
1 Last Time: Locks & Semaphores Implementing locks Test & Set Busy waiting Block waiting Semaphores Generalization of locks.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Objectives Understand.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
Semaphores CSCI 444/544 Operating Systems Fall 2008.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
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.
Semaphores Questions answered in this lecture: Why are semaphores necessary? How are semaphores used for mutual exclusion? How are semaphores used for.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
CS 241 Section Week #4 (2/19/09). Topics This Section  SMP2 Review  SMP3 Forward  Semaphores  Problems  Recap of Classical Synchronization Problems.
Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.
Pthread (continue) General pthread program structure –Encapsulate parallel parts (can be almost the whole program) in functions. –Use function arguments.
Implementing Synchronization. Synchronization 101 Synchronization constrains the set of possible interleavings: Threads “agree” to stay out of each other’s.
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.
11/18/20151 Operating Systems Design (CS 423) Elsa L Gunter 2112 SC, UIUC Based on slides by Roy Campbell, Sam.
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 11: Thread-safe Data Structures, Semaphores.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Software Systems Advanced Synchronization Emery Berger and Mark Corner University.
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.
PThread Synchronization. Thread Mechanisms Birrell identifies four mechanisms commonly used in threading systems –Thread creation –Mutual exclusion (mutex)
Implementing Lock. From the Previous Lecture  The “too much milk” example shows that writing concurrent programs directly with load and store instructions.
CSCI1600: Embedded and Real Time Software Lecture 17: Concurrent Programming Steven Reiss, Fall 2015.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
Implementing Mutual Exclusion Andy Wang Operating Systems COP 4610 / CGS 5765.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Web Server Architecture Client Main Thread for(j=0;j
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores.
CS162 Section 2. True/False A thread needs to own a semaphore, meaning the thread has called semaphore.P(), before it can call semaphore.V() False: Any.
pThread synchronization
CS 311/350/550 Semaphores. Semaphores – General Idea Allows two or more concurrent threads to coordinate through signaling/waiting Has four main operations.
Case Study: Pthread Synchronization Dr. Yingwu Zhu.
CS703 – Advanced Operating Systems
Background on the need for Synchronization
Chapter 5: Process Synchronization – Part II
Monitors, Condition Variables, and Readers-Writers
Chapter 5: Process Synchronization
Jonathan Walpole Computer Science Portland State University
Lecture 2 Part 2 Process Synchronization
Implementing Mutual Exclusion
Concurrency: Mutual Exclusion and Process Synchronization
CSCI1600: Embedded and Real Time Software
Implementing Mutual Exclusion
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
CSE 153 Design of Operating Systems Winter 2019
CS333 Intro to Operating Systems
Chapter 6: Synchronization Tools
CSCI1600: Embedded and Real Time Software
Lecture 20: Synchronization
“The Little Book on Semaphores” Allen B. Downey
CSE 542: Operating Systems
Presentation transcript:

1 Synchronization Threads communicate to ensure consistency If not: race condition (non-deterministic result) Accomplished by synchronization operations How to write concurrent code How to implement synchronization operations

2 Synchronization – Motivation “The too much milk problem” Model of need to synchronize activities

3 Synchronization Terminology Mutual exclusion (“mutex”) – prevents multiple threads from entering Critical section – regions of code that modify or access shared variables – code only one thread can execute at a time Lock – mechanism for mutual exclusion Lock on entering critical section, accessing shared data Unlock when complete Wait if locked

4 Solving the Too Much Milk Problem Correctness properties Safety: “nothing bad happens” Progress: “something good eventually happens” First: use atomic loads & stores as building blocks “Leave a note” (lock) “Remove a note” (unlock) “Don’t buy milk if there’s a note” (wait)

5 Too Much Milk: Solution 1 thread A if (no milk && no note) leave note buy milk remove note thread B if (no milk && no note) leave note buy milk remove note Does this work? too much milk

6 Too Much Milk: Solution 2 thread A leave note A if (no note B) if (no milk) buy milk remove note A thread B leave note B if (no note A) if (no milk) buy milk remove note B Idea: use labeled notes oops – no milk

7 Language Support Synchronization complicated Better way – provide language-level support Higher-level approach Hide gory details in runtime system Increasingly high-level approaches: Locks, Atomic Operations Semaphores – generalized locks Monitors – tie shared data to synchronization

8 Locks Provide mutual exclusion to shared data via two atomic routines Lock::Acquire – wait for lock, then take it Lock::Release – unlock, wake up waiters Rules: Acquire lock before accessing shared data Release lock afterwards Lock – initially released

9 Too Much Milk: Locks Clean, symmetric - but how do we implement it? thread A Lock.acquire() if (no milk) buy milk Lock.release() thread B Lock.acquire() if (no milk) buy milk Lock.release()

10 Implementing Locks Requires hardware support (in general) Can build on atomic operations: Disable interrupts Uniprocessors only Test & Set, Compare & Swap

11 Disabling Interrupts Prevent scheduler from switching threads in middle of critical sections Ignores quantum expiration (timer interrupt) No handling I/O operations (Don’t make I/O calls in critical section!) To ensure current sequence of instructions run atomically Drawback?

12 Atomic Read-Write-Modify Instructions Atomically read old value, write new value Examples: Test & Set (most arch) Exchange (x86) Compare & Swap (68K, Sparc)

13 Implementing Locks: Test & Set int testset (int value) { int old = value; value = 1; return old; } pseudo-code: red = atomic class Lock { private int value; Lock() { value = 0; } void acquire() {…} void release() {…} } void acquire() { while (testset(value)) {} } void release() { value = 0; } Effect of testset(value) when value = 0? or 1? acquire – wait until lock released, then take it release – release lock value: 1 (locked); 0 (unlocked)

14 Busy Waiting (“Spinning”) What’s wrong with this implementation? CPU utilization? Different priorities? void acquire() { while (testset(value)) {} } void release() { value = 0; } spin-lock

15 Minimizing Busy Waiting Can’t implement locks with test & set without any waiting (w/o disabling interrupts) Add queue to lock and sleep: blocking lock void acquire() { while (1) { if (testset(value)) { put thread on queue, sleep } else { break; } } } void release() { value = 0; wake up threads; }

16 Locks in POSIX pthread_mutex_t my_mutex; pthread_mutex_init(&my_mutex); // also: pthread_mutex_t my_mutex = PTHREAD_MUTEX_INITIALIZER;... pthread_mutex_lock(&my_mutex); x = x + 1;/* This is the critical section */ pthread_mutex_unlock(&my_mutex);... pthread_mutex_destroy(&my_mutex); pthread_mutex_t my_mutex; pthread_mutex_init(&my_mutex); // also: pthread_mutex_t my_mutex = PTHREAD_MUTEX_INITIALIZER;... pthread_mutex_lock(&my_mutex); x = x + 1;/* This is the critical section */ pthread_mutex_unlock(&my_mutex);... pthread_mutex_destroy(&my_mutex);

17 Locks in POSIX: Example Reader-writer problem: Share a buffer which holds one item (an integer) A single reader and writer Reader: only read when there is item in buffer Writer: only write when there is space in buffer

18 Locks in POSIX: Example - II #include int buffer_has_item = 0; int buffer; pthread_mutex_t mutex; void * reader_function(void *) { while(1){ pthread_mutex_lock( &mutex ); if ( buffer_has_item == 1) { printf("reader consumes one item: %d.\n", buffer); buffer_has_item = 0; } pthread_mutex_unlock( &mutex ); }

19 Locks in POSIX: Example - III void writer_function(void) { while(1) { pthread_mutex_lock( &mutex ); if ( buffer_has_item == 0 ){ buffer = rand()*100; printf("writer produces one item: %d\n", buffer); buffer_has_item = 1; } pthread_mutex_unlock( &mutex ); } void main() { pthread_t reader; pthread_mutex_init(&mutex, NULL); pthread_create( &reader, NULL, reader_function,NULL); writer_function(); }

20 Locks as Synch Primitive +Locks provide mutual exclusion +Only one thread enters section at a time +Simplifies writing concurrent programs - Low-level - Can complicate coding - e.g., “allow at most n threads to access resource”  What are some alternatives?

21 Locks & Semaphores Implementing locks Semaphores Monitors

22 Semaphores What’s a “semaphore” anyway? A visual system for sending information by means of two flags that are held one in each hand, using an alphabetic code based on the position of the signaler's arms.

23 Semaphores What’s a “semaphore” anyway? A visual signaling apparatus with flags, lights, or mechanically moving arms.

24 Semaphores in CS Computer science: Dijkstra (1965) A non-negative integer counter with atomic increment & decrement. Blocks rather than going negative. Higher-level than locks but not too high level

25 Semaphores: Key Concepts P(sem), a.k.a. wait = decrement counter If sem = 0, block until greater than zero P = “prolagen” (proberen te verlagen, “try to decrease”) In Holland the good Dr. Dijkstra Took a break from a walk on his bijkstra And said: "Which shall it be? Take a P or a V? For the two seem to me quite alijkstra!" V(sem), a.k.a. signal = increment counter Wake 1 waiting process V = “verhogen” (“increase”)

26 Implementing Semaphores class Semaphore { private int value; private Queue q; Semaphore(int v) { value = v; } } void wait() { if (value > 0) { value = value – 1; } if (value == 0) { add this process to Q; sleep(); } void signal() { value = value + 1; if (anyone on Q) { remove P from Q; wakeup(P); }

27 Variants of Semaphores Binary semaphore just two values (0 or 1), typically initial value 1 (“free”) Counting semaphore useful when units of resource are available initialized to number of resources  thread can access as long as one unit available

28 Binary Semaphores: Example “too much milk” with locks thread A Lock.acquire() if (no milk) buy milk Lock.release() thread B Lock.acquire() if (no milk) buy milk Lock.release() thread A sem.wait() if (no milk) buy milk sem.signal() thread B sem.wait() if (no milk) buy milk sem.signal() “too much milk” with binary semaphores (initially 1)

29 Binary Semaphore: Example More flexible than locks! By initializing semaphore to 0, threads can wait for an event to occur thread A // wait for thread B sem.wait(); // do stuff … thread B // do stuff, then // wake up A sem.signal();

30 Counting Semaphores: Example Controlling resources: Allow threads to use at most 5 files simultaneously Initialize to 5 thread A sem.wait(); // use a file sem.signal(); thread B sem.wait(); // use a file sem.signal();

31 Semaphore in POSIX sem_t sem; sem_init(&sem, int pshared, unsigned value); sem_wait(&sem); sem_post(&sem); sem_destroy(&sem); sem_t sem; sem_init(&sem, int pshared, unsigned value); sem_wait(&sem); sem_post(&sem); sem_destroy(&sem);

32 Solving Reader-writer Problem Using Semaphore Reader-writer problem: Share a buffer which holds one item (an integer) A single reader and writer Reader: only read when there is item in buffer Writer: only write when there is space in buffer How to make the writer enter the critical section first? How to make the reader & writer alternate to enter critical section?

33 Semaphore in Linux: Solving Reader-writer Problem #include sem_t readers_turn, writers_turn; /* semaphore declaration */ int buffer, loopcnt = 5; void * reader_function(void *) { int i; for (i=0; i<loopcnt; i++) { sem_wait( &readers_turn ); printf("reader consumes one item: %d.\n", buffer); sem_post( &writers_turn ); } void writer_function(void) { int i; for (i=0; i<loopcnt; i++) { sem_wait( &writers_turn ); buffer = rand(); printf("writer produces one item: %d\n", buffer); sem_post( &readers_turn ); }

34 Semaphore in Linux: Solving Reader-writer Problem - II int main() { pthread_t reader; if (sem_init(&readers_turn, 0, 0) < 0) { perror("sem_init"); exit(1); } if (sem_init(&writers_turn, 0, 1) < 0) { perror("sem_init"); exit(1); } if(pthread_create( &reader, NULL, reader_function, NULL) != 0) { perror("pthread_create"); exit(1); } writer_function(); sem_destroy(&readers_turn); sem_destroy(&writers_turn); return 1; }

35 Summary Implementing locks Test & Set Spin locks, blocking locks Semaphores Generalization of locks Binary, counting (“Dijkstra-style”) Useful for: Controlling resources Waiting on events