Monitors; Software Transactional Memory

Size: px
Start display at page:

Download "Monitors; Software Transactional Memory"

Transcription

1 Monitors; Software Transactional Memory Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto Superior Técnico October 18, 2012 CPD (DEI / IST) Parallel and Distributed Computing / 35

2 Outline Monitors vs Semaphores Software Transactional Memory CPD (DEI / IST) Parallel and Distributed Computing / 35

3 SMP Programming Errors Shared Memory parallel programming: saves the programmer from having to map data onto multiple processors. opens up a range of new errors coming from unanticipated shared resource conflicts Race Conditions The output of a program depends on the timing of the threads in the team. Deadlock Threads lock up on a locked resource that will never become free. Livelock Threads working on individual tasks which the ensemble cannot finish. CPD (DEI / IST) Parallel and Distributed Computing / 35

4 Critical Regions Critical Region Sections of the code that access a shared resource which must not be accessed concurrently by another thread. non-critical entry region critical-region leave region non-critical Unfortunate notation: the critical region is really in data but the guards are in code CPD (DEI / IST) Parallel and Distributed Computing / 35

5 Monitors Semaphores are low-level synchronization primitives, inherently unstructured usage of the semaphore must be correct for all regions of the program CPD (DEI / IST) Parallel and Distributed Computing / 35

6 Monitors Semaphores are low-level synchronization primitives, inherently unstructured usage of the semaphore must be correct for all regions of the program Monitors provide a structured concurrent programming primitive that concentrates the responsibility of correctness to a few modules. Monitors encapsulate: items of data procedures that operate on this set of data In object-oriented programming, monitors synchronize calls to the methods of a class. CPD (DEI / IST) Parallel and Distributed Computing / 35

7 Monitor Concept Already in the 1970s concurrent programmers were faced with the following problems: Concurrent programs were unreliable and hard to write Semaphores were already understood but semaphores were often used incorrectly and a compiler could provide no help on using them Brinch-Hansen and Hoare proposed monitors, which: provide the facility for synchronizing concurrent programs that would be easy to use and could also be checked by a compiler when threads (process) have to access the same variables, they must be granted mutually exclusive access one thread must be able to wait for a condition that another thread can cause without creating a deadlock situation CPD (DEI / IST) Parallel and Distributed Computing / 35

8 Monitors Monitor A monitor is essentially a shared class with explicit queues. A monitor region is code that needs to be executed as one indivisible operation with respect to a particular monitor. A monitor enforces this one-thread-at-a-time execution of its monitor regions. The only way a thread can enter a monitor is by arriving at the beginning of one of the monitor regions associated with that monitor. The only way a thread can move forward and execute the monitor region is by acquiring the monitor. CPD (DEI / IST) Parallel and Distributed Computing / 35

9 Monitors When a thread arrives at the beginning of a monitor region, it is placed into an entry set for the associated monitor. CPD (DEI / IST) Parallel and Distributed Computing / 35

10 Monitors When a thread arrives at the beginning of a monitor region, it is placed into an entry set for the associated monitor. When the thread finishes executing the monitor region, it exits and releases the monitor. CPD (DEI / IST) Parallel and Distributed Computing / 35

11 Monitors When a thread arrives at the beginning of a monitor region, it is placed into an entry set for the associated monitor. When the thread finishes executing the monitor region, it exits and releases the monitor. A thread can suspend itself inside the monitor by executing a wait command. When a thread executes a wait, it releases the monitor and enters a wait set. CPD (DEI / IST) Parallel and Distributed Computing / 35

12 Monitors When a thread arrives at the beginning of a monitor region, it is placed into an entry set for the associated monitor. When the thread finishes executing the monitor region, it exits and releases the monitor. A thread can suspend itself inside the monitor by executing a wait command. When a thread executes a wait, it releases the monitor and enters a wait set. The thread will stay suspended in the wait set until some time after another thread executes a notify command inside the monitor. CPD (DEI / IST) Parallel and Distributed Computing / 35

13 Monitors When a thread arrives at the beginning of a monitor region, it is placed into an entry set for the associated monitor. When the thread finishes executing the monitor region, it exits and releases the monitor. A thread can suspend itself inside the monitor by executing a wait command. When a thread executes a wait, it releases the monitor and enters a wait set. The thread will stay suspended in the wait set until some time after another thread executes a notify command inside the monitor. When a thread executes a notify, it continues to own the monitor until it releases the monitor of its own accord, either by executing a wait or by completing the monitor region. CPD (DEI / IST) Parallel and Distributed Computing / 35

14 Simple Monitor Operation Entry Set The Owner Wait Set enter acquire release acquire release and exit CPD (DEI / IST) Parallel and Distributed Computing / 35

15 Simple Monitor Operation Entry Set The Owner Wait Set enter acquire release acquire release and exit CPD (DEI / IST) Parallel and Distributed Computing / 35

16 Simple Monitor Operation Entry Set The Owner Wait Set enter acquire release acquire release and exit CPD (DEI / IST) Parallel and Distributed Computing / 35

17 Simple Monitor Operation Entry Set The Owner Wait Set enter acquire release acquire release and exit CPD (DEI / IST) Parallel and Distributed Computing / 35

18 Simple Monitor Operation Entry Set The Owner Wait Set enter acquire release acquire release and exit CPD (DEI / IST) Parallel and Distributed Computing / 35

19 Simple Monitor Operation Entry Set The Owner Wait Set enter acquire release acquire release and exit CPD (DEI / IST) Parallel and Distributed Computing / 35

20 Simple Monitor Operation Entry Set The Owner Wait Set enter acquire release acquire release and exit CPD (DEI / IST) Parallel and Distributed Computing / 35

21 Three Types of Monitors Brinch-Hansen and Hoare Signal and continue Java monitor graphics by Theodore Norvell CPD (DEI / IST) Parallel and Distributed Computing / 35

22 Producer-Consumer with Semaphores void producer() { int item; while (TRUE) { item = produce_item(); /* claim empty spot */ sem_wait(&not_full); sem_wait(&mutex); /* insert, protected by mutex */ insert_item(item); sem_post(&mutex); /* signal filled-in spot */ sem_post(&not_empty); } } void consumer() { int item; while (TRUE) { /* claim spot in buffer */ sem_wait(&not_empty); sem_wait(&mutex); /* remove, protected by mutex */ item = remove_item(); sem_post(&mutex); /* signal empty spot */ sem_post(&not_full); consume_item(item); } } CPD (DEI / IST) Parallel and Distributed Computing / 35

23 Producer-Consumer with Java Monitors class Buffer { private Object[] buf; private int in = 0; //index for put private int out = 0; //index for get private int count = 0; //no of items private int size; } Buffer(int size) { this.size = size; buf = new Object[size]; } synchronized public void put(object o) {...} synchronized public Object get() {...} CPD (DEI / IST) Parallel and Distributed Computing / 35

24 Producer-Consumer with Java Monitors synchronized public void put(object o) { while (count >= size) { try { wait(); } catch(interruptedexception e){} } buf[in] = o; ++count; in = (in + 1) % size; notifyall(); } synchronized public Object get() { while (count == 0) { try { wait(); } catch (InterruptedException e){} } Object o = buf[out]; buf[out] = null; // display purposes --count; out = (out + 1) % size; notifyall(); // [count < size] return (o); } CPD (DEI / IST) Parallel and Distributed Computing / 35

25 Producer-Consumer with Java Monitors Solution is more structured than semaphores: data and procedures are encapsulated in a single module mutual exclusion is provided automatically by the implementation producer and consumer processes see only abstract put and get CPD (DEI / IST) Parallel and Distributed Computing / 35

26 Limitations of Previous Approach only one queue for threads waiting at an object must always wake up all of the waiting threads CPD (DEI / IST) Parallel and Distributed Computing / 35

27 Limitations of Previous Approach only one queue for threads waiting at an object must always wake up all of the waiting threads the thread awakened by notify() or notifyall() does not get immediate control of the lock. By the time the thread runs, the condition may no longer be true, so it must check the condition again. CPD (DEI / IST) Parallel and Distributed Computing / 35

28 Transactions Transaction Operations in a transaction either all occur or none occur. Atomic operation: Commit: takes effect Abort: effects rolled back Usually retried Linearizable Appear to happen in one-at-a-time order CPD (DEI / IST) Parallel and Distributed Computing / 35

29 Transactions Transaction Operations in a transaction either all occur or none occur. Atomic operation: Commit: takes effect Abort: effects rolled back Usually retried Linearizable Appear to happen in one-at-a-time order Transactional Memory A section of code with reads and writes to shared memory which logically occur at a single instant in time. CPD (DEI / IST) Parallel and Distributed Computing / 35

30 Software Transactional Memory Software Transactional Memory (STM) has been proposed as an alternative to Lock-based Synchronization. Concurrency Unlocked no thread control when entering critical regions if there are no memory access conflicts during thread execution, operations executed by thread are accepted in case of conflict, program state is rolled-back to the state it was before the thread entered the critical region CPD (DEI / IST) Parallel and Distributed Computing / 35

31 Software Transactional Memory Benefits of STM Optimistic: increased concurrency Composable: define atomic set of operations Conditional Critical Regions CPD (DEI / IST) Parallel and Distributed Computing / 35

32 Optimistic Increased concurrency: threads are not blocked. Conflicts only arise when more than one thread makes an access to the same memory position. Conflicts are rare small number of roll-backs. CPD (DEI / IST) Parallel and Distributed Computing / 35

33 Composable Atomic Operations Keyword atomic allows the definition of the set of operations that make up the transaction. atomic { delete(t1, item); add(t2, item); } atomic { newnode->prev = node; newnode->next = node->next; node->next->prev = newnode; node->next = newnode; } either all happen or none at all remaining threads never see intermediate values CPD (DEI / IST) Parallel and Distributed Computing / 35

34 Conditional Critical Regions The default action when a transaction fails is to retry. What if the successful completion of a transaction is dependent on some variable? Example: consumer accessing an empty queue. CPD (DEI / IST) Parallel and Distributed Computing / 35

35 Conditional Critical Regions The default action when a transaction fails is to retry. What if the successful completion of a transaction is dependent on some variable? Example: consumer accessing an empty queue. thread enters a cycle of failures (i.e., active wait!) CPD (DEI / IST) Parallel and Distributed Computing / 35

36 Conditional Critical Regions If the successful completion of a transaction is dependent on some variable use a guard condition. atomic (queuesize > 0) { remove item from queue } If condition is not satisfied, thread will be blocked until a commit has been made that affects the condition. CPD (DEI / IST) Parallel and Distributed Computing / 35

37 Conditional Critical Regions Keyword retry allows the thread to abort and block at any point in the transaction: atomic { if(queuesize > 0) remove item from queue else retry; } CPD (DEI / IST) Parallel and Distributed Computing / 35

38 Conditional Critical Regions STM includes the possibility of alternative course of actions when a transaction fails: orelse keyword atomic { delete(t1, item) orelse delete(t2, item); add(t3, item); } if delete in T1 fails, a delete in T2 is attempted. if delete in T2 fails, the whole transaction retries. add is only performed after a successful delete of either T1 or T2. CPD (DEI / IST) Parallel and Distributed Computing / 35

39 Software Transactional Memory Benefits of STM Optimistic: increased concurrency Composable: define atomic set of operations Conditional Critical Regions CPD (DEI / IST) Parallel and Distributed Computing / 35

40 Software Transactional Memory Benefits of STM Optimistic: increased concurrency Composable: define atomic set of operations Conditional Critical Regions Problems with STM CPD (DEI / IST) Parallel and Distributed Computing / 35

41 Software Transactional Memory Benefits of STM Optimistic: increased concurrency Composable: define atomic set of operations Conditional Critical Regions Problems with STM overhead for conflict detection, both computational and memory CPD (DEI / IST) Parallel and Distributed Computing / 35

42 Software Transactional Memory Benefits of STM Optimistic: increased concurrency Composable: define atomic set of operations Conditional Critical Regions Problems with STM overhead for conflict detection, both computational and memory overhead from commit CPD (DEI / IST) Parallel and Distributed Computing / 35

43 Software Transactional Memory Benefits of STM Optimistic: increased concurrency Composable: define atomic set of operations Conditional Critical Regions Problems with STM overhead for conflict detection, both computational and memory overhead from commit cannot be used when operations cannot be undone (i.e., I/O) CPD (DEI / IST) Parallel and Distributed Computing / 35

44 STM Overhead Advantage of STM: much fewer conflicts. most transactions will commit. However, every commit has a potentially large overhead... Note that if there are no conflicts, the only overhead of the mutex approach is in locking and unlocking. CPD (DEI / IST) Parallel and Distributed Computing / 35

45 Implementation Issues Transaction Log each read and write in transaction is logged to a thread-local transaction log CPD (DEI / IST) Parallel and Distributed Computing / 35

46 Implementation Issues Transaction Log each read and write in transaction is logged to a thread-local transaction log writes go to the log only, not to memory CPD (DEI / IST) Parallel and Distributed Computing / 35

47 Implementation Issues Transaction Log each read and write in transaction is logged to a thread-local transaction log writes go to the log only, not to memory at the end, the transaction tries to commit to memory CPD (DEI / IST) Parallel and Distributed Computing / 35

48 Implementation Issues Transaction Log each read and write in transaction is logged to a thread-local transaction log writes go to the log only, not to memory at the end, the transaction tries to commit to memory in case commit fails, discard log and retry transaction CPD (DEI / IST) Parallel and Distributed Computing / 35

49 Implementation Issues Commit-time Locking uses a global clock CPD (DEI / IST) Parallel and Distributed Computing / 35

50 Implementation Issues Commit-time Locking uses a global clock each memory location maintains an access time, T a CPD (DEI / IST) Parallel and Distributed Computing / 35

51 Implementation Issues Commit-time Locking uses a global clock each memory location maintains an access time, T a marks time at beginning of transaction, T t CPD (DEI / IST) Parallel and Distributed Computing / 35

52 Implementation Issues Commit-time Locking uses a global clock each memory location maintains an access time, T a marks time at beginning of transaction, T t for every read/write, if T a > T t, abort transaction CPD (DEI / IST) Parallel and Distributed Computing / 35

53 Implementation Issues Commit-time Locking uses a global clock each memory location maintains an access time, T a marks time at beginning of transaction, T t for every read/write, if T a > T t, abort transaction during commit, all write locations are locked and access times updated CPD (DEI / IST) Parallel and Distributed Computing / 35

54 Implementation Issues Commit-time locking: roll-back simple; commit expensive... CPD (DEI / IST) Parallel and Distributed Computing / 35

55 Implementation Issues Commit-time locking: roll-back simple; commit expensive... Encounter-time Locking memory positions inside a transaction are locked CPD (DEI / IST) Parallel and Distributed Computing / 35

56 Implementation Issues Commit-time locking: roll-back simple; commit expensive... Encounter-time Locking memory positions inside a transaction are locked thread has exclusive access to them during execution of transaction CPD (DEI / IST) Parallel and Distributed Computing / 35

57 Implementation Issues Commit-time locking: roll-back simple; commit expensive... Encounter-time Locking memory positions inside a transaction are locked thread has exclusive access to them during execution of transaction remaining threads abort immediately when accessing one of these positions CPD (DEI / IST) Parallel and Distributed Computing / 35

58 Hardware Support Hardware support for transactional memories has been proposed long ago. Performance of STM would improve considerably! Sun s Rock Processor First multicore (16 cores) designed for hardware transaction memory. Special Assembly instructions: chkpt <fail pc>: begin a transaction commit: commit the transaction CPD (DEI / IST) Parallel and Distributed Computing / 35

59 Distributed STM The concept of Software Transactional Memory can be extended to distributed systems. IST s Fenix is based on a distributed STM concept. CPD (DEI / IST) Parallel and Distributed Computing / 35

60 STM in Fenix Fenix is a large web application, with a rich domain model Before STM (2005), Fenix had major problems: frequent bugs poor performance Root of the problems: Locks used for concurrency control Idea: Wrap each HTTP request with a transaction. CPD (DEI / IST) Parallel and Distributed Computing / 35

61 STM in Fenix The JVSTM went into production in September Major benefits: The data-corruption errors disappeared they were caused mostly by misplaced locks There was a perceived increase in the performance after an initial warm-up New functionalities are developed significantly faster it requires less coding and less debugging CPD (DEI / IST) Parallel and Distributed Computing / 35

62 Review Monitors vs Semaphores Software Transactional Memory CPD (DEI / IST) Parallel and Distributed Computing / 35

63 Next Class Foster s design methodology partitioning communication agglomeration mapping Application Examples Boundary value problem Finding the maximum n-body problem CPD (DEI / IST) Parallel and Distributed Computing / 35

Monitors; Software Transactional Memory

Monitors; Software Transactional Memory Monitors; Software Transactional Memory Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto Superior Técnico March 17, 2016 CPD (DEI / IST) Parallel and Distributed

More information

Java Monitors. Parallel and Distributed Computing. Department of Computer Science and Engineering (DEI) Instituto Superior Técnico.

Java Monitors. Parallel and Distributed Computing. Department of Computer Science and Engineering (DEI) Instituto Superior Técnico. Java Monitors Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto Superior Técnico October 19, 2010 Monteiro, Costa (DEI / IST) Parallel and Distributed Computing

More information

Synchronization. CSCI 5103 Operating Systems. Semaphore Usage. Bounded-Buffer Problem With Semaphores. Monitors Other Approaches

Synchronization. CSCI 5103 Operating Systems. Semaphore Usage. Bounded-Buffer Problem With Semaphores. Monitors Other Approaches Synchronization CSCI 5103 Operating Systems Monitors Other Approaches Instructor: Abhishek Chandra 2 3 Semaphore Usage wait(s) Critical section signal(s) Each process must call wait and signal in correct

More information

Concurrency: a crash course

Concurrency: a crash course Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Concurrency: a crash course Concurrent computing Applications designed as a collection of computational units that may execute

More information

Lecture 10: Introduction to Semaphores

Lecture 10: Introduction to Semaphores COMP 150-CCP Concurrent Programming Lecture 10: Introduction to Semaphores Dr. Richard S. Hall rickhall@cs.tufts.edu Concurrent programming February 19, 2008 Semaphores Semaphores (Dijkstra 1968) are widely

More information

What's wrong with Semaphores?

What's wrong with Semaphores? Next: Monitors and Condition Variables What is wrong with semaphores? Monitors What are they? How do we implement monitors? Two types of monitors: Mesa and Hoare Compare semaphore and monitors Lecture

More information

Real-Time and Concurrent Programming Lecture 4 (F4): Monitors: synchronized, wait and notify

Real-Time and Concurrent Programming Lecture 4 (F4): Monitors: synchronized, wait and notify http://cs.lth.se/eda040 Real-Time and Concurrent Programming Lecture 4 (F4): Monitors: synchronized, wait and notify Klas Nilsson 2016-09-20 http://cs.lth.se/eda040 F4: Monitors: synchronized, wait and

More information

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

What is the Race Condition? And what is its solution? What is a critical section? And what is the critical section problem? What is the Race Condition? And what is its solution? Race Condition: Where several processes access and manipulate the same data concurrently and the outcome of the execution depends on the particular

More information

Resource management. Real-Time Systems. Resource management. Resource management

Resource management. Real-Time Systems. Resource management. Resource management Real-Time Systems Specification Implementation Verification Mutual exclusion is a general problem that exists at several levels in a real-time system. Shared resources internal to the the run-time system:

More information

Programming Language Concepts: Lecture 13

Programming Language Concepts: Lecture 13 Programming Language Concepts: Lecture 13 Madhavan Mukund Chennai Mathematical Institute madhavan@cmi.ac.in http://www.cmi.ac.in/~madhavan/courses/pl2009 PLC 2009, Lecture 13, 09 March 2009 An exercise

More information

Lecture 8: September 30

Lecture 8: September 30 CMPSCI 377 Operating Systems Fall 2013 Lecture 8: September 30 Lecturer: Prashant Shenoy Scribe: Armand Halbert 8.1 Semaphores A semaphore is a more generalized form of a lock that can be used to regulate

More information

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

Need for synchronization: If threads comprise parts of our software systems, then they must communicate. Thread communication and synchronization There are two main aspects to Outline for Lecture 19 multithreaded programming in Java: I. Thread synchronization. thread lifecycle, and thread synchronization.

More information

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

Deadlock and Monitors. CS439: Principles of Computer Systems September 24, 2018 Deadlock and Monitors CS439: Principles of Computer Systems September 24, 2018 Bringing It All Together Processes Abstraction for protection Define address space Threads Share (and communicate) through

More information

Summary Semaphores. Passing the Baton any await statement. Synchronisation code not linked to the data

Summary Semaphores. Passing the Baton any await statement. Synchronisation code not linked to the data Lecture 4 Monitors Summary Semaphores Good news Simple, efficient, expressive Passing the Baton any await statement Bad news Low level, unstructured omit a V: deadlock omit a P: failure of mutex Synchronisation

More information

Threads and Parallelism in Java

Threads and Parallelism in Java Threads and Parallelism in Java Java is one of the few main stream programming languages to explicitly provide for user-programmed parallelism in the form of threads. A Java programmer may organize a program

More information

Dealing with Issues for Interprocess Communication

Dealing with Issues for Interprocess Communication Dealing with Issues for Interprocess Communication Ref Section 2.3 Tanenbaum 7.1 Overview Processes frequently need to communicate with other processes. In a shell pipe the o/p of one process is passed

More information

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst Operating Systems CMPSCI 377 Spring 2017 Mark Corner University of Massachusetts Amherst What is a Monitor? Ties data and the synchronization operations together Monitors guarantee mutual exclusion, i.e.,

More information

CSCI 5828: Foundations of Software Engineering

CSCI 5828: Foundations of Software Engineering CSCI 5828: Foundations of Software Engineering Lecture 16: Monitors & Condition Synchronization Slides created by Magee and Kramer for the Concurrency textbook 1 Chapter 5 Monitors & Condition Synchronization

More information

Condition Variables CS 241. Prof. Brighten Godfrey. March 16, University of Illinois

Condition Variables CS 241. Prof. Brighten Godfrey. March 16, University of Illinois Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois 1 Synchronization primitives Mutex locks Used for exclusive access to a shared resource (critical section) Operations:

More information

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

Last Class: Synchronization. Review. Semaphores. Today: Semaphores. MLFQ CPU scheduler. What is test & set? Last Class: Synchronization Review Synchronization Mutual exclusion Critical sections Example: Too Much Milk Locks Synchronization primitives are required to ensure that only one thread executes in a critical

More information

CIS Operating Systems Application of Semaphores. Professor Qiang Zeng Spring 2018

CIS Operating Systems Application of Semaphores. Professor Qiang Zeng Spring 2018 CIS 3207 - Operating Systems Application of Semaphores Professor Qiang Zeng Spring 2018 Big picture of synchronization primitives Busy-waiting Software solutions (Dekker, Bakery, etc.) Hardware-assisted

More information

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

Thread-Local. Lecture 27: Concurrency 3. Dealing with the Rest. Immutable. Whenever possible, don t share resources Thread-Local Lecture 27: Concurrency 3 CS 62 Fall 2016 Kim Bruce & Peter Mawhorter Some slides based on those from Dan Grossman, U. of Washington Whenever possible, don t share resources Easier to have

More information

Real-Time Systems. Lecture #4. Professor Jan Jonsson. Department of Computer Science and Engineering Chalmers University of Technology

Real-Time Systems. Lecture #4. Professor Jan Jonsson. Department of Computer Science and Engineering Chalmers University of Technology Real-Time Systems Lecture #4 Professor Jan Jonsson Department of Computer Science and Engineering Chalmers University of Technology Real-Time Systems Specification Resource management Mutual exclusion

More information

Operating Systems. Lecture 8

Operating Systems. Lecture 8 CMSC 321: Operating Systems Lecture 8 Famous IPC Problems, Monitors Producer/Consumer Problem Two processes share common, infinite buffer Producer: puts info into buffer Consumer: takes info from buffer

More information

Reminder from last time

Reminder from last time Concurrent systems Lecture 3: CCR, monitors, and concurrency in practice DrRobert N. M. Watson 1 Reminder from last time Implementing mutual exclusion: hardware support for atomicity and inter-processor

More information

Synchroniza+on II COMS W4118

Synchroniza+on II COMS W4118 Synchroniza+on II COMS W4118 References: Opera+ng Systems Concepts (9e), Linux Kernel Development, previous W4118s Copyright no2ce: care has been taken to use only those web images deemed by the instructor

More information

Concurrency and Synchronisation

Concurrency and Synchronisation Concurrency and Synchronisation 1 Sections 2.3 & 2.4 Textbook 2 Making Single-Threaded Code Multithreaded Conflicts between threads over the use of a global variable 3 Inter- Thread and Process Communication

More information

Monitors & Condition Synchronization

Monitors & Condition Synchronization Chapter 5 Monitors & Condition Synchronization monitors & condition synchronization Concepts: monitors: encapsulated data + access procedures mutual exclusion + condition synchronization single access

More information

Concurrency and Synchronisation

Concurrency and Synchronisation Concurrency and Synchronisation 1 Learning Outcomes Understand concurrency is an issue in operating systems and multithreaded applications Know the concept of a critical region. Understand how mutual exclusion

More information

CS Operating Systems

CS Operating Systems CS 4500 - Operating Systems Module 4: The Producer-Consumer Problem and Solution Methods Stanley Wileman Department of Computer Science University of Nebraska at Omaha Omaha, NE 68182-0500, USA June 3,

More information

CS Operating Systems

CS Operating Systems CS 4500 - Operating Systems Module 4: The Producer-Consumer Problem and Solution Methods Stanley Wileman Department of Computer Science University of Nebraska at Omaha Omaha, NE 68182-0500, USA June 3,

More information

MultiJav: A Distributed Shared Memory System Based on Multiple Java Virtual Machines. MultiJav: Introduction

MultiJav: A Distributed Shared Memory System Based on Multiple Java Virtual Machines. MultiJav: Introduction : A Distributed Shared Memory System Based on Multiple Java Virtual Machines X. Chen and V.H. Allan Computer Science Department, Utah State University 1998 : Introduction Built on concurrency supported

More information

Advances in Programming Languages

Advances in Programming Languages O T Y H Advances in Programming Languages APL5: Further language concurrency mechanisms David Aspinall (including slides by Ian Stark) School of Informatics The University of Edinburgh Tuesday 5th October

More information

Synchronization. Announcements. Concurrent Programs. Race Conditions. Race Conditions 11/9/17. Purpose of this lecture. A8 released today, Due: 11/21

Synchronization. Announcements. Concurrent Programs. Race Conditions. Race Conditions 11/9/17. Purpose of this lecture. A8 released today, Due: 11/21 Announcements Synchronization A8 released today, Due: 11/21 Late deadline is after Thanksgiving You can use your A6/A7 solutions or ours A7 correctness scores have been posted Next week's recitation will

More information

Synchronization in Java

Synchronization in Java Synchronization in Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park Synchronization Overview Unsufficient atomicity Data races Locks Deadlock Wait /

More information

Foster s Methodology: Application Examples

Foster s Methodology: Application Examples Foster s Methodology: Application Examples Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto Superior Técnico October 19, 2011 CPD (DEI / IST) Parallel and

More information

Part IV Other Systems: I Java Threads

Part IV Other Systems: I Java Threads Part IV Other Systems: I Java Threads Spring 2019 C is quirky, flawed, and an enormous success. 1 Dennis M. Ritchie Java Threads: 1/6 Java has two ways to create threads: Create a new class derived from

More information

Performance Throughput Utilization of system resources

Performance Throughput Utilization of system resources Concurrency 1. Why concurrent programming?... 2 2. Evolution... 2 3. Definitions... 3 4. Concurrent languages... 5 5. Problems with concurrency... 6 6. Process Interactions... 7 7. Low-level Concurrency

More information

COMP346 Winter Tutorial 4 Synchronization Semaphores

COMP346 Winter Tutorial 4 Synchronization Semaphores COMP346 Winter 2015 Tutorial 4 Synchronization Semaphores 1 Topics Synchronization in Details Semaphores Introducing Semaphore.java 2 Synchronization What is it? An act of communication between unrelated

More information

Multiple Inheritance. Computer object can be viewed as

Multiple Inheritance. Computer object can be viewed as Multiple Inheritance We have seen that a class may be derived from a given parent class. It is sometimes useful to allow a class to be derived from more than one parent, inheriting members of all parents.

More information

CMSC 433 Programming Language Technologies and Paradigms. Composing Objects

CMSC 433 Programming Language Technologies and Paradigms. Composing Objects CMSC 433 Programming Language Technologies and Paradigms Composing Objects Composing Objects To build systems we often need to Create thread safe objects Compose them in ways that meet requirements while

More information

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

Learning Outcomes. Concurrency and Synchronisation. Textbook. Concurrency Example. Inter- Thread and Process Communication. Sections & 2. Learning Outcomes Concurrency and Synchronisation Understand concurrency is an issue in operating systems and multithreaded applications Know the concept of a critical region. Understand how mutual exclusion

More information

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

27/04/2012. We re going to build Multithreading Application. Objectives. MultiThreading. Multithreading Applications. What are Threads? Objectives MultiThreading What are Threads? Interrupting threads Thread properties By Võ Văn Hải Faculty of Information Technologies Summer 2012 Threads priorities Synchronization Callables and Futures

More information

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages CMSC 330: Organization of Programming Languages Multithreading Multiprocessors Description Multiple processing units (multiprocessor) From single microprocessor to large compute clusters Can perform multiple

More information

Monitors & Condition Synchronization

Monitors & Condition Synchronization Chapter 5 Monitors & Condition Synchronization controller 1 Monitors & Condition Synchronization Concepts: monitors (and controllers): encapsulated data + access procedures + mutual exclusion + condition

More information

G52CON: Concepts of Concurrency

G52CON: Concepts of Concurrency G52CON: Concepts of Concurrency Lecture 11: Semaphores I" Brian Logan School of Computer Science bsl@cs.nott.ac.uk Outline of this lecture" problems with Peterson s algorithm semaphores implementing semaphores

More information

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

Deadlock and Monitors. CS439: Principles of Computer Systems February 7, 2018 Deadlock and Monitors CS439: Principles of Computer Systems February 7, 2018 Last Time Terminology Safety and liveness Atomic Instructions, Synchronization, Mutual Exclusion, Critical Sections Synchronization

More information

Programming in Parallel COMP755

Programming in Parallel COMP755 Programming in Parallel COMP755 All games have morals; and the game of Snakes and Ladders captures, as no other activity can hope to do, the eternal truth that for every ladder you hope to climb, a snake

More information

CS 2112 Lecture 20 Synchronization 5 April 2012 Lecturer: Andrew Myers

CS 2112 Lecture 20 Synchronization 5 April 2012 Lecturer: Andrew Myers CS 2112 Lecture 20 Synchronization 5 April 2012 Lecturer: Andrew Myers 1 Critical sections and atomicity We have been seeing that sharing mutable objects between different threads is tricky We need some

More information

Synchronization Lecture 23 Fall 2017

Synchronization Lecture 23 Fall 2017 Synchronization Lecture 23 Fall 2017 Announcements A8 released today, Due: 11/21 Late deadline is after Thanksgiving You can use your A6/A7 solutions or ours A7 correctness scores have been posted Next

More information

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

Operating Systems. Operating Systems Summer 2017 Sina Meraji U of T Operating Systems Operating Systems Summer 2017 Sina Meraji U of T More Special Instructions Swap (or Exchange) instruction Operates on two words atomically Can also be used to solve critical section problem

More information

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

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 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 2. 3. 8 A N D 2. 3. 1 0 S P R I N G 2018 INTER-PROCESS COMMUNICATION 1. How a process pass information to another process

More information

Synchronization COMPSCI 386

Synchronization COMPSCI 386 Synchronization COMPSCI 386 Obvious? // push an item onto the stack while (top == SIZE) ; stack[top++] = item; // pop an item off the stack while (top == 0) ; item = stack[top--]; PRODUCER CONSUMER Suppose

More information

The University of Texas at Arlington

The University of Texas at Arlington The University of Texas at Arlington Lecture 6: Threading and Parallel Programming Constraints CSE 5343/4342 Embedded Systems II Based heavily on slides by Dr. Roger Walker More Task Decomposition: Dependence

More information

G Programming Languages Spring 2010 Lecture 13. Robert Grimm, New York University

G Programming Languages Spring 2010 Lecture 13. Robert Grimm, New York University G22.2110-001 Programming Languages Spring 2010 Lecture 13 Robert Grimm, New York University 1 Review Last week Exceptions 2 Outline Concurrency Discussion of Final Sources for today s lecture: PLP, 12

More information

CS 455: INTRODUCTION TO DISTRIBUTED SYSTEMS [THREADS] Frequently asked questions from the previous class survey

CS 455: INTRODUCTION TO DISTRIBUTED SYSTEMS [THREADS] Frequently asked questions from the previous class survey CS 455: INTRODUCTION TO DISTRIBUTED SYSTEMS [THREADS] Threads block when they can t get that lock Wanna have your threads stall? Go ahead, synchronize it all The antidote to this liveness pitfall? Keeping

More information

The University of Texas at Arlington

The University of Texas at Arlington The University of Texas at Arlington Lecture 10: Threading and Parallel Programming Constraints CSE 5343/4342 Embedded d Systems II Objectives: Lab 3: Windows Threads (win32 threading API) Convert serial

More information

Synchronization in Concurrent Programming. Amit Gupta

Synchronization in Concurrent Programming. Amit Gupta Synchronization in Concurrent Programming Amit Gupta Announcements Project 1 grades are out on blackboard. Detailed Grade sheets to be distributed after class. Project 2 grades should be out by next Thursday.

More information

Threads Tuesday, September 28, :37 AM

Threads Tuesday, September 28, :37 AM Threads_and_fabrics Page 1 Threads Tuesday, September 28, 2004 10:37 AM Threads A process includes an execution context containing Memory map PC and register values. Switching between memory maps can take

More information

PROCESS SYNCHRONIZATION

PROCESS SYNCHRONIZATION PROCESS SYNCHRONIZATION Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Semaphores Classic Problems of Synchronization Monitors Synchronization

More information

Monitors & Condition Synchronization

Monitors & Condition Synchronization Feb. 15, 2012 Monitors & condition Synchronization Concepts: monitors: encapsulated data + access procedures mutual exclusion + condition synchronization single access procedure active in the monitor Models:

More information

CMSC 433 Programming Language Technologies and Paradigms. Spring 2013

CMSC 433 Programming Language Technologies and Paradigms. Spring 2013 1 CMSC 433 Programming Language Technologies and Paradigms Spring 2013 Wait / Notify / NotifyAll Optimistic Retries Composition Follow-up (the risk I mentioned) ReentrantLock, Wait, Notify, NotifyAll Some

More information

Synchronization II. Today. ! Condition Variables! Semaphores! Monitors! and some classical problems Next time. ! Deadlocks

Synchronization II. Today. ! Condition Variables! Semaphores! Monitors! and some classical problems Next time. ! Deadlocks Synchronization II Today Condition Variables Semaphores Monitors and some classical problems Next time Deadlocks Condition variables Many times a thread wants to check whether a condition is true before

More information

Potential violations of Serializability: Example 1

Potential violations of Serializability: Example 1 CSCE 6610:Advanced Computer Architecture Review New Amdahl s law A possible idea for a term project Explore my idea about changing frequency based on serial fraction to maintain fixed energy or keep same

More information

C340 Concurrency: Semaphores and Monitors. Goals

C340 Concurrency: Semaphores and Monitors. Goals C340 Concurrency: Semaphores and Monitors Wolfgang Emmerich 1 Goals Introduce concepts of Semaphores Monitors Implementation in Java synchronised methods and private attributes single thread active in

More information

<Insert Picture Here> Revisting Condition Variables and Transactions

<Insert Picture Here> Revisting Condition Variables and Transactions Revisting Condition Variables and Transactions Victor Luchangco and Virendra Marathe Oracle Labs Copyright 2011 Oracle and/or its affiliates ( Oracle ). All rights are reserved by

More information

CS5460: Operating Systems

CS5460: Operating Systems CS5460: Operating Systems Lecture 9: Implementing Synchronization (Chapter 6) Multiprocessor Memory Models Uniprocessor memory is simple Every load from a location retrieves the last value stored to that

More information

Lecture 6: Lazy Transactional Memory. Topics: TM semantics and implementation details of lazy TM

Lecture 6: Lazy Transactional Memory. Topics: TM semantics and implementation details of lazy TM Lecture 6: Lazy Transactional Memory Topics: TM semantics and implementation details of lazy TM 1 Transactions Access to shared variables is encapsulated within transactions the system gives the illusion

More information

Race Conditions & Synchronization

Race Conditions & Synchronization Race Conditions & Synchronization Lecture 25 Spring 2017 Gries office hours 2 A number of students have asked for time to talk over how to study for the prelim. Gries will hold office hours at the times

More information

Java Threads. COMP 585 Noteset #2 1

Java Threads. COMP 585 Noteset #2 1 Java Threads The topic of threads overlaps the boundary between software development and operation systems. Words like process, task, and thread may mean different things depending on the author and the

More information

Synchronization. CS 416: Operating Systems Design, Spring 2011 Department of Computer Science Rutgers University

Synchronization. CS 416: Operating Systems Design, Spring 2011 Department of Computer Science Rutgers University Synchronization Design, Spring 2011 Department of Computer Science Synchronization Basic problem: Threads are concurrently accessing shared variables The access should be controlled for predictable result.

More information

W4118 Operating Systems. Instructor: Junfeng Yang

W4118 Operating Systems. Instructor: Junfeng Yang W4118 Operating Systems Instructor: Junfeng Yang Outline Semaphores Producer-consumer problem Monitors and condition variables 2 Semaphore motivation Problem with lock: mutual exclusion, but no ordering

More information

Monitors & Condition Synchronisation

Monitors & Condition Synchronisation Chapter 5 Monitors & Condition Synchronisation controller 1 Monitors & Condition Synchronisation Concepts: monitors (and controllers): encapsulated data + access procedures + mutual exclusion + condition

More information

Note: in this document we use process and thread interchangeably.

Note: in this document we use process and thread interchangeably. Summary on Monitor Implementation techniques Note: in this document we use process and thread interchangeably. Monitor is neither a process (thread) nor an active entity. It is just an abstract data type

More information

Threads and Synchronization. Kevin Webb Swarthmore College February 15, 2018

Threads and Synchronization. Kevin Webb Swarthmore College February 15, 2018 Threads and Synchronization Kevin Webb Swarthmore College February 15, 2018 Today s Goals Extend processes to allow for multiple execution contexts (threads) Benefits and challenges of concurrency Race

More information

Programming Language Concepts: Lecture 11

Programming Language Concepts: Lecture 11 Programming Language Concepts: Lecture 11 Madhavan Mukund Chennai Mathematical Institute madhavan@cmi.ac.in PLC 2011, Lecture 11, 01 March 2011 Concurrent Programming Monitors [Per Brinch Hansen, CAR Hoare]

More information

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

Operating Systems. Lecture 4 - Concurrency and Synchronization. Master of Computer Science PUF - Hồ Chí Minh 2016/2017 Operating Systems Lecture 4 - Concurrency and Synchronization Adrien Krähenbühl Master of Computer Science PUF - Hồ Chí Minh 2016/2017 Mutual exclusion Hardware solutions Semaphores IPC: Message passing

More information

Concurrency: Deadlock and Starvation

Concurrency: Deadlock and Starvation Concurrency: Deadlock and Starvation Chapter 6 E&CE 354: Processes 1 Deadlock Deadlock = situation in which every process from a set is permanently blocked, i.e. cannot proceed with execution Common cause:

More information

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

Overview. CMSC 330: Organization of Programming Languages. Concurrency. Multiprocessors. Processes vs. Threads. Computation Abstractions CMSC 330: Organization of Programming Languages Multithreaded Programming Patterns in Java CMSC 330 2 Multiprocessors Description Multiple processing units (multiprocessor) From single microprocessor to

More information

Concurrency: Deadlock and Starvation. Chapter 6

Concurrency: Deadlock and Starvation. Chapter 6 Concurrency: Deadlock and Starvation Chapter 6 Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate with each other Involve conflicting needs for resources

More information

CS11 Java. Fall Lecture 7

CS11 Java. Fall Lecture 7 CS11 Java Fall 2006-2007 Lecture 7 Today s Topics All about Java Threads Some Lab 7 tips Java Threading Recap A program can use multiple threads to do several things at once A thread can have local (non-shared)

More information

Synchronising Threads

Synchronising Threads Synchronising Threads David Chisnall March 1, 2011 First Rule for Maintainable Concurrent Code No data may be both mutable and aliased Harder Problems Data is shared and mutable Access to it must be protected

More information

Lecture 7: Transactional Memory Intro. Topics: introduction to transactional memory, lazy implementation

Lecture 7: Transactional Memory Intro. Topics: introduction to transactional memory, lazy implementation Lecture 7: Transactional Memory Intro Topics: introduction to transactional memory, lazy implementation 1 Transactions New paradigm to simplify programming instead of lock-unlock, use transaction begin-end

More information

Synchronization Lecture 24 Fall 2018

Synchronization Lecture 24 Fall 2018 Synchronization Lecture 24 Fall 2018 Prelim 2 tonight! The room assignments are on the course website, page Exams. Check it carefully! Come on time! Bring you Cornell id card! No lunch with gries this

More information

CSE 230. Concurrency: STM. Slides due to: Kathleen Fisher, Simon Peyton Jones, Satnam Singh, Don Stewart

CSE 230. Concurrency: STM. Slides due to: Kathleen Fisher, Simon Peyton Jones, Satnam Singh, Don Stewart CSE 230 Concurrency: STM Slides due to: Kathleen Fisher, Simon Peyton Jones, Satnam Singh, Don Stewart The Grand Challenge How to properly use multi-cores? Need new programming models! Parallelism vs Concurrency

More information

Concurrency and Synchronisation. Leonid Ryzhyk

Concurrency and Synchronisation. Leonid Ryzhyk Concurrency and Synchronisation Leonid Ryzhyk Textbook Sections 2.3 & 2.5 2 Concurrency in operating systems Inter-process communication web server SQL request DB Intra-process communication worker thread

More information

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems 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

More information

Operating systems and concurrency (B08)

Operating systems and concurrency (B08) Operating systems and concurrency (B08) David Kendall Northumbria University David Kendall (Northumbria University) Operating systems and concurrency (B08) 1 / 20 Introduction Semaphores provide an unstructured

More information

Chapter 32 Multithreading and Parallel Programming

Chapter 32 Multithreading and Parallel Programming Chapter 32 Multithreading and Parallel Programming 1 Objectives To get an overview of multithreading ( 32.2). To develop task classes by implementing the Runnable interface ( 32.3). To create threads to

More information

CS 347 Parallel and Distributed Data Processing

CS 347 Parallel and Distributed Data Processing CS 347 Parallel and Distributed Data Processing Spring 2016 Notes 5: Concurrency Control Topics Data Database design Queries Decomposition Localization Optimization Transactions Concurrency control Reliability

More information

Classical concurrency control: topic overview 1 In these lectures we consider shared writeable data in main memory

Classical concurrency control: topic overview 1 In these lectures we consider shared writeable data in main memory Classical concurrency control: topic overview 1 In these lectures we consider shared writeable data in main memory Controlling access by concurrent processes to shared writeable data has been studied as

More information

Problems with Concurrency. February 19, 2014

Problems with Concurrency. February 19, 2014 with Concurrency February 19, 2014 s with concurrency interleavings race conditions dead GUI source of s non-determinism deterministic execution model 2 / 30 General ideas Shared variable Access interleavings

More information

6 Transactional Memory. Robert Mullins

6 Transactional Memory. Robert Mullins 6 Transactional Memory ( MPhil Chip Multiprocessors (ACS Robert Mullins Overview Limitations of lock-based programming Transactional memory Programming with TM ( STM ) Software TM ( HTM ) Hardware TM 2

More information

Synchronization II: EventBarrier, Monitor, and a Semaphore. COMPSCI210 Recitation 4th Mar 2013 Vamsi Thummala

Synchronization II: EventBarrier, Monitor, and a Semaphore. COMPSCI210 Recitation 4th Mar 2013 Vamsi Thummala Synchronization II: EventBarrier, Monitor, and a Semaphore COMPSCI210 Recitation 4th Mar 2013 Vamsi Thummala Check point: Mission in progress Master synchronization techniques Develop best practices for

More information

Chapter 5 Asynchronous Concurrent Execution

Chapter 5 Asynchronous Concurrent Execution Chapter 5 Asynchronous Concurrent Execution Outline 5.1 Introduction 5.2 Mutual Exclusion 5.2.1 Java Multithreading Case Study 5.2.2 Critical Sections 5.2.3 Mutual Exclusion Primitives 5.3 Implementing

More information

C09: Process Synchronization

C09: Process Synchronization CISC 7310X C09: Process Synchronization Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/29/2018 CUNY Brooklyn College 1 Outline Race condition and critical regions The bounded

More information

OS06: Monitors in Java

OS06: Monitors in Java OS06: Monitors in Java Based on Chapter 4 of [Hai17] Jens Lechtenbörger Computer Structures and Operating Systems 2018 1 Introduction 1.1 OS Plan ˆ OS Motivation (Wk 23) ˆ OS Introduction (Wk 23) ˆ Interrupts

More information

Models of concurrency & synchronization algorithms

Models of concurrency & synchronization algorithms Models of concurrency & synchronization algorithms Lecture 3 of TDA383/DIT390 (Concurrent Programming) Carlo A. Furia Chalmers University of Technology University of Gothenburg SP3 2016/2017 Today s menu

More information

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

Process Synchronization: Semaphores. CSSE 332 Operating Systems Rose-Hulman Institute of Technology Process Synchronization: Semaphores CSSE 332 Operating Systems Rose-Hulman Institute of Technology Critical-section problem solution 1. Mutual Exclusion - If process Pi is executing in its critical section,

More information

Concurrent Programming with OpenMP

Concurrent Programming with OpenMP Concurrent Programming with OpenMP Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto Superior Técnico October 11, 2012 CPD (DEI / IST) Parallel and Distributed

More information