Docsity

Concurrency: Modeling Processes and Threads with FSP and LTS, Exams of Software Engineering

Partial preview of the text

Download Concurrency: Modeling Processes and Threads with FSP and LTS and more Exams Software Engineering in PDF only on Docsity!

Concurrency: processes & threads 1

Chapter 2

Processes & Threads

Concurrency: processes & threads 2

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 4

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 5

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

on ‡‡ off ‡‡ on ‡‡ off ‡‡ on ‡‡ off ‡‡ ……….

a sequence of actions or trace

on

off

Concurrency: processes & threads 7

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).

on

off

Concurrency: processes & threads 8

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.

on

off

0 1

Concurrency: processes & threads 10

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 11

FSP - choice

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

LTS generated using LTSA :

Possible traces?

FSP model of a drinks machine :

red

blue

coffee

tea

0 1 2

Concurrency: processes & threads 13

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 ).

in

in

out

0 1

Concurrency: processes & threads 14

Single slot buffer that inputs a value in the range 0 to 3 and then outputs that value:

FSP - indexed processes and actions

BUFF = (in[i:0..3]->out[i]-> BUFF). equivalent to

or using a process parameter with default value:

BUFF = (in[0]->out[0]->BUFF |in[1]->out[1]->BUFF |in[2]->out[2]->BUFF |in[3]->out[3]->BUFF ).

BUFF(N=3) = (in[i:0..N]->out[i]-> BUFF).

Concurrency: processes & threads 16

FSP - guarded actions

The choice (when B x -> P | y -> 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 (N=3) = COUNT[0],

COUNT[i:0..N] = (when(i<N) inc->COUNT[i+1] |when(i>0) dec->COUNT[i-1] ). inc inc

dec

inc

dec dec

0 1 2 3

Concurrency: processes & threads 17

FSP - guarded actions

COUNTDOWN (N=3) = (start->COUNTDOWN[N]), COUNTDOWN[i:0..N] = (when(i>0) tick->COUNTDOWN[i-1] |when(i==0)beep->STOP |stop->STOP ).

A countdown timer which beeps after N ticks, or can be stopped.

start

stop

tick

stop

tick

stop tick beep

stop

0 1 2 3 4 5

Concurrency: processes & threads 19

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:

Alphabet of WRITER is the set {write[0..3]} (we make use of alphabet extensions in later chapters)

WRITER = (write[1]->write[3]->WRITER) +{write[0..3]}.

Concurrency: processes & threads 20

2.2 Implementing processes

Modeling 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.