COMP31212: Concurrency and Process...
Transcript of COMP31212: Concurrency and Process...
Topic 1: Introduction Topic 2: Modelling Processes with FSP - I
COMP31212: Concurrency and ProcessAlgebra
Introduction to the Course and to FSP
David Rydeheard
Room KB2.111: email: [email protected]
Topic 1: Introduction Topic 2: Modelling Processes with FSP - I
Acknowledgements
This course owes much to previous versions prepared by AlanWilliams and Howard Barringer.Others involved in early versions were Donal Fellows and PeterAczel.
Topic 1: Introduction Topic 2: Modelling Processes with FSP - I
Course materialsThis course is based on the following textbook:
Concurrency: State Models and Java ProgramsJeff Magee and Jeff Kramer, (2nd Edition), Wiley 2006.
We will follow both the presentation of topics and also theexercises in this book.The authors have produced an LTSA (Labelled Transition SystemsAnalysis) tool to allow us to animate models of concurrent systemsand test models for various properties. The tool is available fromthe course website or from the authors’ website.If you take this course, you will need (1) to have this bookavailable and (2) to download and use the LTSA tool.The course website is packed with supporting materials: slides,exercises, answers, notes, links, examples, etc. Look at it and useit!
Topic 1: Introduction Topic 2: Modelling Processes with FSP - I
Contents
Topic 1: IntroductionGeneral BackgroundOn ConcurrencyExamplesImplementation
Topic 2: Modelling Processes with FSP - IProcess AlgebrasLabelled Transition SystemsFSP: Basic ElementsSummary
Topic 1: Introduction Topic 2: Modelling Processes with FSP - I
Course Topics
1. Introduction and Overview
2. Process algebras: FSP and Modelling Processes
3. Properties: Safety, Liveness, Deadlock, Livelock and Fairness
4. Process Equivalence
5. Java Threads - implementing concurrent processes
6. Concurrency Patterns:• Mutual Exclusion• Monitors+Semaphores• Producers/Consumers• Readers/Writers• GUIs• Termination
7. Revision
Topic 1: Introduction Topic 2: Modelling Processes with FSP - I
General Comments
• Some concepts familiar from previous courses on distributedsystem and on operating systems,
• We follow much of Magee and Kramer, including theirmodelling approach through the process algebra, FSP,
• Java used to illustrate and implement — but this is not aprogramming course
Topic 1: Introduction Topic 2: Modelling Processes with FSP - I
Supporting and Background Material
Books• Jeff Magee and Jeff Kramer. Concurrency: State Models and
Java Programs. 2nd Edition, Wiley, 2006.• L. Aceto, A Ingolfsdottir, K. Larsen and J Srba. Reactive
Systems: Modelling, Specification and Verification. CambridgeUniversity Press, 2007.
• R. Milner. Communication and Concurrency. Prentice-Hall,1989.
• C.A.R. Hoare. Communicating Sequential Processes.Prentice-Hall, 1985.
LTSA: Magee and Kramer’s modelling and analysis tool,
Exercises: offline and in lectures,
Lecture Slides: in lectures and on course website,
Notes on FSP.
All exercises, notes, lecture slides, and other material are availablefrom the course website (accessed from the syllabus page).
Topic 1: Introduction Topic 2: Modelling Processes with FSP - I
Assessment
Two-hour examination (answer 3 questions from 5). See previousyears’ examinations for sample questions.
Topic 1: Introduction Topic 2: Modelling Processes with FSP - I
What is Concurrency?
Simple account: A set of sequential programs (processes) executed“in parallel”.
The notion of “parallel” needs careful analysis, as does the waythat the programs communicate and/or share information. Someissues:
• threads of control
• multi-threading
• parallel processing
• multi-processing
• multi-tasking
• shared memory
• message-passing: point-to-point or broadcast, synchronous orasynchronous
Topic 1: Introduction Topic 2: Modelling Processes with FSP - I
What is Concurrency?
Key concepts:
• ‘Atomicity of actions’ and interference,
• Non-determinism,
• Hierarchical structure of concurrent systems?
• Difficulty of reasoning about concurrency, rules for reasoningand building correct systems.
Topic 1: Introduction Topic 2: Modelling Processes with FSP - I
Why use Concurrency?
• often a natural concept when systems consist of more thanone process
• computationally, sequencing is often over specified, andconcurrency is natural, e.g. in the development of algorithms
• performance issues:• increased speed of performance (e.g. using multiprocessor and
multicore machines)• functionally: increased responsiveness and throughput
Topic 1: Introduction Topic 2: Modelling Processes with FSP - I
Why is concurrency hard?
• development of concurrent algorithms: problems, includingdifficulties with abstraction
• efficiency and performance
• in general, behaviour is non-deterministic: consequences fortesting and simulation
• correctness:• multiple threads to follow but their interleaving - hence overall
behaviour - is undetermined,• there are properties not present in sequential systems e.g.
deadlock, livelock, fairness, liveness, etc.
Topic 1: Introduction Topic 2: Modelling Processes with FSP - I
Example: Mutual Exclusion
Design a basic control protocol to ensure two processes neverexecute some “critical” region of program together.
Is it OK? At first it looks like a possible solution - but it candeadlock (actually livelock).
Topic 1: Introduction Topic 2: Modelling Processes with FSP - I
Example: Mutual Exclusion - continued
This example indicates many of the problems in developing andreasoning about concurrent processes:
• the large number of possible traces of execution that mayarise through the interleaving of actions, and consideringwhether we have analysed sufficient to establish correctness,
• behaviours not present in sequential programming, such asdeadlock and livelock
• restricting access to resources and interference,
• fairness of interaction, etc.
Topic 1: Introduction Topic 2: Modelling Processes with FSP - I
Example: The Firing Squad Synchronisation Problem
• On the command “FIRE”, the chain of control units mustmutually synchronise to fire each gun simultaneously.
• The control units must be identical and work for any sizechain of artillery.
How to do it?
Topic 1: Introduction Topic 2: Modelling Processes with FSP - I
Example: The Firing Squad Synchronisation Problem -continued
This example illustrates further aspects that arise in the descriptionand analysis of concurrent processes:
• this is an example of interaction via message passing (theprevious example had interaction via shared memory),
• the problem is one of synchronization, which is a special caseof consensus amongst processes.
Topic 1: Introduction Topic 2: Modelling Processes with FSP - I
Modelling Concurrency
We consider how to describe concurrent processes using a processalgebra called FSP:
• representating the real world at a suitable level of abstraction?
• modelling before implementing
• model captures interesting aspects: concurrency
• animation
• analysis
• Model Description Language: FSP (Finite State Processes)
• Models: LTS (Labelled Transition Systems)
Topic 1: Introduction Topic 2: Modelling Processes with FSP - I
Example: Cruise Control System
• Does it do what we expect? Is it safe? How do we model andimplement the required bahaviour?
Topic 1: Introduction Topic 2: Modelling Processes with FSP - I
FSP: Animation
INPUTSPEED = ( engineOn -> CHECKSPEED ),CHECKSPEED = ( speed -> CHECKSPEED
| engineOff -> INPUTSPEED ).
Topic 1: Introduction Topic 2: Modelling Processes with FSP - I
Structure Diagrams
SENSORSCAN
CRUISECONTROLLER
SPEED CONTROL
INPUTSPEED THROTTLE
CONTROL
speed
Sensors
setThrottle
EnginePrompts
set Sensors = {engineOn,engineOff,on,off,
resume, brake, accelerator}
set Engine = {engineOn,engineOff}
set Prompts = {clearSpeed,recordSpeed,
enableControl,disableControl}
Topic 1: Introduction Topic 2: Modelling Processes with FSP - I
Implementation in Java
• Thread class; Runnable interface
• starting, stopping, suspending threads
• mutual exclusion: synchronized methods and code blocks
• monitors, condition synchronization
• wait, notify, notifyAll
• sleep, interrupt
• suspend, resume, stop
• properties: safety, liveness
Topic 1: Introduction Topic 2: Modelling Processes with FSP - I
Process algebras
A process algebra is a notation and calculus for describingconcurrent systems, based on:
• two types of elements, processes and actions, and theconstruct a.P (otherwise written as a → P) meaning performaction a and then become process P,
• a variety of process constructors, including || for the parallelcomposition of processes,
• processes are defined by (recursive) equations,
• evaluation is via rewriting of expressions using rules,
• synchronisation and communication is via shared actions.
Can consider process definitions as specifications, as runnableprototypes, or as models.
Topic 1: Introduction Topic 2: Modelling Processes with FSP - I
Pioneers
Robin Milner (CCS and the pi-calculus) Sir Tony Hoare (CSP)
There are several varieties of process algebra, depending on howcommunication is handled, whether data is included, exactly whatprocess constructors are incorporated, etc.
Topic 1: Introduction Topic 2: Modelling Processes with FSP - I
Labelled Transition Systems
What is an LTS?:
LTS = (S ,A, σ, s0)
where
S : set of states SA : alphabet A ⊆ Actσ : transition relation σ ⊆ (S × A× S)s0 : initial state s0 ∈ S
Act is the set of transition labels or (as we shall call them)‘actions’. We model concurrency so that these are atomic eventswhich cannot be interrupted or interferred with, and so are thelowest level of granularity.
Topic 1: Introduction Topic 2: Modelling Processes with FSP - I
First Exercise
DAY1: A Day In the Life Of:
• Get up — action: up,
• then have a cup of tea — action: tea
• then work — action: work
F LTS for DAY1? F
• DAY2: Now repeat the Day
F LTS for DAY2? F
• DAY3: Now be able to choose coffee — action: coffee —instead of tea
F LTS for DAY3? F
Topic 1: Introduction Topic 2: Modelling Processes with FSP - I
FSP: A Textual Representation for LTS
FSP - Finite State ProcessesWhat FSP constructs Aae required?
• sequencing of actions
• STOP
• process definition, with recursion
• choice
Later, we consider the concurrent/parallel running of processes asa program construct.
Topic 1: Introduction Topic 2: Modelling Processes with FSP - I
Action Prefix
If x is an action and P is a process then (x->P) describes aprocess that initially engages in the action x and then behavesexactly as described by P.
(once->STOP).
Convention:
• actions begin with a lower case letter
• PROCESS NAMES begin with an upper case letter
• STOP is a specially pre-defined FSP process name.
F FSP for DAY1? F
Topic 1: Introduction Topic 2: Modelling Processes with FSP - I
Process Definition
Basic form:
ProcId = process expression
The meaning of ProcId (‘process identifier’) will be given by themeaning of process expression.ProcId should start with an upper-case letter.
(more complex forms possible — see later. . . )F FSP for DAY2? F
Topic 1: Introduction Topic 2: Modelling Processes with FSP - I
Choice
• If x and y are actions then (x->P | y->Q) describes a processwhich initially engages in either of the actionsx or y.
• After that the subsequent behaviour is described by• P if the first action was x,• Q if the first action was y.
F FSP for DAY3? F
Topic 1: Introduction Topic 2: Modelling Processes with FSP - I
Example: Various Switches
Repetitive behaviour is via recursion:SWITCH = OFF,OFF = (on->ON),ON = (off->OFF).
Substituting to get a more concise definition:SWITCH = OFF,OFF = (on->off->OFF).
And again:SWITCH = (on->off->SWITCH).
F Are these FSP SWITCH definitions the same? F
Topic 1: Introduction Topic 2: Modelling Processes with FSP - I
Example: Traffic Light
FSP model of a traffic light (USA sequence!):
TRAFFICLIGHT = (red->amber->green->amber->TRAFFICLIGHT).
F LTS generated using LTSA? F
F Trace? F
red->amber->green->amber->red ->amber->green-> · · ·
Topic 1: Introduction Topic 2: Modelling Processes with FSP - I
Example: Vending Machine
FSP model of a drinks machine:
DRINKS = (red->coffee->DRINKS| blue->tea->DRINKS).
F LTS generated using LTSA? F
F Possible traces? F
Topic 1: Introduction Topic 2: Modelling Processes with FSP - I
Non-Deterministic Choice
Process (x->P | x->Q) describes a process which engages in x andthen behaves as either P or Q.
COIN = (toss->HEADS | toss->TAILS),HEADS = (heads->COIN),TAILS = (tails->COIN).
Tossing a coin
F Possible traces? F
Topic 1: Introduction Topic 2: Modelling Processes with FSP - I
Indexed Processes and Actions
Single slot buffer that inputs a value in the range 0 to 3 and thenoutputs a value:
BUFF = (in[i : 0..3]->out[i]->BUFF).
equivalent to
BUFF = (in[0]->out[0]->BUFF| in[1]->out[1]->BUFF| in[2]->out[2]->BUFF| in[3]->out[3]->BUFF).
Topic 1: Introduction Topic 2: Modelling Processes with FSP - I
or using a constant and indexed process BUFF[i]:
const N = 3BUFF = (in[i : 0..N]->BUFF[i]),BUFF[i : 0..N] = (out[i]->BUFF).
or using a process parameter with default value:
BUFF(N = 3) = (in[i : 0..N]->out[i]->BUFF).
Topic 1: Introduction Topic 2: Modelling Processes with FSP - I
Guarded Actions
The choice (when B x->P | y->Q) describes a process that is like(x->P | y->Q) except that the action x can only be chosen whenthe guard B is true.
Topic 1: Introduction Topic 2: Modelling Processes with FSP - I
Example: A Counter
COUNT(N = 3) = COUNT[0],COUNT[i : 0..N] = (when (i < N) inc->COUNT[i + 1]
|when (i > 0) dec->COUNT[i− 1]).
Topic 1: Introduction Topic 2: Modelling Processes with FSP - I
Example: A Countdown Timer
A countdown timer which beeps after N ticks, or can be stopped.
COUNTDOWN(N = 3) = (start->COUNTDOWN[N]),COUNTDOWN[i : 0..N] = (when (i > 0) tick
->COUNTDOWN[i− 1]|when (i == 0) beep->STOP|stop->STOP).
F LTS? F
Topic 1: Introduction Topic 2: Modelling Processes with FSP - I
Example: What is this?
F What is the following FSP process equivalent to? F
const False = 0
P = (when (False) doanything -> P).
Topic 1: Introduction Topic 2: Modelling Processes with FSP - I
Constant and Range Declarations
Index expressions to model a calculation:
const N = 1range T = 0..Nrange R = 0..2*N
SUM = (in[a:T][b:T] -> TOTAL[a+b]),TOTAL[s:R] = (out[s] -> SUM).
F Write SUM using basic FSP? F
Topic 1: Introduction Topic 2: Modelling Processes with FSP - I
Process Alphabets
• The alphabet of a process is the set of actions in which it isallowed to engage.
• This is usually determined implicitly as the actions in which itcan engage.
• But the implicit alphabet can be extended:
WRITER = (write[1]->write[3]->WRITER)+{write[0..3]}.
The alphabet of WRITER is the set {write[0..3]}; i.e. the set{write[0],write[1],write[2],write[3]}.
Topic 1: Introduction Topic 2: Modelling Processes with FSP - I
FSP: Summary
Forms of process expression Examples
prefix action (coffee->DRINKS)guarded action (when (i == 0) beep->STOP)deterministic choice (red->COFFEE | blue->TEA)non-deterministic choice (toss->HEADS | toss->TAILS)dependent process (out[i]->BUFF)indexed choice (in[i : 0..3]->BUFF[i])
process name
{DRINKS,BUFF[i]
Topic 1: Introduction Topic 2: Modelling Processes with FSP - I
Process equation: process name = process expression
Process definition:
declarationsmain process equation,local process equation,
.
.
.local process equation,local process equation.