Concurrency/synchronization using UML state models November 27th, 2007 Michigan State University.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Concepts & Notations. Acknowledgements  The material in this tutorial is based in part on: Concurrency: State Models & Java Programming, by Jeff Magee.
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.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
CS 5704 Fall 00 1 Monitors in Java Model and Examples.
Concurrency/synchronization using UML state models April 14 th, 2008 Michigan State University.
1 CS 333 Introduction to Operating Systems Class 6 – Monitors and Message Passing Jonathan Walpole Computer Science Portland State University.
Page 1 Processes and Threads Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
Chapter 11: Distributed Processing Parallel programming Principles of parallel programming languages Concurrent execution –Programming constructs –Guarded.
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.
K. Stirewalt CSE 335: Software Design Administrivia Last homework will be #9, assigned later this week –Thus, need to complete 7 of 9 rather than 8 of.
CS444/CS544 Operating Systems Classic Synchronization Problems 2/26/2007 Prof. Searleman
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.
CS510 Concurrent Systems Introduction to Concurrency.
מידול התנהגותי 1. Today’s Session Sequence Diagrams State Machines 2.
1 Object-Oriented Modeling Using UML (2) CS 3331 Fall 2009.
CS5204 – Operating Systems 1 Communicating Sequential Processes (CSP)
E. Kraemer CSE 335: Software Design Software Architecture and Larger System Design Issues Lecture 6: Advanced state modeling/analysis Topics: –Modeling/analyzing.
1 CS 333 Introduction to Operating Systems Class 6 – Monitors and Message Passing Jonathan Walpole Computer Science Portland State University.
CSE 425: Concurrency III Monitors A monitor is a higher level construct for synchronizing multiple threads’ access to a common code segment –Can implement.
Java Threads 11 Threading and Concurrent Programming in Java Introduction and Definitions D.W. Denbo Introduction and Definitions D.W. Denbo.
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
Semaphores, Locks and Monitors By Samah Ibrahim And Dena Missak.
1 Concurrency Architecture Types Tasks Synchronization –Semaphores –Monitors –Message Passing Concurrency in Ada Java Threads.
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
1 Qualitative Reasoning of Distributed Object Design Nima Kaveh & Wolfgang Emmerich Software Systems Engineering Dept. Computer Science University College.
University of Toronto at Scarborough © Kersti Wain-Bantin CSCC40 state charts 1 object states an object state is a condition or situation during the life.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
11/18/20151 Operating Systems Design (CS 423) Elsa L Gunter 2112 SC, UIUC Based on slides by Roy Campbell, Sam.
Using a simple Rendez-Vous mechanism in Java
ICS 313: Programming Language Theory Chapter 13: Concurrency.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
Consider the program fragment below left. Assume that the program containing this fragment executes t1() and t2() on separate threads running on separate.
Java Thread and Memory Model
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
13-1 Chapter 13 Concurrency Topics Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads C# Threads.
Threads-Process Interaction. CONTENTS  Threads  Process interaction.
Practical Object-Oriented Design with UML 2e Slide 1/1 ©The McGraw-Hill Companies, 2004 PRACTICAL OBJECT-ORIENTED DESIGN WITH UML 2e Chapter 10: Statecharts.
1 Previous Lecture Overview  semaphores provide the first high-level synchronization abstraction that is possible to implement efficiently in OS. This.
Operating Systems CMPSC 473 Signals, Introduction to mutual exclusion September 28, Lecture 9 Instructor: Bhuvan Urgaonkar.
SaUML -- Synchronization- adorned UML diagrams Eileen Kraemer April 23, 2007 Michigan State University.
Chapter 71 Monitors (7.7)  A high-level-language object-oriented concept that attempts to simplify the programming of synchronization problems  A synchronization.
1 Chapter 11 Global Properties (Distributed Termination)
CS510 Concurrent Systems Jonathan Walpole. Introduction to Concurrency.
1 5-High-Performance Embedded Systems using Concurrent Process (cont.)
Semaphores Chapter 6. Semaphores are a simple, but successful and widely used, construct.
CS162 Section 2. True/False A thread needs to own a semaphore, meaning the thread has called semaphore.P(), before it can call semaphore.V() False: Any.
State Modeling. Introduction A state model describes the sequences of operations that occur in response to external stimuli. As opposed to what the operations.
Concurrency/synchronization using UML state models
CS510 Operating System Foundations
Software Architecture and Larger System Design Issues
CS510 Operating System Foundations
5-High-Performance Embedded Systems using Concurrent Process (cont.)
Multithreading.
Chapter 30 Condition Variables
Semaphores Chapter 6.
Subject : T0152 – Programming Language Concept
CSE 153 Design of Operating Systems Winter 19
CS333 Intro to Operating Systems
Presentation transcript:

Concurrency/synchronization using UML state models November 27th, 2007 Michigan State University

Overview Programming model used to implement multi-threaded concurrency Threads Monitors Condition synchronization Application of UML 2.0 state diagrams to model and reason about concurrent interactions

Overview Programming model used to implement multi-threaded concurrency Threads Monitors Condition synchronization Application of UML 2.0 state diagrams to model and reason about concurrent interactions

Thread Concepts A thread is always in one of three states: running ready suspended context switch wait signal

b : sharedBuffer startWrite() p:Producer c : Consumer startRead() sd scenario n Sequence diagram Snapshot of execution in progress Active object Passive object thread is running thread is ready Thread is suspended Thread is not running in this object

Thread Concepts: Context Switch A context switch is the simultaneous transitioning of: one thread from the ready state to the running state AND the previously running thread from the running state to another state (ready or suspended).

b : sharedBuffer startWrite() p:Producer c : Consumer startRead() sd scenario n Producer switches out, consumer switches in … Consumer suspends, producer switches in …

Monitors Class-like programming construct that allows at most one thread to execute concurrently Implemented by associating a lock with each monitor object Threads that execute the methods of a monitor object must:  obtain the monitor lock before doing anything else in the method  release the lock just prior to returning.

Monitor Synchronization When a thread attempts to acquire a monitor lock that is held by another thread: the thread that made the failed attempt suspends (i.e., changes state from running to suspended). If the operating system causes a thread running within a monitor to yield (context switch out), the thread will not release the lock before yielding.

: BoundedBuffer: LineConsumer: LineProducer putLine(“d”) locked; unlocked; getLine() sd scenario6 State labels - producer enters and obtains lock Producer releases lock before returning Consumer fails to obtain the lock; suspends

Condition Synchronization Counting variables are used to encode the synchronization state of a shared resource. Conditions are predicates formed over one or more counting variables. Condition variables are associated with conditions and used: by a waiting thread to register interest in a change in the associated condition  CV.wait() by a running thread to inform registered waiting threads of changes in the associated condition.  CV.signal()  CV.broadcast()

Producer-Consumer Example void BoundedLineBuffer::putLine(unsigned id, const string& line){ ACE_Guard guard(lock_); while (isFull()) { // condition: buffer full? cout << "WAIT-ON-FULL: Producer #" << id << endl; fullCond.wait(); // operation on condition variable } buf.push_back(line); cout << "PRODUCE: Producer #" << id << " " << "(buf.size = "<< buf_.size() << ")" << endl; if (buf.size() == 1) { // counting variable: num elements in buffer emptyCond_.broadcast(); }

: BoundedBuffer: LineProducer :LineConsumer getLine() putLine() wait() getLine() broadcast() sd scenario2 okToRead: … locked; size=1; unlocked; Size=0; locked; size=0; locked; size=0; unlocked; size=0; locked; size=1; locked; size=0; unlocked; Size=1; …Locked, then unlocked

Producer Consumer Example void BoundedLineBuffer::getLine(unsigned id, string& line){ ACE_Guard guard(lock_); while (isEmpty()) { // condition: buffer state cout << "WAIT-ON-EMPTY: Consumer #" << id << endl; emptyCond_.wait(); } line = buf_.front(); buf_.pop_front(); cout << "CONSUME: Consumer #" << id << " " << "(buf.size = "<< buf_.size() << ")" << endl; if (buf_.size() == capacity_ - 1) //counting variable = num elements in buffer { fullCond_.broadcast(); // operation on condition variable }

: BoundedBuffer: LineProducer :LineConsumer getLine() putLine() wait() getLine() Broadcast() sd scenario2 okToRead: … locked; size=1; unlocked; Size=0; locked; size=0; locked; size=0; unlocked; size=0; locked; size=1; locked; size=0; unlocked; Size=1; …Locked, then unlocked

Wait on a Condition Variable Waiting on a condition variable causes a thread to: release its hold on the monitor lock change state from running to suspended When a call to wait returns, the calling thread will be back in the monitor will have reacquired the monitor lock

: BoundedBuffer: LineProducer :LineConsumer getLine() putLine() wait() getLine() broadcast() sd scenario2 okToRead: … locked; size=1; unlocked; Size=0; locked; size=0; locked; size=0; unlocked; size=0; locked; size=1; locked; size=0; unlocked; Size=1; …Locked, then unlocked

Signal on a condition variable Signaling a condition variable: changes to ready the state of some thread that is suspended waiting on this variable does not cause signaling thread to release the monitor lock. does not cause the signaling thread to change its state is a necessary but not sufficient condition to cause another thread to return from a call to wait on that variable

: BoundedBuffer: LineProducer :LineConsumer getLine() putLine() wait() getLine() Broadcast() sd scenario2 okToRead: … locked; size=1; unlocked; Size=0; locked; size=0; locked; size=0; unlocked; size=0; locked; size=1; locked; size=0; unlocked; Size=1; …Locked, then unlocked

Condition Synchronization programmed using a loop  guard checks the condition  body executes a wait on condition variable. while (isEmpty()) { // condition: buffer state cout << "WAIT-ON-EMPTY: Consumer #" << id << endl; emptyCond.wait(); // suspended (on wait) -> ready (on signal) -> // running (on context switch) -> re-acquire monitor lock } return from wait indicates that associated condition was true at some point between invocation of wait and return. BUT -- some other thread could have made the condition false before the waiting thread obtains monitor lock SO: the thread must check that the associated condition remains true THUS: it is important to place the wait inside a loop.

: BoundedBuffer: LineProducer :LineConsumer getLine() putLine() wait() getLine() Broadcast() sd scenario2 okToRead: … locked; size=1; unlocked; Size=0; locked; size=0; locked; size=0; unlocked; size=0; locked; size=1; locked; size=0; unlocked; Size=1; …Locked, then unlocked

Overview Programming model used to implement multi-threaded concurrency Threads Monitors Condition synchronization Application of UML 2.0 state diagrams to model and reason about concurrent interactions

Overview Programming model used to implement multi-threaded concurrency Threads Monitors Condition synchronization Application of UML 2.0 state diagrams to model and reason about concurrent interactions

Analytical models of behavior UML sequence diagrams useful for documentation/explanation “roughing out” a design prior to implementation But they are not very rigorous: Depict only one scenario of interaction among objects Not good for reasoning about space of possible behaviors Such reasoning requires more formal and complete models of behavior

UML 2.0 State Models Used to model concurrent designs Abstract away much of the ugliness associated with the multi-threaded programming model Allow reasoning about space of behaviors of an object and of concurrent, interacting objects Key idea: Each object modeled by a communicating sequential process Processes are inherently concurrent with one another  Note: Even a “passive” object is modeled by a process Processes communicate by sending and receiving one- way, asynchronous signals More complex modes of interaction (e.g., rendezvous) built atop the signaling facilities

Key terms Event: instantaneous occurrence at a point in time receipt of an asynchronous signal  e.g., alarm raised, powered on onset of a condition  e.g., paper tray becomes empty execution of some action or effect State: behavioral condition that persists in time waiting for arrival of one or more asynchronous signals and/or the onset of one or more conditions period during which some activity is being performed Transition: instantaneous change in state triggered by an event

State diagrams Graphical state-modeling notation: States: labeled roundtangles Transitions: directed arcs, labeled by signal occurrence, guard condition, and/or effects Example: ST signal(attribs) [guard-condition] / effect States Event Transition

Events run to completion Run-to-completion semantics: State machine processes one event at a time and finishes all consequences of that event before processing another event Events do not interact with one another during processing Pool: Where new incoming signals for an object are stored until object is ready to process them No arrival ordering assumed in the pool

Example C1 C2 C3 / send S.init / send S.seed(100) rand(x) [x > 1] / send S.seed(x/10) S1 S2 init / v :=0 seed(x) / v := x / send C.rand (v+3) CS

Modeling method invocations Given state machines for two objects C and S, where C is the client and S the supplier of a method m Model the call as a signal that requests the operation on behalf of the client Model return as a reply from the supplier to the client C should send the request to S and then await the reply This protocol of interaction is called a rendezvous

UML 2.0 support for rendezvous UML implements rendezvous using: Call activities, performed by the client Accept-call and reply actions, performed by the supplier

Example C1 do/ call S.seed(100) C2 do/ x := call S.rand() Idle S1 do/ v := x / accept-call (seed(x)) reply(seed) CS S2 do/ v := v+3 / accept-call (rand) reply (rand,v)