Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Similar documents
More Synchronization; Concurrency in Java. CS 475, Spring 2018 Concurrent & Distributed Systems

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

Lesson 6: Process Synchronization

Chapter 5: Process Synchronization. Operating System Concepts Essentials 2 nd Edition

CS370 Operating Systems

CSE 4/521 Introduction to Operating Systems

CS370 Operating Systems

Chapter 5: Process Synchronization

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

Chapter 6: Process Synchronization

EI 338: Computer Systems Engineering (Operating Systems & Computer Architecture)

Chapter 6: Synchronization. Operating System Concepts 8 th Edition,

Chapter 6: Synchronization. Chapter 6: Synchronization. 6.1 Background. Part Three - Process Coordination. Consumer. Producer. 6.

Process Synchronization

Synchronization Principles

Process Synchronization

9/29/2014. CS341: Operating System Mid Semester Model Solution Uploaded Semaphore ADT: wait(), signal()

Chapter 6: Process Synchronization

Chapter 5: Process Synchronization

Chapter 5: Process Synchronization

Chapter 6: Process Synchronization

Chapter 7: Process Synchronization!

Interprocess Communication By: Kaushik Vaghani

Process Synchronization

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy

Process Synchronization: Semaphores. CSSE 332 Operating Systems Rose-Hulman Institute of Technology

Process Synchronization

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

CHAPTER 6: PROCESS SYNCHRONIZATION

Process Synchronization

Chapter 7: Process Synchronization. Background. Illustration

Chapter 6: Process Synchronization. Operating System Concepts 8 th Edition,

Chapter 7: Process Synchronization. Background

Process Synchronization(2)

Process Synchronization(2)

Prof. Hui Jiang Dept of Computer Science and Engineering York University

Chapter 6: Process Synchronization. Module 6: Process Synchronization

Process Synchronization

Semaphore. Originally called P() and V() wait (S) { while S <= 0 ; // no-op S--; } signal (S) { S++; }

Systems Fundamentals. SWE 622, Spring 2017 Distributed Software Engineering

CSE Opera,ng System Principles

COP 4225 Advanced Unix Programming. Synchronization. Chi Zhang

Process Synchronization(2)

Module 6: Process Synchronization

Process Synchronization. CISC3595, Spring 2015 Dr. Zhang

CS370 Operating Systems

Real-Time Operating Systems M. 5. Process Synchronization

Process Coordination

Chapter 6: Process Synchronization. Operating System Concepts 8 th Edition,

Synchronization. Race Condition. The Critical-Section Problem Solution. The Synchronization Problem. Typical Process P i. Peterson s Solution

Concurrency and Synchronisation

Process Synchronization. studykorner.org

Introduction to Operating Systems

Concurrency and Synchronisation

Learning Outcomes. Concurrency and Synchronisation. Textbook. Concurrency Example. Inter- Thread and Process Communication. Sections & 2.

Background. Module 6: Process Synchronization. Bounded-Buffer (Cont.) Bounded-Buffer. Background

Synchronization Basic Problem:

Process Synchronization

Synchronization. CSE 2431: Introduction to Operating Systems Reading: Chapter 5, [OSC] (except Section 5.10)

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Process Management And Synchronization

CS420: Operating Systems. Process Synchronization

CS3502 OPERATING SYSTEMS

Semaphores. To avoid busy waiting: when a process has to wait, it will be put in a blocked queue of processes waiting for the same event

Semaphores. Semaphores. Semaphore s operations. Semaphores: observations

Module 6: Process Synchronization. Operating System Concepts with Java 8 th Edition

Chapter 6 Synchronization

Dealing with Issues for Interprocess Communication

Process Co-ordination OPERATING SYSTEMS

Processes. Rafael Ramirez Dep Tecnologia Universitat Pompeu Fabra

Chapter 6: Process Synchronization

Chapter 6: Process Synchronization

CS370 Operating Systems Midterm Review. Yashwant K Malaiya Spring 2019

Deadlock and Monitors. CS439: Principles of Computer Systems September 24, 2018

CS3733: Operating Systems

Background. The Critical-Section Problem Synchronisation Hardware Inefficient Spinning Semaphores Semaphore Examples Scheduling.

Module 6: Process Synchronization

High-level Synchronization

Semaphores (by Dijkstra)

Operating Systems Antonio Vivace revision 4 Licensed under GPLv3

Lecture Outline. CS 5523 Operating Systems: Concurrency and Synchronization. a = 0; b = 0; // Initial state Thread 1. Objectives.

Chapter 6: Synchronization

CSE 120. Fall Lecture 6: Semaphores. Keith Marzullo

Lecture 3: Synchronization & Deadlocks

Process Synchronization

Maximum CPU utilization obtained with multiprogramming. CPU I/O Burst Cycle Process execution consists of a cycle of CPU execution and I/O wait

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

Process Synchronization

1. Motivation (Race Condition)

Synchronization Classic Problems

Synchronization Principles II

CSC501 Operating Systems Principles. Process Synchronization

Lecture Topics. Announcements. Today: Concurrency (Stallings, chapter , 5.7) Next: Exam #1. Self-Study Exercise #5. Project #3 (due 9/28)

Introduction to OS Synchronization MOS 2.3

OS Process Synchronization!

Background. Old Producer Process Code. Improving the Bounded Buffer. Old Consumer Process Code

Lecture 5: Inter-process Communication and Synchronization

PESIT Bangalore South Campus

UNIT 2 Basic Concepts of CPU Scheduling. UNIT -02/Lecture 01

Classic Problems of Synchronization

Transcription:

Synchronization CS 475, Spring 2018 Concurrent & Distributed Systems

Review: Threads: Memory View code heap data files code heap data files stack stack stack stack m1 m1 a1 b1 m2 m2 a2 b2 m3 m3 a3 m4 m4 Single-Threaded Process Multi-Threaded Process Each thread might be executing totally different code, too 2

Review: Race Conditions Thread 1 Thread 2 static int i = 0; public static void increment() { i = i + 1; } increment() read i = 0 increment() write i = 1 read i = 0 write i =1 3

Review: Critical Section in increment() static int i = 0; public static void increment() { entersection(); i = i + 1; exitsection(); } Only one thread can read/write i at once But how to implement entersection() and exitsection()? 4

Announcements Additional readings: OS TEP Ch 28.1-28.4; Ch 31 Reminder: HW1 is due 2pm on Weds 5

Critical Section Problem Each thread/process has some critical section of code that: Changes shared variables, files etc When one thread/process is in a critical section, no other may be in the same critical section Critical section problem: design a protocol to solve this Each process/thread asks for permission to enter critical section, does its work, then exits the section 6

Solution to Critical-Section Problem Need to guarantee mutual exclusion If one thread/process is executing its critical section, no other can execute in their critical sections Need to guarantee progress: If no process is executing in its critical section, and some other would like to, then some process must be allowed to continue Need to guarantee bounded waiting: If some process wants to enter its critical section, it must eventually be granted access 7

Peterson s Solution Simple algorithm that solves critical section problem Requires two variables: int turn, boolean flag[] turn indicates which process can enter the critical section flag is an array indicating which process is ready to enter its critical section, flag[i] = true means Pi is ready to enter its critical section 8

Peterson s Solution Algorithm for Pi while(true) { flag[i] = true; turn = j; while(flag[j] && turn == j); //wait if j is in its critical section //critical section flag[i] = false; //signal we are done //do anything else that is not in critical section } Busy waiting Problem: Inefficient - this thread keeps checking flag[], preventing other things from running on your CPU Sidebar: tough to do correctly on modern hardware, e.g. JVM (Double Checked Locking Manifesto) 9

Busy Waiting while(true) { flag[i] = true; turn = j; while(flag[j] && turn == j); //wait if j is in its critical section //critical section flag[i] = false; //signal we are done //do anything else that is not in critical section } Wait for lock Acquire lock Acquire lock Release lock Time T1 T2 T1 T2 T1 T2 T1 T1 T2 T2 T1 10

Locks Most systems have some hardware support for implementing this, based on locks This tells the OS and processor that when a thread is waiting for a lock, not to bother running it until it can receive the lock Wait for lock Acquire lock Acquire lock Release lock T1 finished faster, T2 got to start sooner T1 T2 T1 T1 T1 T1 T2 T1 T2 T2 T1 Time Wait for lock Acquire lock Acquire lock Release lock T1 T2 T1 T2 T1 T2 T1 T1 T2 T2 T1 11

Mutex Locks Previous solutions are complicated and generally inaccessible to application programmers OS designers build software tools to solve critical section problem Simplest is mutex lock Protect a critical section by first acquire() a lock then release() the lock Calls to acquire() and release() must be atomic Usually implemented via hardware atomic instructions Ideally: automatically sleeps calling thread if not available Otherwise, called a spinlock 12

Semaphores Synchronization tool that provides more sophisticated ways (than Mutex locks) for process to synchronize their activities. Semaphore S integer variable Can only be accessed via two indivisible (atomic) operations wait(): consumes a resource (once available) signal(): release a resource 13

Sempahores Counting semaphore integer value can range over an unrestricted domain Binary semaphore integer value can range only between 0 and 1 Same as a mutex lock Can solve various synchronization problems 14

Semaphores Define a semaphore as a record: typedef struct { int value; struct process *L; } semaphore; Assume two simple operations provided by OS: block suspends the process that invokes it. wakeup(p) resumes the execution of a blocked process P. 15

Semaphores Implementation of wait and signal. Starting value is # of permits wait(s): S.value--; if (S.value < 0) { enqueue(this,s.l); block(); } signal(s): S.value++; if (S.value <= 0) { Thread towake = pop(s.l); wakeup(towake); } 16

Deadlocks & Starvation Starvation: one or more threads are blocked from gaining access to a resource, and hence, can t make progress Deadlock: Two or more threads are waiting for the others to do something T1: Has lock 1, needs lock 2 T2: Has lock 2, needs lock 1 Hence, neither T1 nor T2 can execute 17

Classical Problems of Synchronization Bounded-Buffer Problem Readers and Writers Problem Dining-Philosophers Problem 18

Bounded Buffer Producer/consumer communicating through a buffer that holds n items Producer can t put too many items in at one time, consumer can t remove if no items there Might have multiple producers, consumers Write Read Producer Buffer Consumer Example: buffer can only hold 2 items 19

Bounded Buffer What needs to be tracked? Number of items in buffer Which threads are waiting to put into buffer Which threads are waiting to pull out of buffer 20

Bounded Buffer with Semaphores Semaphore mutex initialized to the value 1 Semaphore full initialized to the value 0 Semaphore empty initialized to the value n mutex =1 Makes sure buffer is viewed atomically full = 0 empty = n Represents number of full slots, tracks threads waiting to add more (if totally full) Represents number of empty slots, tracks threads waiting to read (if empty) 21

Bounded Buffer Pseudocode Producer do { /* produce an item in next_produced */ wait(empty); wait(mutex); /* add next produced to the buffer */ signal(mutex); signal(full); } while (true); Consumer do { wait(full); wait(mutex); /* remove an item from buffer to next_consumed */ signal(mutex); signal(empty); /* consume the item in next consumed */ } while (true); 22

Bounded Buffer Example Producer Write Buffer Read Consumer Example: buffer can only hold 2 items mutex =10 full = 012 empty = 2 10 Makes sure buffer is viewed atomically Represents number of full slots, tracks threads waiting to add more (if totally full) Represents number of empty slots, tracks threads waiting to read (if empty) 23

Readers and Writers Problem Commonly called a read-write lock: data can be read by an unlimited number of threads at a time, written by at most one If a thread wants to write, nobody else can be reading High level approach: Semaphore used to guard writing Integer variable used to track number of readers First reader acquires write semaphore, last reader to finish releases it 24

Readers + Writers with Semaphores Semaphore rw_mutex initialized to 1 Semaphore mutex initialized to 1 Integer read_count initialized to 0 rw_mutex =1 Guards writing read_count=0 Tracks number of clients reading mutex = 1 Guards updates to read_count 25

Readers Writers Pseudocode Writer do { wait(rw_mutex); /* writing is performed */ signal(rw_mutex); } while (true); Readers do { wait(mutex); read_count++; if (read_count == 1) wait(rw_mutex); signal(mutex); /* reading is performed */ wait(mutex); read count--; if (read_count == 0) signal(rw_mutex); signal(mutex); } while (true); 26

Readers + Writers Example Reader Reader Reader Writer First to arrive, set rw_mutex=0 Increment read_count Decrement read_count Increment read_count Decrement read_count Increment read_count Decrement read_count, Released rw_mutex Blocked: Unable to acquire rw_mutex Holds rw_mutex rw_mutex =1 0 Waiting: writer Guards writing read_count=0123 Tracks number of clients reading mutex = 10 Guards updates to read_count 27

Readers + Writers with Semaphores Might lead to starvation As long as there is at least one thread reading, new readers will take priority over a waiting writer OS typically provides read/write locks to solve this 28

Dining Philosophers Philosophers spend their lives alternating thinking and eating Don t interact with their neighbors, occasionally try to pick up 2 chopsticks (one at a time) to eat from bowl Need both to eat, then release both when done In the case of 5 philosophers Shared data Bowl of rice (data set) Semaphore chopstick [5] initialized to 1 29

Dining Philosophers Solution Each philosopher does this: do { wait (chopstick[i] ); wait (chopstick[ (i + 1) % 5] ); // eat signal (chopstick[i] ); signal (chopstick[ (i + 1) % 5] ); // think } while (TRUE); What is the problem with this solution? 30

Dining Philosophers Solution Resolving the deadlock in prior algorithm: Allow at most 4 philosophers to be sitting simultaneously at the table. Allow a philosopher to pick up the forks only if both are available (picking must be done in a critical section. Use an asymmetric solution -- an odd-numbered philosopher picks up first the left chopstick and then the right chopstick. Even-numbered philosopher picks up first the right chopstick and then the left chopstick. 31

High-level synchronization mechanisms Semaphores are a very powerful mechanism for process synchronization, but they are a low-level mechanism Several high-level mechanisms that are easier to use have been proposed Monitors (Condition variables and locks) Read/Write Locks Java and Pthreads provide both semaphores and highlevel synchronization mechanisms NOTE: high-level mechanisms easier to use but equivalent to semaphores in power 32

Locking in Java Most locks are reentrant: if you hold it, and ask for it again, you don t have to wait (because you already have it) Basic primitives: synchronized{} wait notify Plus Lock API lock.lock(), lock.unlock() The preferred way 33

Synchronized methods in Java public synchronized static void increment() { i = i + 1; } Result: Before entering increment(), thread gets a lock on the Class object of increment()

wait and notify() Two mechanisms to enable coordination between multiple threads using the same monitor (target of synchronized) While holding a monitor on an object, a thread can wait on that monitor, which will temporarily release it, and put that thread to sleep Another thread can then acquire the monitor, and can notify a waiting thread to resume and reacquire the monitor 35

wait and notify() example public class BlockingQueue<T> { private Queue<T> queue = new LinkedList<T>(); private int capacity; public BlockingQueue(int capacity) { this.capacity = capacity; } Only one thread can be in put or take of the same queue public synchronized void put(t element) throws InterruptedException { while(queue.size() == capacity) { wait(); } } queue.add(element); notify(); // notifyall() for multiple producer/consumer threads public synchronized T take() throws InterruptedException { while(queue.isempty()) { wait(); } } } T item = queue.remove(); notify(); // notifyall() for multiple producer/consumer threads return item; 36

Synchronized methods in Java public synchronized static void increment() { i = i + 1; } Result: Before entering increment(), thread gets a lock on the Class object of increment() public synchronized static void incrementother() { j = j + 1; } Result: Before entering incrementother(), thread gets a lock on the Class object of incrementother() Problem?

Synchronized blocks in Java Can also use any object as that monitor static Object someobject = new Object(); public static void increment() { synchronized(someobject){ i = i + 1; } } static Object someotherobject = new Object(); public static void incrementother() { synchronized(someotherobject){ j = j + 1; } } Now, two different threads could call increment() and incrementother() at the same time 38

Assignment 1 Discussion http://www.jonbell.net/gmu-cs-475-spring-2018/ homework-1/ https://autolab.cs.gmu.edu/courses/cstest/ assessments 39

Roadmap Weds: Java Concurrency Joke (didn't make it out last week): Knock, knock. Race condition. Who s there? 40

Checkpoint Go to socrative.com and select Student Login (works well on laptop, tablet or phone) Room Name: CS475 ID is your @gmu.edu email Remember: this is a checkpoint for you, it is only graded for attendance 41