CSC321 §8 Implementing FSP Models in Java 1 Section 8 Implementing FSP Models in Java.

Slides:



Advertisements
Similar presentations
Operating Systems: Monitors 1 Monitors (C.A.R. Hoare) higher level construct than semaphores a package of grouped procedures, variables and data i.e. object.
Advertisements

Assignment 5 - FSP and Java 1.The log-flume ride at a theme park operates as follows. This sequence is repeated forever. Once the log is empty it cleaned.
– R 7 :: 1 – 0024 Spring 2010 Parallel Programming 0024 Recitation Week 7 Spring Semester 2010.
Ch 7 B.
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Concurrency: message passing1 ©Magee/Kramer 2 nd Edition Chapter 10 Message Passing.
CS 5704 Fall 00 1 Monitors in Java Model and Examples.
Concurrency: monitors & condition synchronization1 ©Magee/Kramer 2 nd Edition Chapter 5 Monitors & Condition Synchronization.
Concurrency: monitors & condition synchronization1 ©Magee/Kramer 2 nd Edition COMP60611 Fundamentals of Parallel and Distributed Systems Lecture 8b Monitors.
1 Condition Synchronization. 2 Synchronization Now that you have seen locks, is that all there is? No, but what is the “right” way to build a parallel.
1 Lecture 18 Further Threading Overview  Case Study: Cooperating Threads  Producer/ Consumer model threads  Waiting for Synchronized Data  Busy Waiting.
Monitors Chapter 7. The semaphore is a low-level primitive because it is unstructured. If we were to build a large system using semaphores alone, the.
CS 11 java track: lecture 7 This week: Web tutorial:
© Andy Wellings, 2004 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  Completing the Java Model  Overview of the.
Concurrency Monitors and Condition Synchronization Claus Brabrand
Synchronization in Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Semaphores CSCI 444/544 Operating Systems Fall 2008.
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
28-Jun-15 Producer-Consumer An example of using Threads.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Threads Just Java: C10–pages 251- C11–pages 275-
Threads II. Review A thread is a single flow of control through a program Java is multithreaded—several threads may be executing “simultaneously” If you.
© Amir Kirsh Threads Written by Amir Kirsh. 2 Lesson’s Objectives By the end of this lesson you will: Be familiar with the Java threads syntax and API.
Multithreading.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
1 Processes, Threads, Race Conditions & Deadlocks Operating Systems Review.
Concurrency: dynamic systems1 ©Magee/Kramer 2 nd Edition Chapter 9 Dynamic Systems.
CSC321 §6 Modelling Processes using FSP 1 Chapter 6 Modelling Processes using FSP.
Semaphores, Locks and Monitors By Samah Ibrahim And Dena Missak.
Practice Session 8 Blocking queues Producers-Consumers pattern Semaphore Futures and Callables Advanced Thread Synchronization Methods CountDownLatch Thread.
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
OPERATING SYSTEMS Frans Sanen.  Recap of threads in Java  Learn to think about synchronization problems in Java  Solve synchronization problems in.
In Java processes are called threads. Additional threads are associated with objects. An application is associated with an initial thread via a static.
Advanced Concurrency Topics Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Threads II IS Outline  Quiz  Thread review  Stopping a thread  java.util.Timer  Swing threads javax.swing.Timer  ProgressMonitor.
Li Tak Sing COMPS311F. Threads A thread is a single sequential flow of control within a program. Many programming languages only allow you to write programs.
Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
Java Producer-Consumer Monitor From: Concurrent Programming: The Java Programming Language By Steven J. Hartley Oxford University Press, 1998.
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Module 6: Process Synchronization Codes.
Threads. Objectives You must be able to answer the following questions –What code does a thread execute? –What states can a thread be in? –How does a.
Monitors CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Parallel Processing (CS526) Spring 2012(Week 8).  Shared Memory Architecture  Shared Memory Programming & PLs  Java Threads  Preparing the Environment.
Comunication&Synchronization threads 1 Programación Concurrente Benemérita Universidad Autónoma de Puebla Facultad de Ciencias de la Computación Comunicación.
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
CS 151: Object-Oriented Design November 26 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
1 Java Programming Java Programming II Concurrent Programming: Threads ( II)
Distributed and Parallel Processing George Wells.
Race Conditions & Synchronization
Threads in Java Two ways to start a thread
Java Concurrency.
Condition Synchronization
Lecture 24 Concurrency 2 (D&D 23) Date.
Section 5.7 Concurrency, Interference, and Synchronization.
Threads Chate Patanothai.
Java Concurrency.
Monitors Chapter 7.
Condition Variables and Producer/Consumer
Condition Variables and Producer/Consumer
Thread Synchronization
Race Conditions & Synchronization
Multithreading.
Background Concurrent access to shared data can lead to inconsistencies Maintaining data consistency among cooperating processes is critical What is wrong.
Monitors Chapter 7.
Monitors Chapter 7.
Chapter 6 Synchronization Principles
Software Engineering and Architecture
Don Porter Portions courtesy Emmett Witchel
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

CSC321 §8 Implementing FSP Models in Java 1 Section 8 Implementing FSP Models in Java

CSC321 §8 Implementing FSP Models in Java : The Carpark Model A controller is required for a carpark, which only permits cars to enter when the carpark is not full and does not permit cars to leave when there are no cars in the carpark. Car arrival and departure are simulated by separate threads.

CSC321 §8 Implementing FSP Models in Java 3 Carpark model  Events or actions of interest? arrive and depart  Identify processes. arrivals, departures and carpark control  Define each process and interactions (structure).

CSC321 §8 Implementing FSP Models in Java 4 Carpark model CARPARKCONTROL(N=4) = SPACES[N], SPACES[i:0..N] = ( when(i>0) arrive->SPACES[i-1] |when(i SPACES[i+1] ). ARRIVALS = (arrive->ARRIVALS). DEPARTURES = (depart->DEPARTURES). ||CARPARK = (ARRIVALS||CARPARKCONTROL(4)||DEPARTURES). Guarded actions are used to control arrive and depart.

CSC321 §8 Implementing FSP Models in Java 5 Carpark model

CSC321 §8 Implementing FSP Models in Java : Carpark Program  Model - all entities are processes interacting by actions  Program - need to identify threads and monitors  thread - active entity which initiates (output) actions  monitor - passive entity which responds to (input) actions.

CSC321 §8 Implementing FSP Models in Java 7 public class CarPark { public static void main(String[] args) { CarParkControl carpark = new CarParkControl(4); Thread arrivals = new Thread(new Arrivals(carpark)); Thread departures = new Thread(new Departures(carpark)); arrivals.start(); departures.start(); } Carpark Program

CSC321 §8 Implementing FSP Models in Java 8 Carpark program - Arrivals and Departures threads class Arrivals implements Runnable { CarParkControl carpark; Arrivals(CarParkControl c) {carpark = c;} public void run() { try { while(true) { carpark.arrive(); Time.delay(RandomGenerator.integer(0,520)); } } catch (InterruptedException e){} } How do we implement the control of CarParkControl? Similarly Departures which calls carpark.depart().

CSC321 §8 Implementing FSP Models in Java 9 Carpark program - CarParkControl monitor class CarParkControl { private int spaces; private int capacity; CarParkControl(int n) {capacity = spaces = n;} synchronized void arrive() { … --spaces; … } synchronized void depart() { … ++spaces; … } condition synchronization? block if full? (spaces==0) block if empty? (spaces==N) mutual exclusion by synch methods

CSC321 §8 Implementing FSP Models in Java 10 Carpark program - FSP to Java FSP: when cond act -> NEWSTAT Java: public synchronized void act() throws InterruptedException { while (!cond) wait(); // modify monitor data notifyAll() } The while loop is necessary to retest the condition cond to ensure that cond is indeed satisfied when it re-enters the monitor. notifyAll() is necessary to awaken other thread(s) that may be waiting to enter the monitor now that the monitor data has been changed.

CSC321 §8 Implementing FSP Models in Java 11 CarParkControl - condition synchronization class CarParkControl { private int spaces; private int capacity; CarParkControl(int n) {capacity = spaces = n;} synchronized void arrive() throws InterruptedException { while (spaces==0) wait(); --spaces; notify(); } synchronized void depart() throws InterruptedException { while (spaces==capacity) wait(); ++spaces; notify(); } Why is it safe to use notify() here rather than notifyAll() ?

CSC321 §8 Implementing FSP Models in Java 12 Models to monitors - summary Each guarded action in the model of a monitor is implemented as a synchronized method which uses a while loop and wait() to implement the guard. The while loop condition is the negation of the model guard condition. Active entities (that initiate actions) are implemented as threads. Passive entities (that respond to actions) are implemented as monitors. Changes in the state of the monitor are signaled to waiting threads using notify() or notifyAll().

CSC321 §8 Implementing FSP Models in Java Bounded Buffer A bounded buffer consists of a fixed number of slots. Items are put into the buffer by a producer process and removed by a consumer process. It can be used to smooth out transfer rates between the producer and consumer. (see car park example)

CSC321 §8 Implementing FSP Models in Java 14 bounded buffer - a data-independent model LTS: The behaviour of BOUNDEDBUFFER is independent of the actual data values, and so can be modelled in a data-independent manner.

CSC321 §8 Implementing FSP Models in Java 15 bounded buffer - a data-independent model BUFFER(N=5) = COUNT[0], COUNT[i:0..N] = (when (i COUNT[i+1] |when (i>0) take->COUNT[i-1] ). PRODUCER = (append->PRODUCER). CONSUMER = (take->CONSUMER). ||BOUNDEDBUFFER = (PRODUCER||BUFFER(5)||CONSUMER).

CSC321 §8 Implementing FSP Models in Java 16 bounded buffer - an implementation class Buffer { private int N =...; private Character[] buffer = new Character [N]; private int tail = 0, head = 0; private int count = 0; public synchronized void append (Character c) { while (count == N) try {wait();} catch (InterruptedException e){} buffer[tail] = c; tail = (tail + 1) % N; count++; notifyAll(); } public synchronized Character take() { Character c; while (count == 0) try {wait();} catch (InterruptedException e){} c = buffer[head]; head = (head + 1) % N; count--; notifyAll(); return c; }

CSC321 §8 Implementing FSP Models in Java 17 bounded buffer program - producer process class Producer implements Runnable { Buffer buf; String alphabet= "abcdefghijklmnopqrstuvwxyz"; Producer(Buffer b) {buf = b;} public void run() { int ai = 0; while(true) { buf.append(new Character(alphabet.charAt(ai))); ai=(ai+1) % alphabet.length(); } Similarly Consumer which calls buf.take().