Concurrency Control. Synchronization. Brief Preview of Scheduling. Motivating Example. Motivating Example (Cont d) Interleaved Schedules

Similar documents
Dealing with Issues for Interprocess Communication

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Steve Gribble. Synchronization. Threads cooperate in multithreaded programs

Operating Systems. Sina Meraji U of T

Concurrent Processes Rab Nawaz Jadoon

CSE 153 Design of Operating Systems

Synchronization I. Jo, Heeseung

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Hank Levy 412 Sieg Hall

CS 31: Introduction to Computer Systems : Threads & Synchronization April 16-18, 2019

CS 153 Design of Operating Systems Winter 2016

Operating Systems. Operating Systems Summer 2017 Sina Meraji U of T

CSE 153 Design of Operating Systems Fall 2018

10/17/ Gribble, Lazowska, Levy, Zahorjan 2. 10/17/ Gribble, Lazowska, Levy, Zahorjan 4

Synchronization I. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Lecture 5: Synchronization w/locks

Operating Systems. Synchronization

Synchronization. Dr. Yingwu Zhu

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

Chapter 6: Process Synchronization

Chapter 6: Process Synchronization

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

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

Concurrency, Mutual Exclusion and Synchronization C H A P T E R 5

Synchronization Principles

Mutual Exclusion and Synchronization

CHAPTER 6: PROCESS SYNCHRONIZATION

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

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

Chapter 7: Process Synchronization!

Operating Systems ECE344

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

CSE 451: Operating Systems Winter Synchronization. Gary Kimura

What is the Race Condition? And what is its solution? What is a critical section? And what is the critical section problem?

What's wrong with Semaphores?

Process Synchronization

Lesson 6: Process Synchronization

Interprocess Communication By: Kaushik Vaghani

Operating Systems. Lecture 4 - Concurrency and Synchronization. Master of Computer Science PUF - Hồ Chí Minh 2016/2017

Process Management And Synchronization

IT 540 Operating Systems ECE519 Advanced Operating Systems

Lecture 8: September 30

Overview. CMSC 330: Organization of Programming Languages. Concurrency. Multiprocessors. Processes vs. Threads. Computation Abstractions

Process Synchronization

Deadlock and Monitors. CS439: Principles of Computer Systems February 7, 2018

CS420: Operating Systems. Process Synchronization

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

Opera&ng Systems ECE344

Lecture 6 (cont.): Semaphores and Monitors

CSE 153 Design of Operating Systems

Dr. D. M. Akbar Hussain DE5 Department of Electronic Systems

27/04/2012. We re going to build Multithreading Application. Objectives. MultiThreading. Multithreading Applications. What are Threads?

CS 571 Operating Systems. Midterm Review. Angelos Stavrou, George Mason University

Chapter 5 Concurrency: Mutual Exclusion and Synchronization

Process Coordination

CSE 153 Design of Operating Systems

The concept of concurrency is fundamental to all these areas.

CS 318 Principles of Operating Systems

IV. Process Synchronisation

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

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring Lecture 8: Semaphores, Monitors, & Condition Variables

Thread-Local. Lecture 27: Concurrency 3. Dealing with the Rest. Immutable. Whenever possible, don t share resources

SYNCHRONIZATION M O D E R N O P E R A T I N G S Y S T E M S R E A D 2. 3 E X C E P T A N D S P R I N G 2018

CS-537: Midterm Exam (Spring 2001)

Synchronization. CS61, Lecture 18. Prof. Stephen Chong November 3, 2011

CS3733: Operating Systems

COP 4225 Advanced Unix Programming. Synchronization. Chi Zhang

Part II Process Management Chapter 6: Process Synchronization

CSE 120 Principles of Operating Systems Spring 2016

CMSC 330: Organization of Programming Languages

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

Lecture 7: CVs & Scheduling

CS370 Operating Systems

Need for synchronization: If threads comprise parts of our software systems, then they must communicate.

Chapter 6: Process Synchronization. Module 6: Process Synchronization

Last Class: Synchronization. Review. Semaphores. Today: Semaphores. MLFQ CPU scheduler. What is test & set?

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

Principles of Operating Systems CS 446/646

CSC 1600: Chapter 6. Synchronizing Threads. Semaphores " Review: Multi-Threaded Processes"

W4118 Operating Systems. Instructor: Junfeng Yang

Process Synchronization

Chapter 5 Concurrency: Mutual Exclusion. and. Synchronization. Operating Systems: Internals. and. Design Principles

CMSC 132: Object-Oriented Programming II

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

Concurrency: State Models & Design Patterns

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

Lecture 9: Midterm Review

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Chapter 7: Process Synchronization. Background. Illustration

Dept. of CSE, York Univ. 1

Introduction to OS Synchronization MOS 2.3

Process Synchronization

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Parallelism and Concurrency. Motivation, Challenges, Impact on Software Development CSE 110 Winter 2016

PESIT Bangalore South Campus

CS604 - Operating System Solved Subjective Midterm Papers For Midterm Exam Preparation

Preview. The Thread Model Motivation of Threads Benefits of Threads Implementation of Thread

Chapter 7: Process Synchronization. Background

Operating Systems. Operating Systems Sina Meraji U of T

CS533 Concepts of Operating Systems. Jonathan Walpole

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

Process Synchronization

Transcription:

Brief Preview of Scheduling Concurrency Control Nan Niu (nn@cs.toronto.edu) CSC309 -- Summer 2008 Multiple threads ready to run Some mechanism for switching between them Context switches Some policy for choosing the next thread to run This policy may be pre-emptive Meaning thread can t anticipate when it may be forced to yield the CPU By design, it is not easy to detect it has happened (only the timing changes) 2 Synchronization Threads interact in a multiprogrammed system To share resources (such as shared data) To coordinate their execution Arbitrary interleaving of thread executions can have unexpected consequences We need a way to restrict the possible interleavings of executions Scheduling is invisible to the application Synchronization is the mechanism that gives us this control 3 Motivating Example Suppose we write functions to handle withdrawals and deposits to bank account: Deposit(acct, amt) { Now suppose you share this account with someone and the balance is $1000 You each go to separate ATM machines - you withdraw $100 and your S.O. deposits $100 4 Motivating Example (Cont d) We can represent this situation by creating separate threads for each action, which may run at the bank s central server (or at the ATM): Deposit(account, amount) { What s wrong with this implementation? Think about potential schedules for these two threads 5 Interleaved Schedules The problem is that the execution of the two processes can be interleaved: Schedule A What is the account balance now? Is the bank happy with our implementation? Are you? Context switch Schedule B 6 1

What Went Wrong? Two concurrent threads manipulated a shared resource (the account) without any synchronization Outcome depends on the order in which accesses take place This is called a race condition We need to ensure that only one thread at a time can manipulate the shared resource So that we can reason about program behavior We need synchronization 7 What Program Data is Shared? Local variables are not shared (private) Each thread has its own stack Local vars are allocated on this private stack Never pass/share/store a pointer to a local variable on another thread s stack! Global variables and static objects are shared Stored in the static data segment, accessible by any thread Dynamic objects and other heap objs are shared Allocated from heap with new/delete Any properties/attributes of a class 8 Given: Mutual Exclusion A set of n threads, T 1, T 2,, T n A set of resources shared between threads A segment of code which accesses the shared resources, called the critical section, CS We want to ensure that: Only one thread at a time can execute in the CS All other threads are forced to on entry When a thread leaves the CS, another can enter 9 The Critical Section Problem Design a protocol that threads can use to cooperate Each thread must request permission to enter its CS, in its entry section CS may be followed by an exit section Remaining code is the remainder section remainder Entry CS Exit remainder Each thread is executing at non-zero speed no assumptions about relative speed 10 Critical Section Requirements Mutual Exclusion If one thread is in the CS, then no other is Progress If no thread is in the CS, and some threads want to enter CS, only threads not in the remainder section can influence the choice of which thread enters next, and choice cannot be postponed indefinitely Bounded ing (no starvation) If some thread T is ing on the CS, then there is a limit on the number of times other threads can enter CS before this thread is granted access Performance The overhead of entering and exiting the CS is small High-level Abstractions for CS s Locks Very primitive, minimal semantics Semaphores Basic, easy to understand, hard to program with Monitors High-level, ideally has language support (Java) Messages Simple model for communication & synchronization Direct application to distributed systems with respect to the work being done within it 11 12 2

Function Definitions Deposit(account, amount) { Using Locks Possible schedule 13 Monitors an abstract data type (data and operations on the data) with the restriction that only one process at a time can be active within the monitor Local data accessed only by the monitor s procedures (not by any external procedure) A process enters the monitor by invoking 1 of its procs Other processes that attempt to enter monitor are blocked 14 More on Monitors If no process is suspended, a has no effect If process P executes an x. operation and $ a suspended process Q associated with condition x, then we have a problem: P is already in the monitor, does not need to block Q becomes unblocked by the, and wants to resume execution in the monitor but both cannot be simultaneously active in the monitor! 15 Java Synchronization Use synchronized Java keyword to restrict execution to 1 thread Method level synchronized public void dosomething() { // Only one thread can execute // method at any time Object public void dosomething() { synchronized(someobject) { // Limit access to only // one thread per object Class public void dosomething() { synchronized(someobject.class) { // Limit access to only // one thread per class 16 Synchronization Example Problem: ensure consistency of a shared variable in a multithreaded program Idea: use the singleton pattern to ensure a single instance Problem: possible simultaneous initializations Idea: use synchronization Synchronization Example (Cont d) public class Singleton { private volatile static Singleton myuniqueinstance; private Singleton() { public static Singleton getinstance() { synchronized (Singleton.class) { myuniqueinstance = new Singleton(); return myuniqueinstance; 17 18 3

Synchronization Efficiency Problem: synchronization is expensive Solution: check condition first unsynchronized; then, synchronized Synchronization Efficiency Example public class Singleton { private volatile static Singleton myuniqueinstance; private Singleton() { 19 public static Singleton getinstance() { synchronized (Singleton.class) { myuniqueinstance = new Singleton(); return myuniqueinstance; 20 Producer-Consumer Problem A group of processes/threads produce data (producers) A different group of processes/threads consume data (consumers) Data to be consumed is held in a shared buffer Infinite size for now Producer-Consumer Consumer Producer while(buffer.size < 1) add element to buffer remove first buffer element 21 22 Consumer Producer-Consumer ver2 Buffer size = 5 while(buffer.size < 1) remove first buffer element if (buffer == 4) Producer while (buffer.size > 4) add element to buffer if (buffer == 1) Enforcing Single Class A process in the monitor may need to for something to happen May need to allow another process to use the monitor Provide a condition type for variables with operations (suspend the invoking process) (resume exactly one suspended process) 23 24 4

More Semantics for Signal Hoare monitors (original) () immediately switches from the caller to a ing thread The condition that the er was blocked on is guaranteed to hold when the er resumes Need another queue for the er, if er was not done using the monitor Mesa monitors (Mesa, Java, Nachos, OS/161) Signal() places a er on the ready queue, but er continues inside monitor Condition is not necessarily true when er resumes Must check condition again 25 Hoare Hoare vs. Mesa Semantics if (empty) (condition); Mesa while(empty) (condition) Tradeoffs Hoare monitors make it easier to reason about program Mesa monitors are easier to implement, more efficient, can support additional ops like broadcast 26 Synchronization for Servlets Problem: multiple service method threads Solution: synchronization Do NOT synchronize doget Excessive blocking Does not mutex other servlets Synchronize on: Servlet ( instance ) variables Session -> session scope synchronized(request.getsession()) Context -> application scope synchronized(getservletcontext()) Best idea: do not share resources unnecessarily 27 5