Parallel Programming

Similar documents
Lecture 6: Introduction to Monitors and Semaphores

Outline of this lecture G52CON: Concepts of Concurrency

3C03 Concurrency: Condition Synchronisation

Chapter 6 Concurrent Programming

Monitors, Java, Threads and Processes

Lecture 6: Semaphores and Monitors

Monitors (Deprecated)

Last Class: Semaphores

Concurrent programming in Java

Monitors & Condition Synchronization

! Past few lectures: Ø Locks: provide mutual exclusion Ø Condition variables: provide conditional synchronization

Semaphores and Monitors: High-level Synchronization Constructs

CS11 Java. Fall Lecture 7

Chapter 8 Implementing FSP Models in Java

Unit 6: Conditions, Barriers, and RendezVous

MonitorExplorer: A State Exploration-Based Approach to Testing Java Monitors

Lecture 8: Safety and Liveness Properties

4. Monitors. Monitors support data encapsulation and information hiding and are easily adapted to an object-oriented environment.

SSC - Concurrency and Multi-threading Java multithreading programming - Synchronisation (I)

Advanced Multiprocessor Programming

Resurrecting Ada s Rendez-Vous in Java

Programming with Data Structures

Overview Motivating Examples Interleaving Model Semantics of Correctness Testing, Debugging, and Verification

Distributed Systems [Fall 2012]

Mutual Exclusion using Monitors

This lecture. Abstract data types Stacks Queues. ADTs, Stacks, Queues Goodrich, Tamassia

Stacks. Stacks (and Queues) Stacks. q Stack: what is it? q ADT. q Applications. q Implementation(s) CSCU9A3 1

Thread Synchronization and the Java Monitor

6 Synchronization with Semaphores

Concurrent Programming

Course: Programming II - Abstract Data Types. The ADT Queue. (Bobby, Joe, Sue, Ellen) Add(Ellen) Delete( ) The ADT Queues Slide Number 1

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

Monitors and Semaphores

Built-in Concurrency Primitives in Java Programming Language. by Yourii Martiak and Mahir Atmis

Monitor Object. An Object Behavioral Pattern for Concurrent Programming. Douglas C. Schmidt

8. Condition Objects. Prof. O. Nierstrasz. Selected material 2005 Bowbeer, Goetz, Holmes, Lea and Peierls

Data Structures and Algorithms Lists

TESTING JAVA MONITORS BY STATE SPACE EXPLORATION MONICA HERNANDEZ. Presented to the Faculty of the Graduate School of

QUEUES. Primitive Queue operations. enqueue (q, x): inserts item x at the rear of the queue q

Threads 1. When writing games you need to do more than one thing at once.

Java Concurrency Framework. Sidartha Gracias

Java Virtual Machine Locks

A Survey of Parallel Processing in Linux

aicas Technology Multi Core und Echtzeit Böse Überraschungen vermeiden Dr. Fridtjof Siebert CTO, aicas OOP 2011, 25 th January 2011

Rigorous Software Development CSCI-GA

Computer Science 483/580 Concurrent Programming Midterm Exam February 23, 2009

Multithreaded Programming

Design Patterns in C++

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

Outline. The Stack ADT Applications of Stacks Array-based implementation Growable array-based stack. Stacks 2

Topics. Producing Production Quality Software. Concurrent Environments. Why Use Concurrency? Models of concurrency Concurrency in Java

Shared Address Space Computing: Programming

Linux Driver Devices. Why, When, Which, How?

Facing the Challenges for Real-Time Software Development on Multi-Cores

Computing Concepts with Java Essentials

race conditions Image courtesy of photostock / FreeDigitalPhotos.net Flavia Rainone - Principal Software Engineer

COS 318: Operating Systems. Semaphores, Monitors and Condition Variables

System Software Winter 2015

Real Time Programming: Concepts

Tuple spaces and Object spaces. Distributed Object Systems 12. Tuple spaces and Object spaces. Communication. Tuple space. Mechanisms 2.

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

How To Write A Multi Threaded Software On A Single Core (Or Multi Threaded) System

What is a Stack? Stacks and Queues. Stack Abstract Data Type. Java Interface for Stack ADT. Array-based Implementation

Java Memory Model: Content

CmpSci 187: Programming with Data Structures Spring 2015

SYSTEM ecos Embedded Configurable Operating System

Data Types. Abstract Data Types. ADTs as Design Tool. Abstract Data Types. Integer ADT. Principle of Abstraction

PROGRAMMING CONCEPTS AND EMBEDDED PROGRAMMING IN C, C++ and JAVA: Lesson-4: Data Structures: Stacks

Semaphores. Otto J. Anshus University of {Tromsø, Oslo}

Threads & Tasks: Executor Framework

OBJECT ORIENTED PROGRAMMING LANGUAGE

Algorithms and Data Structures Written Exam Proposed SOLUTION

CompSci-61B, Data Structures Final Exam

Concurrency and Parallelism

HW3: Programming with stacks

OPERATING SYSTEMS PROCESS SYNCHRONIZATION

Monitors and Exceptions : How to Implement Java efficiently. Andreas Krall and Mark Probst Technische Universitaet Wien

Priority Based Implementation in Pintos

Deadlock Victim. dimanche 6 mai 12

API for java.util.iterator. ! hasnext() Are there more items in the list? ! next() Return the next item in the list.

Grundlæggende Programmering IT-C, Forår Written exam in Introductory Programming

Building a Multi-Threaded Web Server

DATA STRUCTURE - STACK

A Deduplication File System & Course Review

Concurrent Programming

Monitor-Konzepte. nach Brinch Hansen. Konzepte von Betriebssystem-Komponenten: Konkurrenz und Koordinierung in Manycore-Systemen.

W4118 Operating Systems. Instructor: Junfeng Yang

Sophos Mobile Control Web service guide

Comp 255Q - 1M: Computer Organization Lab #3 - Machine Language Programs for the PDP-8

Synchronization. Todd C. Mowry CS 740 November 24, Topics. Locks Barriers

Comparing Java, C# and Ada Monitors queuing policies : a case study and its Ada refinement

3. Java Nuggets in a Nutshell

Queues Outline and Required Reading: Queues ( 4.2 except 4.2.4) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

Scheduling recurring tasks in Java applications

Computer Programming I

Application of Stacks: Postfix Expressions Calculator (cont d.)

Creating a Simple, Multithreaded Chat System with Java

Conditions for Strong Synchronization In Concurrent Data Types

The Little Book of Semaphores

Better Monitors for Java

Transcription:

Parallel Programming 0024 Recitation Week 7 Spring Semester 2010 R 7 :: 1 0024 Spring 2010

Today s program Assignment 6 Review of semaphores Semaphores Semaphoren and (Java) monitors Semaphore implementation of monitors (review) Assignment 7 R 7 :: 2 0024 Spring 2010

Semaphores Special Integer variable w/2 atomic operations P() V() (Passeren, wait/up) (Vrijgeven/Verhogen, signal/down) Names of operations reflect the Dutch origin of the inventor... R 7 :: 3 0024 Spring 2010

class Semaphore public class Semaphore { private int value; public Semaphore() { value = 0; public Semaphore(int k) { value = k; public synchronized void P() { /* see next slide */ public synchronized void V() { /* see next slide */ R 7 :: 4 0024 Spring 2010

P() operation public synchronized void P() { while (value == 0) { try { wait(); catch (InterruptedException e) { value --; R 7 :: 5 0024 Spring 2010

V() operation public synchronized void V() { ++value; notifyall(); R 7 :: 6 0024 Spring 2010

Comments You can modify the value of an semphore instance only using the P() and V() operations. Effect Initialize in constructor P() may block V() never blocks Application of semaphores: Mutual exclusion Conditional synchronization R 7 :: 7 0024 Spring 2010

2 kinds of semaphores Binary semaphore Value is either 0 or 1 Supports implemention of mutual exclusion Semaphore s = new Semaphore(1); s.p() //critical section s.v() Counting (general) semaphore Value can be any positive integer value R 7 :: 8 0024 Spring 2010

Fairness A semaphore is considered to be fair if all threads that execute a P() operation eventually succeed. Different models of fairness Semaphore is unfair : a thread blocked in the P() operation must wait forever while other threads (that executed the operation later) succeed. R 7 :: 9 0024 Spring 2010

Semaphores in Java java.util.concurrent.semaphore acquire() instead of P() release() instead of V() Constructors Semaphore(int permits); Semaphore(int permits, boolean fair); Semaphore(int permits, boolean fair); permits: initial value fair: if true then the semphore uses a FIFO to manage blocked threads R 7 :: 10 0024 Spring 2010

Semaphores and monitors Monitor: model for synchronized methods in Java Both constructs are equivalent One can be used to implement the other R 7 :: 11 0024 Spring 2010

Example from Mar 18 See slides for context R 7 :: 12 0024 Spring 2010

Buffer using condition queues class BoundedBuffer extends Buffer { public BoundedBuffer(int size) { super(size); public synchronized void insert(object o) throws InterruptedException { while (isfull()) { wait(); doinsert(o); notifyall(); R 7 :: 13 0024 Spring 2010

Buffer using condition queues, continued // in class BoundedBuffer public synchronized Object extract() throws InterruptedException { while (isempty()) { wait(); Object o = doextract(); notifyall(); return o; // extract // BoundedBuffer R 7 :: 14 0024 Spring 2010

Emulation of monitor w/ semaphores We need 2 semaphores: One to make sure that only one synchronized method executes at any tiven time call this the access semaphore (access) binary semaphore One semaphore to line up threads that are waiting for some condition call this the condition semaphore (cond) also binary threads that wait must do an acquire and this will not For convenience: Counter waitthread to count number of waiting threads i.e., threads in queue for cond complete R 7 :: 15 0024 Spring 2010

Basic idea 1) Frame all synchronized methods with access.acquire() and access.release() This ensures that only one thread executes a synchronized method at any point in time Recall access is binary. 1) Translate wait() and notifyall() to give threads waiting in line a chance to progress (these threads use cond) To simplify the debate, we require that notifyall() is the last action in a synchronized method Java does not enforce this requirement but the mapping of synchronized methods into semaphores is simplified. R 7 :: 16 0024 Spring 2010

Buffer with auxiliary fields class BoundedBuffer extends Buffer { public BoundedBuffer(int size) { super(size); access = new Semaphore(1); cond = new Semaphore(0); private Semaphore access; private Semaphore cond; private int waitthread = 0; // continued... R 7 :: 17 0024 Spring 2010

1) Framing all methods public void insert(object o) throws InterruptedException { access.acquire(); //ensure mutual exclusion while (isfull()) { wait(); doinsert(o); notifyall(); access.release(); // insert R 7 :: 18 0024 Spring 2010

Notes There is one semaphore for all synchronized methods of one instance of BoundedBuffer Why? Must make sure that insert and extract don t overlap. There must be separate semaphore to deal with waiting threads. Why? Imagine the buffer is full. A thread that attempts to insert an item must wait. But it must release the access semaphore. Otherwise a thread that wants to remove an item will not be able to executed the (synchronized!) extract method. R 7 :: 19 0024 Spring 2010

2) Translate wait and notifyall The operation wait() is translated into waitthread++ access.release(); // other threads can execute methods cond.acquire(); access.acquire(); waitthread // wait until condition changes R 7 :: 20 0024 Spring 2010

Each occurrrence of NotifyAll() is translated to for (int i=0; i < waitthread; i++) { cond.release(); All threads waiting are released and will compete to (re)acquire access. They decrement waitthread after they leave cond.acquire Note that to enter the line (i.e., increment waitthread) the thread must hold the access semaphore access R 7 :: 21 0024 Spring 2010

Recall that access.release is done at the end of the synchronized method So all the threads that had lined up waiting for cond compete to get access to access No thread can line up while the cond.release operations are done since this thread holds access. R 7 :: 22 0024 Spring 2010

Note We wake up all threads they might not be able to enter their critical section if the condition they waited for does not hold. But all threads get a chance. This approach is different from what we discussed in the lecture R 7 :: 23 0024 Spring 2010

Translate wait() public void insert(object o) throws InterruptedException { access.acquire(); while (isfull()) { waitthread++; access.release(); // let other thread access object cond.acquire(); // wait for change of state access.acquire() waitthread ; doinsert(o); notifyall(); access.release(); R 7 :: 24 0024 Spring 2010

Translate notifyall() public void insert(object o) throws InterruptedException { access.acquire(); while (isfull()) { waitthread ++; access.release(); // let other thread access object cond.acquire(); // wait for change of state access.acquire() waitthread ; doinsert(o); for (int i; i < waitthread; i++) { cond.release(); access.release(); // insert R 7 :: 25 0024 Spring 2010

Example Consider the buffer, one slot is empty, four operations insert I1 insert I2 insert I3 extract E I1 access.acquire I2 access.acquire: blocks on access I3 access.acquire: blocks on access I1 waitthread == 0 I1.release R 7 :: 26 0024 Spring 2010

(cont) I2 access.acquire completes I2 buffer full waitthread =1 I2 access.release I2 cond.acquire blocks on cond I3 access.acquire completes I3 buffer full waitthread = 2 I3 access.release I3 cond.acquire blocks on cond R 7 :: 27 0024 Spring 2010

(cont) E access.acquire remove item E cond.release E cond.release E access.release One of I2 or I3 will succeed with access.acquire and be able to insert the next item R 7 :: 28 0024 Spring 2010

Exercise How would the method extract look like (if we used semaphores to emulate monitors)? R 7 :: 29 0024 Spring 2010

Assignment 7 Hint: Thread.currentThread() returns a handle to the current thread and might be useful for the assignment. R 7 :: 30 0024 Spring 2010

Overview Your task is to implement a Read/Write Lock Max. 4 Threads Max. 2 Reader Threads (shared access is allowed) and 1 Writer Thread A thread that executes read() is a reader At a later time it can be a writer... R 7 :: 31 0024 Spring 2010

The challenge No starvation Efficient implementation If there are fewer than 2 readers and no waiting writers then the next reader must be allowed to proceed If there is no contention then a thread must be allowed to proceed immediately Your implementation must be fair (you may want to use FIFOQueue.java) R 7 :: 32 0024 Spring 2010

Other comments Keep your solution as simple as possible Decide what you want to use [your decision] Monitors (synchronized methods) Semaphores http://java.sun.com/j2se/1.5.0/docs/api/ Semaphore Only change class Monitor.java If you feel it s necessary to change other files, please let us know. Please comment your code! R 7 :: 33 0024 Spring 2010

You may want to recall Thread.currentThread().getId() wait_list.enq(thread.currentthread().getid()) wait_list.getfirstitem() == Thread.currentThread().getId() R 7 :: 34 0024 Spring 2010

Your log could look like this READ LOCK ACQUIRED 1 READ LOCK RELEASED 0 WRITE LOCK ACQUIRED 1 WRITE LOCK RELEASED 0 READ LOCK ACQUIRED 1 READ LOCK ACQUIRED 2 READ LOCK RELEASED 1 READ LOCK ACQUIRED 2 READ LOCK RELEASED 1 READ LOCK ACQUIRED 2 R 7 :: 35 0024 Spring 2010