Book Chapter 1 - PowerPoint PPT Presentation

1 / 51
About This Presentation
Title:

Book Chapter 1

Description:

Compile-time verification of safety properties. Concurrency Magee/Kramer. February ... The alphabet of a process is the set of actions in which it can engage. ... – PowerPoint PPT presentation

Number of Views:56
Avg rating:3.0/5.0
Slides: 52
Provided by: jeffk167
Category:
Tags: book | chapter | compile

less

Transcript and Presenter's Notes

Title: Book Chapter 1


1
Concurrency
Spring 2007
Claus Brabrand brabrand_at_daimi.au.dk University of
Aarhus
2
Concurrency State Models and Java Programs
Jeff Magee and Jeff Kramer (adapted by Claus
Brabrand)
3
Models
  • Model simplified representation of the real
    world
  • Focuses on concurrency aspects (of the program)
  • everything else abstracted away
  • Based on Labelled Transition Systems (LTS )
  • Described textually as Finite State Processes
    (FSP )

Aka. Finite State Machine (FSM )
SWITCH OFF, OFF (on -gt ON), ON
(off-gt OFF).
4
Models (Contd)
  • Model simplified representation of the real
    world
  • Analysed/Displayed by LTS Analyser (LTSA )
  • Runtime testing (animation) of model to visualise
    behaviour
  • Compile-time verification of safety properties

Engineers use models to gain confidence in the
adequacy and validity of a proposed design
5
Testing vs. Verification
Cruise control system
  • What does
  • safety / testing / verification
  • ... mean? (e.g., for the cruise controller)

6
Programming Practice
  • Java
  • widely available, generally accepted, and
    portable
  • provides good concurrency abstractions
  • Thus, we shall use Java for all
  • Illustrative examples, demonstrations, and
    exercises

Toy problems crystallize concurrency
programming issues and problems!
7
Course Objective
This course is divided into concepts, models and
practice involved in designing concurrent
software.
  • Concepts
  • Conceptual ideas behind concurrency problems and
    solution techniques
  • Models
  • Provide insight into concurrent behaviour and aid
    reasoning about particular designs
  • Practice
  • Programming practice and experience

8
Course Outline
  • Processes and Threads
  • Concurrent Execution
  • Shared Objects Interference
  • Monitors Condition Synchronization
  • Deadlock
  • Safety and Liveness Properties
  • Model-based Design

Concepts
Models
Practice
9
Web-Based Course Material
http//www.doc.ic.ac.uk/jnm/book/
  • Java examples and demonstration programs
  • LTS models for the examples
  • Labelled Transition System Analyser (LTSA) for
    visualisation, animation, and verification

10
Summary
  • Concepts
  • We adopt a model-based approach for the design
    and construction of concurrent programs
  • Models
  • We use finite state models to represent
    concurrent behaviour
  • Practice
  • We use Java for constructing concurrent programs

Examples are used to illustrate the concepts,
models, and demonstration programs
11
Chapter 2
Processes Threads
12
Concurrent Processes
Concept process sequences of actions
We structure complex systems as sets of simpler
activities, each represented as a (sequential)
process Processes can be concurrent Designing
concurrent software - complex and error
prone We need rigorous engineering approach!
Model process Finite State Processes (FSP)
Practice process Java thread
13
Processes and Threads
Concepts A Process a unit of sequential
execution Models Finite State Processes (FSP)
to model a process as
sequences of actions Labelled Transition
Systems (LTS) to analyse, display, and animate
behaviour Practice Java thread
14
2.1 Modelling Processes
Models are described using state machines, known
as Labelled Transition Systems (LTS) These are
described textually as Finite State Processes
(FSP) Analysed/Displayed by the LTS Analyser
(LTSA)
SWITCH OFF, OFF (on -gt ON), ON
(off-gt OFF).
  • FSP - algebraic form
  • LTS - graphical form

15
FSP - STOP
STOP is the inactive process, doing absolutely
nothing.
FSP
INACTIVE STOP.
LTS
INACTIVE state machine (terminating process)
Note that it offers the prototypical behavior of
a deadlocked process (that cannot proceed any
further in its execution)
16
FSP - action prefix
If x is an action and P a process then (x-gt P)
describes a process that initially engages in the
action x and then behaves exactly as described by
P.
FSP
MATCH (strike -gt STOP).
LTS
Convention actions begin with lowercase
letters PROCESSES begin with uppercase
letters
17
Action Prefix (contd)
If x is an action and P a process then (x-gt P)
describes a process that initially engages in the
action x and then behaves exactly as described by
P.
FSP
COMPLEX_MATCH (take -gt strike -gt STOP).
LTS
18
Action Prefix (contd)
If x is an action and P a process then (x-gt P)
describes a process that initially engages in the
action x and then behaves exactly as described by
P.
FSP
DISPENSER (coin -gt coke -gt STOP).
LTS
  • Input vs. Output actions ?
  • From Dispensers perspective ?
  • From Environments perspective ?

19
Modeling Processes
A process is modelled by a sequential
program. It is modelled as a finite state
machine which transits from state to state by
executing a sequence of atomic actions.
a light switch LTS
a sequence of actions or trace
on?off?on?off?on?off? .
20
FSP - action prefix recursion
Repetitive behaviour uses recursion
SWITCH OFF, OFF (on -gt ON), ON
(off-gt OFF).
Substituting to get a more succinct definition
SWITCH OFF, OFF (on -gt(off-gtOFF)).
Again?
SWITCH (on-gtoff-gtSWITCH).
21
Animation using LTSA
The LTSA animator can be used to produce a trace.
Ticked actions are eligible for selection. In the
LTS, the last action is highlighted in red.
22
FSP - action prefix
FSP model of a traffic light
TRAFFICLIGHT (red-gtorange-gtgreen-gtorange
-gt TRAFFICLIGHT).
LTS?
Trace(s)?
red?orange?green?orange?red?orange?green
What would the LTS look like for?
T (red-gtorange-gtgreen-gtorange-gtSTOP).
23
FSP - choice
If x and y are actions then (x-gt P y-gt 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?
24
FSP - choice
FSP model of a drinks machine
DRINKS (red-gtcoffee-gtDRINKS
blue-gttea-gtDRINKS ).
LTS generated using LTSA
Possible traces?
25
Non-deterministic choice
Process (x -gt P x -gt Q) describes a process
which engages in x and then non-deterministically
behaves as either P or Q.
COIN (toss-gtHEADStoss-gtTAILS), HEADS
(heads-gtCOIN), TAILS (tails-gtCOIN).
Tossing a coin.
LTS?
Possible traces?
26
Example Modelling unreliable communication
channel
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-gtCHAN in-gtout-gtCHAN ).
27
FSP - indexed processes and actions
Single slot buffer that inputs a value in the
range 0 to 3 and then outputs that value
BUFF (ini0..3-gtouti-gt BUFF).
Def/Use (as in programming languages)
Could we have made this process w/o using the
indices?
BUFF (in_0-gtout_0-gtBUFF
in_1-gtout_1-gtBUFF in_2-gtout_2-gtBUFF
in_3-gtout_3-gtBUFF ).
BUFF (in0-gtout0-gtBUFF
in1-gtout1-gtBUFF in2-gtout2-gtBUFF
in3-gtout3-gtBUFF ).
...or...
28
Indices (contd)
BUFF (ini0..3-gtouti-gt BUFF). or
BUFF (in0-gtout0-gtBUFF
in1-gtout1-gtBUFF in2-gtout2-gtBUFF
in3-gtout3-gtBUFF).
LTS?
29
FSP - indexed processes and actions (contd)
BUFF (ini0..3-gtouti-gt BUFF).
equivalent to
BUFF (ini0..3-gtOUTi), OUTi0..3
(outi-gtBUFF).
equivalent to
BUFF (ini0..3-gtOUTi), OUTj0..3
(outj-gtBUFF).
30
FSP - constant addition
index expressions to model calculation
const N 1 SUM (ina0..Nb0..N-gtT
OTALab), TOTALs0..2N (outs-gtSUM).
31
FSP - constant range declaration
index expressions to model calculation
const N 1 range T 0..N range R 0..2N SUM
(inaTbT-gtTOTALab), TOTALsR
(outs-gtSUM).
32
FSP - guarded actions
The choice (when B x -gt P y -gt Q) means that
when the guard B is true then the actions x and y
are both eligible to be chosen, otherwise if B is
false then the action x cannot be chosen.
COUNT (N3) COUNT0, COUNTi0..N
(when(iltN) inc-gtCOUNTi1
when(igt0) dec-gtCOUNTi-1 ).
LTS?
Could we have made this process w/o using the
guards?
33
FSP - guarded actions
A countdown timer which beeps after N ticks, or
can be stopped.
COUNTDOWN (N3) (start-gtCOUNTDOWNN), COUNTDO
WNi0..N (when(igt0) tick-gtCOUNTDOWNi-1
when(i0)beep-gtSTOP stop-gtSTOP ).
34
FSP - guarded actions
What is the following FSP process equivalent to?
const False 0 P (when (False)
do_anything-gtP).
35
FSP - process alphabets
The alphabet of a process is the set of actions
in which it can engage.
Alphabet extension can be used to extend the
implicit alphabet of a process
WRITER (write1-gtwrite3-gtWRITER)
write0..3.
Alphabet of WRITER is the set write0..3 (we
make use of alphabet extensions in later chapters)
36
Practice
  • Threads in Java

37
2.2 Implementing processes
Modelling processes as finite state machines
using FSP/LTS.
Implementing threads in Java.
Note to avoid confusion, we use the term process
when referring to the models, and thread when
referring to the implementation in Java.
38
One Process
  • 1 Process
  • Data The heap (global, heap allocated data)
  • Code The program (bytecode)
  • Stack The stack (local data, call stack)
  • Descriptor program counter, stack pointer,

data
code
descriptor
stack
39
Implementing processes - the OS view
A multi-threaded process
data
code
descriptor
descr.
descr.
descr.
. . . . . .
stack
stack
stack
Thread 1
Thread 2
Thread n
A (heavyweight) process in an operating system is
represented by its code, data and the state of
the machine registers, given in a descriptor. In
order to support multiple (lightweight) threads
of control, it has multiple stacks, one for each
thread.
40
Threads in Java
A Thread class manages a single sequential thread
of control. Threads may be created and deleted
dynamically.
The Thread class executes instructions from its
method run(). The actual code executed depends on
the implementation provided for run() in a
derived class.
class MyThread extends Thread public void
run() //......
Thread x new MyThread()
41
Threads in Java (contd)
Since Java does not permit multiple inheritance,
we often implement the run() method in a class
not derived from Thread but from the interface
Runnable.
public interface Runnable public abstract
void run() class MyRun implements Runnable
public void run() //......
Thread x new Thread(new MyRun())
42
Thread Life-Cycle
  • Java Thread Life-Cycle
  • sleep(kmsec) wakeup
  • wait() notify()
  • I/O block unblock
  • suspend() resume()
  • start()

Running
schedule yield()
  • stop()
  • destroy()
  • run() terminates

43
Example Countdown timer
  • Model lt-gt Impl.

44
CountDown timer example
const N 3 COUNTDOWN (start-gtCOUNTDOWNN), COU
NTDOWNi0..N (when(igt0)
tick-gtCOUNTDOWNi-1 when(i0)
beep-gtSTOP stop-gtSTOP ).
Implementation in Java?
45
CountDown class
public class CountDown implements Runnable
Thread counter int i final static int N
3 public void run() ...
public void start() ... public void
stop() ... protected void tick()
... protected void beep() ...
46
CountDown class - start(), stop() and run()
COUNTDOWN Model
public void start() counter new
Thread(this) i N counter.start()
public void stop() counter null
public void run() while(true) if
(igt0) tick() --i if (i0) beep()
return if (counter null) return

start -gt CountDownN
stop -gt STOP
COUNTDOWNi process recursion as a while loop
when(igt0) tick -gt CDi-1 when(i0)beep -gt
STOP stop-gtSTOP STOP run() terminates
47
CountDown class the output actions tick() and
beep()
protected void tick() ltltemit tick
soundgtgt try
Thread.sleep(1000) catch(InterruptedExc
eption iex) // ignore (in this
toy-example) protected void
beep() ltltemit beep soundgtgt
48
Summary
  • Concepts
  • process - unit of concurrency, execution of a
    program
  • Models
  • LTS to model processes as state machines -
    sequences of atomic actions
  • FSP to specify processes using prefix -gt,
    choice and recursion
  • Practice
  • Java threads to implement processes
  • Thread lifecycle(created, running, runnable,
    non-runnable, terminated)

49
Process Exercises (FSP)
  • MK 2.1
  • Simple introductory FSP exercises
  • MK 2.2
  • Variable ) read reads the last value
    written.
  • MK 2.3
  • Bistable
  • MK 2.4
  • Sensor
  • Thread Life-Cycle (as an FSP process)

50
FSP Exercise MK 2.5
  • This weeks hand-in
  • SUGAROLA

A drinks dispensing machine charges 15p for a can
of Sugarola. The machine accepts coins with
denominations 5p, 10p, and 20p and gives change.
Model the machine as an FSP process, DRINKS.
51
The End
  • Questions?
  • See you next Monday..
Write a Comment
User Comments (0)
About PowerShow.com