EECE.4810/EECE.5730 Operating Systems

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Ch 7 B.
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.
CS444/CS544 Operating Systems Synchronization 2/16/2006 Prof. Searleman
Monitors CSCI 444/544 Operating Systems Fall 2008.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
Synchronization Andy Wang Operating Systems COP 4610 / CGS 5765.
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.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
11/18/20151 Operating Systems Design (CS 423) Elsa L Gunter 2112 SC, UIUC Based on slides by Roy Campbell, Sam.
11/21/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.
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.
CS533 – Spring Jeanie M. Schwenk Experiences and Processes and Monitors with Mesa What is Mesa? “Mesa is a strongly typed, block structured programming.
COSC 3407: Operating Systems Lecture 9: Readers-Writers and Language Support for Synchronization.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
CPS110: Thread cooperation Landon Cox. Constraining concurrency  Synchronization  Controlling thread interleavings  Some events are independent  No.
1 Previous Lecture Overview  semaphores provide the first high-level synchronization abstraction that is possible to implement efficiently in OS. This.
Condition Variables Condition variables support three operations: Wait – add calling thread to the condition variable’s queue and put the thread to sleep.
Review: threads and processes Landon Cox January 20, 2016.
Implementing Lock. From the Previous Lecture  The “too much milk” example shows that writing concurrent programs directly with load and store instructions.
3/17/2016cse synchronization-p2 © Perkins, DW Johnson and University of Washington1 Synchronization Part 2 CSE 410, Spring 2008 Computer.
Concurrency: Locks and synchronization Slides by Prof. Cox.
Implementing Mutual Exclusion Andy Wang Operating Systems COP 4610 / CGS 5765.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
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
1 Section 5 Synchronization primitives (Many slides taken from Winter 2006)
CS703 - Advanced Operating Systems
CPS110: Reader-writer locks
CS703 – Advanced Operating Systems
Background on the need for Synchronization
Auburn University COMP 3500 Introduction to Operating Systems Project 3 – Synchronization Condition Variables Dr. Xiao.
Review: threads and synchronization
Monitors, Condition Variables, and Readers-Writers
EECE.4810/EECE.5730 Operating Systems
EECE.4810/EECE.5730 Operating Systems
Anthony D. Joseph and Ion Stoica
Implementing Mutual Exclusion
CSE 451: Operating Systems Autumn Lecture 8 Semaphores and Monitors
Slides by Prof. Landon Cox and Vamsi Thummala
Implementing Mutual Exclusion
Kernel Synchronization II
September 12, 2012 Ion Stoica CS162 Operating Systems and Systems Programming Lecture 5 Semaphores, Conditional Variables.
CS333 Intro to Operating Systems
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Instructor: Dr. Michael Geiger Spring 2019 Lecture 29: Linked queues
Dynamic allocation (continued)
EECE.4810/EECE.5730 Operating Systems
EECE.4810/EECE.5730 Operating Systems
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
CSE 451 Section 1/27/2000.
EECE.4810/EECE.5730 Operating Systems
Instructor: Dr. Michael Geiger Spring 2019 Lecture 23: Exam 2 Preview
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Instructor: Dr. Michael Geiger Spring 2019 Lecture 34: Exam 3 Preview
EECE.4810/EECE.5730 Operating Systems
EECE.4810/EECE.5730 Operating Systems
EECE.4810/EECE.5730 Operating Systems
EECE.4810/EECE.5730 Operating Systems
EECE.4810/EECE.5730 Operating Systems
Don Porter Portions courtesy Emmett Witchel
EECE.4810/EECE.5730 Operating Systems
EECE.4810/EECE.5730 Operating Systems
CPS110: Thread cooperation
Presentation transcript:

EECE.4810/EECE.5730 Operating Systems Instructor: Dr. Michael Geiger Spring 2019 Lecture 12: Synchronization (continued)

Operating Systems: Lecture 10 Lecture outline Announcements/reminders Program 2 to be posted; due date TBD Exam 1: Monday, 2/25, 3-5 PM, Ball 214 Survey posted to address conflicts/schedule alt exam Today’s lecture Synchronization Review: Locks Condition variables Monitors 7/8/2019 Operating Systems: Lecture 10

Operating Systems: Lecture 7 Review: Locks A lock (or mutex) prevents another thread from entering a critical section “Lock fridge while checking milk & shopping” Two operations: lock(): wait until lock is free, then acquire it do { if (lock is free) { // code in red acquire lock // is atomic break out of loop } } while (1); unlock(): release lock 7/8/2019 Operating Systems: Lecture 7

Review: thread-safe queue Thread-safe queue: queue shared between several threads Synch. primitives necessary to restrict access Queues commonly used in OS Track processes/threads waiting for resources Thread-safe queue necessary so multiple kernel threads can enqueue/dequeue items 7/8/2019 Operating Systems: Lecture 7

Case study: thread-safe shared queue 7/8/2019 Operating Systems: Lecture 6

Operating Systems: Lecture 7 Shared queue: no synch void enqueue(struct node *new_element) { // Find queue tail for (ptr = head; ptr->next != NULL; ptr = ptr->next) {} // Add new elem to tail ptr->next = new_element; new_element->next = NULL; } struct node *dequeue() { struct node *elem = NULL; // If queue not empty, // remove first node if (head->next != NULL) { elem = head->next; head->next = head->next->next; } return element; 7/8/2019 Operating Systems: Lecture 7

Shared queue: simple synch void enqueue(struct node *new_element) { qmutex.lock(); // Find queue tail for (ptr = head; ptr->next != NULL; ptr = ptr->next) {} // Add new elem to tail ptr->next = new_element; new_element->next = NULL; qmutex.unlock(); } struct node *dequeue() { struct node *elem = NULL; qmutex.lock(); // If queue not empty, // remove first node if (head->next != NULL) { elem = head->next; head->next = head->next->next; } qmutex.unlock(); return element; 7/8/2019 Operating Systems: Lecture 7

Operating Systems: Lecture 6 Fine-grained locking One lock per queue  only one thread can access queue at once One lock per node: fine-grained locking What’s the major benefit? Lock each node as queue is traversed, release once it’s safe, allowing other threads to traverse queue Hand-over-hand locking necessary 7/8/2019 Operating Systems: Lecture 6

Operating Systems: Lecture 6 Avoiding busy waiting Have dequeuer “go to sleep” Put dequeuer on waiting list, then go to sleep if (queue is empty) { add self to waiting list go to sleep } Wait to be awoken once something’s in queue 7/8/2019 Operating Systems: Lecture 6

Operating Systems: Lecture 6 Ordering constraints What if you want dequeue() to wait if queue is empty? What’s problem with solution below? struct node *dequeue() { struct node *elem = NULL; qmutex.lock(); // Wait for queue to not be empty qmutex.unlock(); while (head->next == NULL); // Remove element, then unlock queue and return elem = head->next; head->next = head->next->next; return elem; } 7/8/2019 Operating Systems: Lecture 6

Ordering constraints, pt. 2 What if you want dequeue() to wait if queue is empty? What’s problem with solution below? struct node *dequeue() { struct node *elem = NULL; qmutex.lock(); // Wait for queue to not be empty while (head->next == NULL) { qmutex.unlock(); } // Remove element, then unlock queue and return elem = head->next; head->next = head->next->next; return elem; 7/8/2019 Operating Systems: Lecture 6

Synchronization types Mutual exclusion Ensures only one thread in critical section “Not at the same time” lock/unlock Condition variables Used when one thread must wait for another to do something “Before/after” dequeue() must wait for enqueue() if empty 7/8/2019 Operating Systems: Lecture 7

Operating Systems: Lecture 7 Condition variables Enable thread to sleep inside critical section by (steps in red are atomic): Release lock Put thread on waiting list Go to sleep After being woken, re-acquire lock() Each condition variable tracks list of threads waiting on that specific condition Each condition variable associated with lock 7/8/2019 Operating Systems: Lecture 7

Condition variable operations wait(&lock) Atomically release lock, add thread to waiting list, then go to sleep Thread must hold lock when calling wait() Must re-acquire lock before exiting wait() signal() Wake up one thread waiting on condition variable If no thread waiting, does nothing broadcast() Wake up all threads waiting on condition variable 7/8/2019 Operating Systems: Lecture 7

Thread-safe queue with CVs 7/8/2019 Operating Systems: Lecture 7

Operating Systems: Lecture 9 Monitors Combine two types of synchronization Lock for mutual exclusion Condition variables for ordering constraints Monitor = shared data + 1+ locks + CVs associated with lock In OOP, “shared objects” 7/8/2019 Operating Systems: Lecture 9

Programming with monitors List shared data needed for problem Assign locks to each group of shared data Enforces mutual exclusion Assign condition variables for every condition thread holding lock may have to wait on Before/after conditions: while (!condition) wait Call signal() or broadcast() when thread changes something another thread might be waiting for Need queue of threads associated with every lock, condition variable Implicitly handled in common thread libraries 7/8/2019 Operating Systems: Lecture 9

Operating Systems: Lecture 9 Monitor queues 7/8/2019 Operating Systems: Lecture 9

Typical monitor programming lock while (!condition) { wait } do work signal other thread(s) about work unlock 7/8/2019 Operating Systems: Lecture 9

Operating Systems: Lecture 10 Final notes Next time Continue synchronization discussion Reminders: Program 2 to be posted; due date TBD Exam 1 date still TBD Respond to scheduling poll ASAP 7/8/2019 Operating Systems: Lecture 10

Operating Systems: Lecture 10 Acknowledgements These slides are adapted from the following sources: Silberschatz, Galvin, & Gagne, Operating Systems Concepts, 9th edition Chen & Madhyastha, EECS 482 lecture notes, University of Michigan, Fall 2016 Dr. Hang Liu 7/8/2019 Operating Systems: Lecture 10