Concurrency - City University .Do I need to know about concurrent programming ... Concurrency: concurrent

  • View

  • Download

Embed Size (px)

Text of Concurrency - City University .Do I need to know about concurrent programming ... Concurrency:...

  • Concurrency: introduction 1Magee/Kramer

    ConcurrencyState Models and Java Programs

    Jeff Magee and Jeff Kramer

    Concurrency: introduction 2Magee/Kramer

    What is a Concurrent Program?

    A sequential program has a single thread of control.

    A concurrent program has multiple threads of control allowing it perform multiple computations in parallel and to control multiple external activities which occur at the same time.

    Concurrency: introduction 3Magee/Kramer

    Why Concurrent Programming?

    Performance gain from multiprocessing hardware


    Increased application throughput

    an I/O call need only block one thread.

    Increased application responsiveness

    high priority thread for user requests.

    More appropriate structure

    for programs which interact with the environment, control

    multiple activities and handle multiple events.

    Concurrency: introduction 4Magee/Kramer

    Do I need to know about concurrent programming?

    Therac - 25 computerised radiation therapy machine

    Concurrent programming errors contributed to accidents causing deaths and serious injuries.

    Mars Rover

    Problems with interaction between concurrent taskscaused periodic software resets reducing availability forexploration.

    Concurrency is widespread but error prone.

    Concurrency: introduction 5Magee/Kramer

    a Cruise Control System

    Is the system safe?

    Would testing be sufficient to discover all errors?

    When the car ignition is switched on and the onbutton is pressed, the current speed is recorded and the system is enabled: it maintains the speed of the car at the recorded setting.

    Pressing the brake, accelerator or off button disables the system. Pressing resume re-enables the system.


    Concurrency: introduction 6Magee/Kramer


    A model is a simplified representation of the real world.

    Engineers use models to gain confidence in the adequacy and validity of a proposed design.

    focus on an aspect of interest - concurrency

    model animation to visualise a behaviour

    mechanical verification of properties (safety & progress)

    Models are described using state machines, known as Labelled Transition Systems LTS. These are described textually as finite state processes (FSP) and displayed and analysed by the LTSA analysis tool.

  • Concurrency: introduction 7Magee/Kramer

    modeling the Cruise Control System




    0 1

    Later chapters will explain how to construct models such as this so as to perform animation and verification.

    LTS of the process that monitors speed.

    LTSA Animator to step through system actions and events.

    Concurrency: introduction 8Magee/Kramer

    programming practice in Java

    Java is

    widely available, generally accepted and portable

    provides sound set of concurrency features

    Hence Java is used for all the illustrative examples, the demonstrations and the exercises. Later chapters will explain how to construct Java programs such as the Cruise Control System.

    Toy problems are also used as they crystallize particular aspects of concurrent programming problems!

    Concurrency: introduction 9Magee/Kramer

    course objective

    This course is intended to provide a sound understanding of the concepts, models and practiceinvolved in designing concurrent software.

    The emphasis on principles and concepts provides a thorough understanding of both the problems and the solution techniques. Modeling provides insight into concurrent behavior and aids reasoning about particular designs. Concurrent programming in Java provides the programming practice and experience.

    Concurrency: introduction 10Magee/Kramer

    Course Outline

    Processes and Threads

    Concurrent Execution

    Shared Objects & Interference

    Monitors & Condition Synchronization


    Safety and Liveness Properties

    Model-based Design

    Dynamic systems

    Message Passing




    Concurrent Software Architectures

    Timed SystemsConcurrency: introduction 11


    Web based course material

    Java examples and demonstration programs

    State models for the examples

    Labelled Transition System Analyser (LTSA) for modeling concurrency, model animation and model

    property checking.

    Concurrency: introduction 12Magee/Kramer


    Concurrency:State Models &Java Programs

    Jeff Magee &Jeff Kramer


  • Concurrency: introduction 13Magee/Kramer



    we adopt a model-based approach for the design and

    construction of concurrent programs


    we use finite state models to represent concurrent behavior.


    we use Java for constructing concurrent programs.

    Examples are used to illustrate the concepts, models and demonstration programs.

  • Concurrency: processes & threads 1Magee/Kramer

    Chapter 2

    Processes & Threads

    Concurrency: processes & threads 2Magee/Kramer

    concurrent processes

    We structure complex systems as sets of simpler activities, each represented as a sequential process. Processes can overlap or be concurrent, so as to reflect the concurrency inherent in the physical world, or to offload time-consuming tasks, or to manage communications or other devices.

    Designing concurrent software can be complex and error prone. A rigorous engineering approach is essential.

    Model processes as finite state machines.

    Program processes as threads in Java.

    Concept of a process as a sequence of actions.

    Concurrency: processes & threads 3Magee/Kramer

    processes and threads

    Concepts: processes - units of sequential execution.

    Models: finite state processes (FSP)to model processes as sequences of actions.labelled transition systems (LTS)to analyse, display and animate behavior.

    Practice: Java threads

    Concurrency: processes & threads 4Magee/Kramer

    2.1 Modeling Processes

    Models are described using state machines, known as Labelled Transition Systems LTS. These are described textually as finite state processes (FSP) and displayed and analysed by the LTSA analysis tool.

    LTS - graphical form FSP - algebraic form

    Concurrency: processes & threads 5Magee/Kramer

    modeling processes

    A process is the execution of a sequential program. It is modeled as a finite state machine which transits from state to state by executing a sequence of atomic actions.

    a light switch LTS

    onoffonoffonoff .a sequence of actions or trace



    0 1

    Concurrency: processes & threads 6Magee/Kramer

    FSP - action prefix

    If x is an action and P a process then (x-> P)describes a process that initially engages in the action x and then behaves exactly as described by P.

    ONESHOT = (once -> STOP). ONESHOT state machine

    (terminating process)

    Convention: actions begin with lowercase letters

    PROCESSES begin with uppercase letters


    0 1

  • Concurrency: processes & threads 7Magee/Kramer

    FSP - action prefix & recursion

    SWITCH = OFF,OFF = (on -> ON),ON = (off-> OFF).

    Repetitive behaviour uses recursion:

    Substituting to get a more succinct definition:

    SWITCH = OFF,OFF = (on ->(off->OFF)).

    And again:

    SWITCH = (on->off->SWITCH).



    0 1

    Concurrency: processes & threads 8Magee/Kramer

    animation using LTSA

    Ticked actions are eligible for selection.

    In the LTS, the last action is highlighted in red.

    The LTSA animator can be used to produce a trace.



    0 1

    Concurrency: processes & threads 9Magee/Kramer

    FSP - action prefix

    TRAFFICLIGHT = (red->orange->green->orange-> TRAFFICLIGHT).

    LTS generated using LTSA:


    FSP model of a traffic light :


    red orange green


    0 1 2 3

    Concurrency: processes & threads 10Magee/Kramer

    FSP - choice

    If x and y are actions then (x-> P | y-> Q)describes a process which initially engages in either of the actions x or y. After the first action has occurred, the subsequent behavior is described by P if the first action was x and Q if the first action was y.

    Who or what makes the choice?

    Is there a difference between input and output actions?

    Concurrency: processes & threads 11Magee/Kramer

    FSP - choice

    DRINKS = (red->coffee->DRINKS |blue->tea->DRINKS).

    LTS generated using LTSA:

    Possible traces?

    FSP model of a drinks machine :





    0 1 2

    Concurrency: processes & threads 12Magee/Kramer

    Non-deterministic choice

    Process (x-> P | x -> Q) describes a process which engages in x and then behaves as either P or Q.

    COIN = (toss->HEADS|toss->TAILS),HEADS= (heads->COIN),TAILS= (tails->COIN).

    Tossing acoin.





    0 1 2

    Possible traces?

  • Concurrency: processes & threads 13Magee/Kramer

    Modeling failure

    How do we model an unreliable communication channel which accepts in actions and if a failure occurs produces no output, otherwise performs an out action?

    Use non-determinism...

    CHAN = (in->CHAN|in->out->CHAN).




    0 1