C340 Concurrency: Semaphores and Monitors. Goals

Similar documents
Communications Software Engineering Condition Synchronization and Liveness

Monitors & Condition Synchronization

COMP30112: Concurrency Topics 4.1: Concurrency Patterns - Monitors

CSCI 5828: Foundations of Software Engineering

Monitors & Condition Synchronization

Monitors & Condition Synchronisation

Monitors & Condition Synchronization

Lecture 9: Introduction to Monitors

3C03 Concurrency: Starvation and Deadlocks

Software Architecture

Chapter 10. Message Passing. Concurrency: message passing 1. Magee/Kramer

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

Chapter 10. Message Passing. Concurrency: message passing 1. Magee/Kramer 2 nd Edition

Contribution:javaMultithreading Multithreading Prof. Dr. Ralf Lämmel Universität Koblenz-Landau Software Languages Team

Concurrency: State Models & Design Patterns

Shared Objects & Mutual Exclusion

SFDV3006 Concurrent Programming

Monitors; Software Transactional Memory

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

Performance Throughput Utilization of system resources

Lecture 10: Introduction to Semaphores

Chapter 6. Deadlock. DM519 Concurrent Programming

Monitors; Software Transactional Memory

CS18000: Programming I

System Software Assignment 8 Deadlocks

Contents. 6-1 Copyright (c) N. Afshartous

CPS 506 Comparative Programming Languages. Programming Language Paradigms

Concurrency in Java Prof. Stephen A. Edwards

Writing Parallel Programs COMP360

Answer the first question and two further questions. i. action prefix ( -> ) [3 marks]

Threads and Parallelism in Java

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages

Concurrency. Lecture 14: Concurrency & exceptions. Why concurrent subprograms? Processes and threads. Design Issues for Concurrency.

Concurrency: State Models & Design Patterns

Synchronization in Java

CMSC 330: Organization of Programming Languages. The Dining Philosophers Problem

Chapter 13. Concurrency ISBN

COMP30112: Concurrency Topics 5.2: Properties

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

Chapter Machine instruction level 2. High-level language statement level 3. Unit level 4. Program level

Synchronized Methods of Old Versions of Java

Concurrency - Topics. Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads

User Space Multithreading. Computer Science, University of Warwick

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

Java Threads. COMP 585 Noteset #2 1

Model Requirements and JAVA Programs MVP 2 1

Processes & Threads. Concepts: processes - units of sequential execution.

Chapter 2. Processes & Threads. Concurrency: processes & threads 1. Magee/Kramer

THE QUEEN S UNIVERSITY OF BELFAST

Concurrency 6 - Deadlock

Chapter 13 Topics. Introduction. Introduction

1) Discuss the mutual exclusion mechanism that you choose as implemented in the chosen language and the associated basic syntax

What is a thread anyway?

Concurrent Programming using Threads

CSCI 5828: Foundations of Software Engineering

Shared-Memory and Multithread Programming

Multiple Inheritance. Computer object can be viewed as

CMSC 330: Organization of Programming Languages. Threads Classic Concurrency Problems

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Synchronization Lecture 24 Fall 2018

Reintroduction to Concurrency

Synchronization Lecture 23 Fall 2017

Concurrent processes. Processes and Threads. Processes and threads. Going back to Concurrency. Modelling Processes. Modeling processes

CONCURRENT PROGRAMMING - EXAM

Safety & Liveness Properties

CSE 153 Design of Operating Systems

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

Concurrent Programming

What's wrong with Semaphores?

Models of concurrency & synchronization algorithms

5. Liveness and Guarded Methods

Principles of Software Construction: Concurrency, Part 2

Advances in Programming Languages

High Performance Computing Course Notes Shared Memory Parallel Programming

Lecture 8: September 30

Dealing with Issues for Interprocess Communication

COMP346 Winter Tutorial 4 Synchronization Semaphores

Concurrency: a crash course

COMP 150-CCP Concurrent Programming. Lecture 12: Deadlock. Dr. Richard S. Hall

Concurrent Programming. CS105 Programming Languages Supplement

OS06: Monitors in Java

Multithreading Pearson Education, Inc. All rights reserved.

CMSC 132: Object-Oriented Programming II. Threads in Java

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

CS 537 Lecture 8 Monitors. Thread Join with Semaphores. Dining Philosophers. Parent thread. Child thread. Michael Swift

CS180 Review. Recitation Week 15

Part IV Other Systems: I Java Threads

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

Threads Chate Patanothai

Operating Systems ECE344

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

Results of prelim 2 on Piazza

Introduction to Java Threads

Chair of Software Engineering. Java and C# in depth. Carlo A. Furia, Marco Piccioni, Bertrand Meyer. Java: concurrency

$ Program Verification

Only one thread can own a specific monitor

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

Race Conditions & Synchronization

Question Points Score Total 100

Question Points Score Total 100

Transcription:

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 the monitor at any time wait, notify and notifyall 2 1

Semaphores Introduced by Dijkstra in 1968 ADT with counter and waiting list P/Wait/Down: if (counter > 0) counter-- else add caller to waiting list S/Signal/Up: if (threads wait) activate waiting thread else counter++ 3 Semaphores and Mutual Exclusion One semaphore for each critical section Initialize semaphore to 1. Embed critical sections in wait/signal pair Example in Java: Semaphore S=new Semaphore(1); S.down(); <critical section> S.up(); Demo: Semaphores 4 2

Evaluation of Semaphores + Nice and simple mechanism + Can be efficiently implemented + Available in every programming language Too low level of abstraction Unstructured use of signal and wait leads to spaghetti synchronisation Error prone and errors are dangerous Omitting signal leads to deadlocks Omitting wait leads to safety violations 5 class Semaphore { private int value_; Semaphore (int initial) { value_ = initial; synchronized public void up() { ++value_; notify(); synchronized public void down() { while (value_== 0) { try {wait(); catch (InterruptedException e){ --value_; 6 3

Critical Regions Guarantee mutual exclusion by definition Note subtle difference to critical sections language features implement critical regions Example: Java synchronised method 7 Monitors Hoare s response to Dijkstra s semaphores Higher-level Structured Monitors encapsulate data structures that are not externally accessible Mutual exclusive access to data structure enforced by compiler or language run-time 8 4

Monitors in Java All instance and class variables need to be private or protected All methods need to be synchronised Example: semaphore implementation Use of Monitors: Carpark Problem 9 Carpark Problem Only admit cars if carpark is not full Cars can only leave if carpark is not empty Car arrival and departure are independent threads Demo: CarPark 10 5

Carpark Model Events or actions of interest: Arrive and depart Processes: Arrivals, departures and carpark control Process and Interaction structure: CARPARK ARRIVALS arrive CARPARK depart CONTROL DEPART- URES 11 Carpark FSP Specification CARPARKCONTROL(N=4) = SPACES[N], SPACES[i:0..N] = (when(i>0) arrive-> SPACES[i-1] when(i<n) depart-> SPACES[i+1] ). ARRIVALS = (arrive-> ARRIVALS). DEPARTURES = (depart-> DEPARTURES). CARPARK = (ARRIVALS CARPARKCONTROL DEPARTURES). LTSA 12 6

Java Class Carpark public class Carpark extends Applet { final static int N=4; public void init() { CarParkControl cpk = new CarParkControl(N); Thread arrival,departures; arrivals=new Thread(new Arrivals(cpk)); departures=new Thread(new Departures(cpk)); arrivals.start(); departures.start(); 13 Java Classes Arrivals & Departures public class Arrivals implements Runnable { CarParkControl carpark; Arrivals(CarParkControl c) {carpark = c; public void run() { while (true) carpark.arrive(); class Departures implements Runnable {... public void run() { while (true) carpark.depart(); 14 7

Java Class CarParkControl (Monitor) class CarParkControl {// synchronisation? private int spaces; private int N; CarParkControl(int capacity) { N = capacity; spaces = capacity; synchronized public void arrive() { -- spaces; {// Block if full? synchronized public void depart() { ++ spaces; {// Block if empty? 15 Problems with CarParkControl How do we send arrivals to sleep if car park is full? How do we awake it if space becomes available? Solution: Condition synchronisation 16 8

Summary Semaphores Monitors Next session: Java condition synchronization Relationship between FSP guarded actions and condition synchronization Fairness and Starvation 17 9