Concurrency/synchronization using UML state models April 14 th, 2008 Michigan State University.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

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.
Ken Birman 1. Refresher: Dekers Algorithm Assumes two threads, numbered 0 and 1 CSEnter(int i) { int J = i^1; inside[i] = true; turn = J; while(inside[J]
Ch 7 B.
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Lecture 5 Concurrency and Process/Thread Synchronization     Mutual Exclusion         Dekker's Algorithm         Lamport's Bakery Algorithm.
Chapter 6: Process Synchronization
1 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
Remote Procedure Call (RPC)
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 5 Multithreading and.
CS 11 java track: lecture 7 This week: Web tutorial:
CS444/CS544 Operating Systems Introduction to Synchronization 2/07/2007 Prof. Searleman
Multi-Object Synchronization. Main Points Problems with synchronizing multiple objects Definition of deadlock – Circular waiting for resources Conditions.
CS444/CS544 Operating Systems Synchronization 2/16/2006 Prof. Searleman
CS444/CS544 Operating Systems Synchronization 2/21/2007 Prof. Searleman
Chapter 6 – Concurrent Programming Outline 6.1 Introduction 6.2Monitors 6.2.1Condition Variables 6.2.2Simple Resource Allocation with Monitors 6.2.3Monitor.
Concurrency/synchronization using UML state models and sequence models: A brief review April 21 st, 2008 Michigan State University.
K. Stirewalt CSE 335: Software Design Software Architecture and Larger System Design Issues Lecture 3: Synchronization Topics: –Concurrent access to shared.
Process Synchronization Ch. 4.4 – Cooperating Processes Ch. 7 – Concurrency.
CS Introduction to Operating Systems
Discussion Week 3 TA: Kyle Dewey. Overview Concurrency overview Synchronization primitives Semaphores Locks Conditions Project #1.
Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.
1 Advanced Computer Programming Concurrency Multithreaded Programs Copyright © Texas Education Agency, 2013.
1 CSCD 330 Network Programming Lecture 13 More Client-Server Programming Sometime in 2014 Reading: References at end of Lecture.
Monitor  Giving credit where it is due:  The lecture notes are borrowed from Dr. I-Ling Yen at University of Texas at Dallas  I have modified them and.
1 Thread II Slides courtesy of Dr. Nilanjan Banerjee.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Atomic Operations David Monismith cs550 Operating Systems.
Concurrency, Mutual Exclusion and Synchronization.
CSE 425: Concurrency III Monitors A monitor is a higher level construct for synchronizing multiple threads’ access to a common code segment –Can implement.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Multithreading : synchronization. Avanced Programming 2004, Based on LYS Stefanus’s slides slide 4.2 Solving the Race Condition Problem A thread must.
1 Concurrency Architecture Types Tasks Synchronization –Semaphores –Monitors –Message Passing Concurrency in Ada Java Threads.
Internet Software Development Controlling Threads Paul J Krause.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
1 G53SRP: Java Concurrency Control (1) - synchronisation Chris Greenhalgh School of Computer Science.
Dr. R R DOCSIT, Dr BAMU. Basic Java :Multi Threading Cont. 2 Objectives of This Session Explain Synchronization in threads Demonstrate use of.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
Monitors CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
COSC 3407: Operating Systems Lecture 9: Readers-Writers and Language Support for Synchronization.
Fall 2008Programming Development Techniques 1 Topic 20 Concurrency Section 3.4.
Lecture 3 Concurrency and Thread Synchronization     Mutual Exclusion         Dekker's Algorithm         Lamport's Bakery Algorithm.
Chapter 71 Monitors (7.7)  A high-level-language object-oriented concept that attempts to simplify the programming of synchronization problems  A synchronization.
CS 153 Design of Operating Systems Winter 2016 Lecture 7: Synchronization.
3/17/2016cse synchronization-p2 © Perkins, DW Johnson and University of Washington1 Synchronization Part 2 CSE 410, Spring 2008 Computer.
Concurrent Programming in Java Based on Notes by J. Johns (based on Java in a Nutshell, Learning Java) Also Java Tutorial, Concurrent Programming in Java.
Concurrency/synchronization using UML state models November 27th, 2007 Michigan State University.
Synchronization (Threads Accessing Shared Data). Contents I.The Bank Transfer Problem II.Doing a Simulation on the Bank Transfer Problem III.New Requirement:
CSCD 330 Network Programming
CS703 - Advanced Operating Systems
CSE 120 Principles of Operating
CS703 – Advanced Operating Systems
Concurrency/synchronization using UML state models
CSCI 511 Operating Systems Chapter 5 (Part C) Monitor
Threading And Parallel Programming Constructs
Critical section problem
Monitor Object Pattern
Concurrency: Mutual Exclusion and Process Synchronization
Monitor Giving credit where it is due:
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 451: Operating Systems Winter 2003 Lecture 7 Synchronization
CSE 153 Design of Operating Systems Winter 19
CSE 153 Design of Operating Systems Winter 2019
CSCD 330 Network Programming
Don Porter Portions courtesy Emmett Witchel
Presentation transcript:

Concurrency/synchronization using UML state models April 14 th, 2008 Michigan State University

Overview State models of monitor objects Monitor-process pattern Heuristic for “generating” correct synchronization code from a state model Use of signal vs. broadcast to notify waiting threads Examples Synchronization that is more difficult to model using state diagrams

Method for using models to design concurrent software State model for each system object Passive objects modeled using monitor-process pattern Models then refined into code skeletons Active objects incorporate infrastructure needed to host a thread:  E.g., a distinguished run method whose activation corresponds to the lifetime of a thread Passive objects designed to behave as monitors  Design pattern for passive-object model guarantees monitor-object pattern can be applied to develop code  Guarded transitions in state model engender condition synchronization in code

Simple monitor-process pattern Simple pattern: Distinguished initial state Idle Composite state for each monitor operation  Reachable only from Idle via an accept-call action Entry is named according to the name of the operation Possibly guarded with an enabling condition  Must transition back to Idle via a reply action upon exit Names the entry to which it corresponds Includes return value (if any)  No accept-call actions within the composite state Variations on this pattern will relax some of these restrictions

Simple monitor-process pattern Idle [guard] / accept-call ( op(parms) ) / reply (op, result ) Monitor process Operation body

Bounded Buffer Example / reply ( pull, rv ) BoundedBuffer Idle [queue_.size != 0] / accept-call ( pull ) Pulling do/ rv := queue_.pull Pushing do/ queue_.push(x) / reply ( push ) [queue_.size != MAX] / accept-call ( push(x) )

From simple monitor process to monitor object (code) Class declares a private mutex variable lock Each operation state in the model becomes a monitor method in the code Method body bracketed by acquire/release of lock If any transition out of Idle is guarded: Class must declare a condition variable, associated with lock, to wait on when guard condition is NOT satisfied Method body includes a condition  wait loop immediately following acquisition of lock All other methods must notify this condition variable if they might serve to satisfy this guard condition Notification could be signal or broadcast

Example: Code for Buffer::push void Buffer::push(int x) { lock_.acquire(); while (queue_.size() == MAX) { full_.wait(); } queue_.push(x); empty_.signal(); lock_.release(); } Idle [queue_.size() != MAX] / accept-call (push(x)) Pushing do/ queue_.push(x) / reply (push)

Example: Code for Buffer::push void Buffer::push(int x) { lock_.acquire(); while (queue_.size() == MAX) { full_.wait(); } queue_.push(x); empty_.signal(); lock_.release(); } Idle [queue_.size() != MAX] / accept-call (push(x)) Pushing do/ queue_.push(x) / reply (push)

Example: Code for Buffer::push void Buffer::push(int x) { lock_.acquire(); while (queue_.size() == MAX) { full_.wait(); } queue_.push(x); empty_.signal(); lock_.release(); } Idle [queue_.size() != MAX] / accept-call (push(x)) Pushing do/ queue_.push(x) / reply (push)

Example: Code for Buffer::push void Buffer::push(int x) { lock_.acquire(); while (queue_.size() == MAX) { full_.wait(); } queue_.push(x); empty_.signal(); lock_.release(); } Idle [queue_.size() != MAX] / accept-call (push(x)) Pushing do/ queue_.push(x) / reply (push)

Example: Code for Buffer::pull int Buffer::pull() { lock_.acquire(); while (queue_.size() == 0) { empty_.wait(); } int rv = queue_.pull(); full_.signal(); lock_.release(); return rv; } Idle [queue_.size() != 0] / accept-call (pull) Pushing do/ rv := queue_.pull() / reply (pull, rv)

More complex guard conditions Consider a banking application that allows multiple clients to deposit and withdraw funds from shared accounts Goals: Protect against data races, so that money is not accidentally created or destroyed Prevent overdrafts by making withdraw requests block if account has insufficient funds Question: How should we model the behavior of an account object?

Monitor-process model of BankAccount / reply ( deposit ) BankAccount Idle / accept-call ( deposit(amount) ) Depositing do/ balance_ += amount; Withdrawing do/ balance_ -= amount; / reply ( withdraw ) [ amount <= balance_ ] / accept-call ( withdraw(amount) )

Code for BankAccount::withdraw void BankAccount::withdraw(int amount) { lock_.acquire(); while (amount > balance_) { okToWithdraw_.wait(); } balance_ -= amount; lock_.release(); } Idle [amount <= balance_] / accept-call (withdraw(amount)) Withdrawing do/ balance_ -= amount; / reply (withdraw)

Code for BankAccount::deposit void BankAccount::deposit(int amount) { lock_.acquire(); balance_ += amount; okToWithdraw_.broadcast(); lock_.release(); } Idle / accept-call (deposit(amount)) Depositing do/ balance_ += amount; / reply (deposit)

Signal vs. Broadcast When one thread changes the value of a condition upon which others might be waiting, the modifier is obliged to notify these waiting threads Always safest, though perhaps not very efficient, to use broadcast to notify waiting threads after a change Question: When is it safe to use signal?

More complex monitors… Simple monitor-process pattern insufficient for handling some forms of synchronization E.g., barriers, using which two or more threads must arrive before either may pass Not so easily modeled using guarded transitions May require: Embedding accept-call actions involving other operations within an operation state Explicit modeling, storage, and retrieval of request objects associated with calls into an entry

Party-admission problem Models the admission of (boy–girl) couples to a party Boys and girls arrive independently but are blocked from entering the party except as couples When a boy arrives, he must block unless and until a girl has arrived for him to hook up with Example of something called a barrier synchronization Requires embedding accept-call action for girlArrives inside operation state for boyArrives and vice versa Violates our simple monitor-process conventions Still, may be modeled without too much trouble and without any use of guards on transitions

Example: Part of the model / accept-call (boyArrives) BoyWaiting / reply (boyArrives) / accept-call (girlArrives) / reply (girlArrives) BoyMeetsGirl Idle

Example: Other part of the model / accept-call (girlArrives) GirlWaiting / reply (girlArrives) / accept-call (boyArrives) / reply (boyArrives) BoyMeetsGirl Idle