Transcript
Page 1: Processes and Threads - Home - CECS - ANUcourses.cecs.anu.edu.au/courses/COMP2310/lectures/...resume() yield() Running dispatch s u s p e n d start() stop(), or Also, wait() makes

Processes and Threads

● concurrent processes and their modelling

● modelling of processes in FSP

■ action prefixes

■ (deterministic) choice (of actions)

■ indexed processes and actions

■ guarded actions

■ process (action) alphabets

● implementing processes as threads:

■ OS view

■ Java threads and their life cycle, FSP modelling of life cycle

■ example: the CountDown timer

(these slides including graphics are a modified version of the slides from Ch2 of Magee & Kramer:

Concurrency)

(please log into www.socrative.com for in-class questions)

COMP2310 Lectures 2 & 3: Processes and Threads 2014 ◭◭ ◭ • ◮ ◮◮ × 1

Page 2: Processes and Threads - Home - CECS - ANUcourses.cecs.anu.edu.au/courses/COMP2310/lectures/...resume() yield() Running dispatch s u s p e n d start() stop(), or Also, wait() makes

Concurrent Processes and Their Modelling

● background ideas

■ we structure complex systems as sets of simpler activities,

each represented as a sequential process

■ processes can overlap or be concurrent, in order to

◆ reflect the concurrency inherent in the physical world,

◆ 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!

● overall methodology is the following sequence

■ concept : a process as a sequence of actions;

it represents a unit of sequential execution

■ modelling : model processes as finite state machines

◆ finite state processes to express the sequence of actions

◆ (compiles into) labelled transition systems, to visualize and analyze behavior

■ practice : implement processes as threads in Java

COMP2310 Lectures 2 & 3: Processes and Threads 2014 ◭◭ ◭ • ◮ ◮◮ × 2

Page 3: Processes and Threads - Home - CECS - ANUcourses.cecs.anu.edu.au/courses/COMP2310/lectures/...resume() yield() Running dispatch s u s p e n d start() stop(), or Also, wait() makes

Modeling Processes using LTS and FSP

● a process is the execution of a sequential program

● process models are described using finite state machines,

■ which transits from state to state by executing a sequence of atomic actions

These are known as labelled transition systems (LTS)

■ LTS is the graphical form

■ can be displayed and analyzed by the LTSA analysis tool

● models are described textually as finite state processes FSP)

■ FSP is the algebraic form

a light switch LTS

on −> off −> on −> off −> on a sequence of actions or trace

SWITCH = (on −> off −> SWITCH). FSP specification

COMP2310 Lectures 2 & 3: Processes and Threads 2014 ◭◭ ◭ • ◮ ◮◮ × 3

Page 4: Processes and Threads - Home - CECS - ANUcourses.cecs.anu.edu.au/courses/COMP2310/lectures/...resume() yield() Running dispatch s u s p e n d start() stop(), or Also, wait() makes

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

● e.g. the (terminating) process ONESHOT can be represented by the state machine:

ONESHOT = (on e −> STOP).

● convention: actions begin with lowercase letters, processes begin with

UPPERCASE letters

COMP2310 Lectures 2 & 3: Processes and Threads 2014 ◭◭ ◭ • ◮ ◮◮ × 4

Page 5: Processes and Threads - Home - CECS - ANUcourses.cecs.anu.edu.au/courses/COMP2310/lectures/...resume() yield() Running dispatch s u s p e n d start() stop(), or Also, wait() makes

Action Prefix and Recursion

● repetitive behavior (needed for non-terminating processes) is modelled usingrecursion:

SWITCH = OFF ,

OFF = (on −> ON),

ON = (off−> OFF).

We substitute to get a more succinct definitions:

SWITCH = OFF ,

OFF = (on −> (off −> OFF)).

and again:

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

Note: the inner ()’s can be omitted

● Q1: how many states are in the SWITCH finite state machine?

Q2: how many processes have we defined in SWITCH?

Q3: can finite state models produce infinite traces?

COMP2310 Lectures 2 & 3: Processes and Threads 2014 ◭◭ ◭ • ◮ ◮◮ × 5

Page 6: Processes and Threads - Home - CECS - ANUcourses.cecs.anu.edu.au/courses/COMP2310/lectures/...resume() yield() Running dispatch s u s p e n d start() stop(), or Also, wait() makes

Process Animation using the 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

● observed actions, such as in a trace, can be

referred to as events

COMP2310 Lectures 2 & 3: Processes and Threads 2014 ◭◭ ◭ • ◮ ◮◮ × 6

Page 7: Processes and Threads - Home - CECS - ANUcourses.cecs.anu.edu.au/courses/COMP2310/lectures/...resume() yield() Running dispatch s u s p e n d start() stop(), or Also, wait() makes

FSP Action Prefix: Modelling a Traffic Light

● TRAFFICLIGHT =

(red −> orange −> green −> orange −> TRAFFICLIGHT).

● LTS generated using the LTSA:

● trace:

red −> orange −> green −> orange −> red −> orange −> green ...

COMP2310 Lectures 2 & 3: Processes and Threads 2014 ◭◭ ◭ • ◮ ◮◮ × 7

Page 8: Processes and Threads - Home - CECS - ANUcourses.cecs.anu.edu.au/courses/COMP2310/lectures/...resume() yield() Running dispatch s u s p e n d start() stop(), or Also, wait() makes

(Deterministic) Choice in FSP

● if x and y are actions and P and Q are processes then

■ (x → P | y → Q) is a process which initially engages in either of x or y■ after the first action has occurred, the subsequent behavior is described by P if

it was x and Q otherwise

● example: FSP model of a drinks machine :

DRINKS = ( red −> offee −> DRINKS

| blue −> tea −> DRINKS ).

LTS generated using the LTSA:

Q4: possible traces? red blue off tea, red tea, red off red, blue tea red off

● issues: who or what makes the choice?

■ is there a difference between input and output actions?

COMP2310 Lectures 2 & 3: Processes and Threads 2014 ◭◭ ◭ • ◮ ◮◮ × 8

Page 9: Processes and Threads - Home - CECS - ANUcourses.cecs.anu.edu.au/courses/COMP2310/lectures/...resume() yield() Running dispatch s u s p e n d start() stop(), or Also, wait() makes

FSP - Indexed Processes and Actions

● a single slot buffer that inputs a value in the range 0 to 3 and then outputs that value

BUFF = ( in[i:0..3℄ −> out[i℄ −> BUFF ).

This is equivalent to the 4-way choice:

BUFF = ( in[0℄ −> out[0℄ −> BUFF

| in[1℄ −> out[1℄ −> BUFF

| in[2℄ −> out[2℄ −> BUFF

| in[3℄ −> out[3℄ −> BUFF ).

or using a process parameter with a default value:

BUFF(N=3) = ( in[i:0..N℄ −> out[i℄ −> BUFF ).

● indexed actions in FSP generate labels of the form action.index (the (equivalent)

notation used in LTS diagrams and traces)

● example trace: in.1 −> out.1 −> in.3 −> out.3 −> in.0 −> out.0

COMP2310 Lectures 2 & 3: Processes and Threads 2014 ◭◭ ◭ • ◮ ◮◮ × 9

Page 10: Processes and Threads - Home - CECS - ANUcourses.cecs.anu.edu.au/courses/COMP2310/lectures/...resume() yield() Running dispatch s u s p e n d start() stop(), or Also, wait() makes

FSP - Indexed Processes and Actions (II)

● local indexed process

definitions are

equivalent to process

definitions for each

index value

● index expressions can

be used to model

calculations● example: summing

buffer

onst N = 1

range T = 0..N

range R = 0..2∗N

SUM = ( in[a:T℄[b:T℄ −> TOTAL[a+b℄ ),

TOTAL[s:R℄ = ( out[s℄−>SUM ). //lo al indexed pro ess defn

COMP2310 Lectures 2 & 3: Processes and Threads 2014 ◭◭ ◭ • ◮ ◮◮ × 10

Page 11: Processes and Threads - Home - CECS - ANUcourses.cecs.anu.edu.au/courses/COMP2310/lectures/...resume() yield() Running dispatch s u s p e n d start() stop(), or Also, wait() makes

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 both can be chosen, otherwise x cannot be chosen● example: bounded counter

COUNT (N=3) = COUNT [0℄,

COUNT[i:0..N℄ = ( when (i<N) in −> COUNT[i+1℄

| when (i>0) de −> COUNT[i−1℄ ).

● the above local indexed process definition is a shorthand for:

COUNT [0℄ = ( in −> COUNT [1℄ ),

COUNT [1℄ = ( in −> COUNT [2℄

| de −> COUNT [0℄ ),

COUNT [2℄ = ( in −> COUNT [3℄

| de −> COUNT [1℄ ),

COUNT [3℄ = ( de −> COUNT [2℄ ).

COMP2310 Lectures 2 & 3: Processes and Threads 2014 ◭◭ ◭ • ◮ ◮◮ × 11

Page 12: Processes and Threads - Home - CECS - ANUcourses.cecs.anu.edu.au/courses/COMP2310/lectures/...resume() yield() Running dispatch s u s p e n d start() stop(), or Also, wait() makes

FSP - Guarded Actions: the Countdown Timer

● model a countdown timer which beeps after N ticks, or can be stoppedCOUNTDOWN (N=3) = ( start −> COUNTDOWN[N℄ ),

COUNTDOWN[i:0..N℄ = ( when (i>0) ti k −> COUNTDOWN [i−1℄

| when (i==0) beep −> STOP

| stop −> STOP ).

● Q5: what is the following FSP process equivalent to?

onst False = 0

P = (when (False) go −> P).

(a) P = STOP. (b) P = (go −> STOP). (c) P = (go −> P).

COMP2310 Lectures 2 & 3: Processes and Threads 2014 ◭◭ ◭ • ◮ ◮◮ × 12

Page 13: Processes and Threads - Home - CECS - ANUcourses.cecs.anu.edu.au/courses/COMP2310/lectures/...resume() yield() Running dispatch s u s p e n d start() stop(), or Also, wait() makes

FSP - Process Alphabets

● the alphabet of a process is the set of actions in

which it can engage

● process alphabets are implicitly defined by the

actions in the process definition

● the alphabet of a process can be displayed using the

LTSA alphabet window

Pro ess:

COUNTDOWN

Alphabet:

{ beep ,

start ,

stop ,

ti k

}

● alphabet extension can be used to extend the implicit alphabet of a process● e.g.

WRITER = ( write [1℄ −> write [3℄ −> WRITER )

+ { write [0..3℄}.The alphabet of WRITER is the set {write[0..3℄}

(= {write[0℄, write[1℄, write[2℄, write[3℄})

● we make use of alphabet extensions later.

In the above case, WRITER, when combined with another process, will refuse to

engage in the action write[2℄

COMP2310 Lectures 2 & 3: Processes and Threads 2014 ◭◭ ◭ • ◮ ◮◮ × 13

Page 14: Processes and Threads - Home - CECS - ANUcourses.cecs.anu.edu.au/courses/COMP2310/lectures/...resume() yield() Running dispatch s u s p e n d start() stop(), or Also, wait() makes

Implementing Processes as Threads

● (OS-style) processes: the Operating System view

■ a (heavyweight) process in an operating system is represented by itsdescriptor, code, data and the state of the machine registers

■ to support multiple (lightweight) threads of control, it has multiple stacks, one foreach thread

●modeling processes as finite

state machines using FSP/LTS

⇐implementing

threads in Java

Note: to avoid confusion, we henceforth use the term process when referring to themodels, and thread when referring to the implementation in Java

COMP2310 Lectures 2 & 3: Processes and Threads 2014 ◭◭ ◭ • ◮ ◮◮ × 14

Page 15: Processes and Threads - Home - CECS - ANUcourses.cecs.anu.edu.au/courses/COMP2310/lectures/...resume() yield() Running dispatch s u s p e n d start() stop(), or Also, wait() makes

Threads in Java: the Thread Class

● each object of the Thread class manages a single sequential thread of control

● threads may be created and deleted dynamically

Thread

run()

MyThread

run()

● a Thread object executes instructions from its

run() method

■ a sub-class provides the implementation

for run()

lass MyThread extends Thread {

publi void run() {

//......

}}

● creating a thread object:

Thread a = new MyThread ();

● starting the new thread:

a.start ();

COMP2310 Lectures 2 & 3: Processes and Threads 2014 ◭◭ ◭ • ◮ ◮◮ × 15

Page 16: Processes and Threads - Home - CECS - ANUcourses.cecs.anu.edu.au/courses/COMP2310/lectures/...resume() yield() Running dispatch s u s p e n d start() stop(), or Also, wait() makes

Threads in Java: the Runnable Class

● 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

from the interface Runnable.

Runnable

run()

MyRun

run()

public interface Runnable {

public abstract void run();

}

class MyRun implements Runnable{

public void run() {

//.....}

}

Threadtarget

Creating a thread object: ● creating and starting a thread object:

Thread b = new Thread(new MyRun ());

b.start ();

COMP2310 Lectures 2 & 3: Processes and Threads 2014 ◭◭ ◭ • ◮ ◮◮ × 16

Page 17: Processes and Threads - Home - CECS - ANUcourses.cecs.anu.edu.au/courses/COMP2310/lectures/...resume() yield() Running dispatch s u s p e n d start() stop(), or Also, wait() makes

The Thread Life-cycle in Java

● an overview of the life-cycle of a thread as state transitions:

Created Alive

Terminated

new Thread()

start()

stop(), or

run() returnsstop()

The predicate isAlive() can be

used to test if a thread has been started but

start() causes the thread to call its

run() method.

● the predicate isAlive() can be used to test if a thread has been started but not

terminated.

Once terminated, it cannot be restarted (cf. mortals)

COMP2310 Lectures 2 & 3: Processes and Threads 2014 ◭◭ ◭ • ◮ ◮◮ × 17

Page 18: Processes and Threads - Home - CECS - ANUcourses.cecs.anu.edu.au/courses/COMP2310/lectures/...resume() yield() Running dispatch s u s p e n d start() stop(), or Also, wait() makes

The Thread Alive States in Java

● once started, an alive thread has a number of substates:

Concurrency: processes & threads 27

Runnable Non-Runnablesuspend()

resume()

yield()

Running

dispatch

suspend()

start()

stop(), or

run() returnsAlso, wait() makes a Thread Non-Runnable,

and notify() makes it Runnable

(used in later chapters).

sleep()

Alive

COMP2310 Lectures 2 & 3: Processes and Threads 2014 ◭◭ ◭ • ◮ ◮◮ × 18

Page 19: Processes and Threads - Home - CECS - ANUcourses.cecs.anu.edu.au/courses/COMP2310/lectures/...resume() yield() Running dispatch s u s p e n d start() stop(), or Also, wait() makes

The Java Thread Lifecycle - an FSP SpecificationTHREAD = CREATED ,

CREATED = ( start −> RUNNABLE

| stop −> TERMINATED ),

RUNNABLE = ( suspend −> NON RUNNABLE

| dispat h −> RUNNING

| stop −> TERMINATED ),

RUNNING = ( { suspend , sleep } −> NON RUNNABLE

| yield −> RUNNABLE

| { stop , end } −> TERMINATED

| run −> RUNNING ),

NON RUNNABLE = ( resume −> RUNNABLE

| stop −> TERMINATED ),

TERMINATED = STOP.

Note: {a, b} −> P is a shorthand for ( a −> P | b −> P )

The version in the tool has a small bug; use the corrected Thread.lts version

Q6: in the definition of THREAD, does the order of RUNNABLE and RUNNING matter?

Q7: according to THREAD’s model for Java threads, how many states does a Java thread

have?

COMP2310 Lectures 2 & 3: Processes and Threads 2014 ◭◭ ◭ • ◮ ◮◮ × 19

Page 20: Processes and Threads - Home - CECS - ANUcourses.cecs.anu.edu.au/courses/COMP2310/lectures/...resume() yield() Running dispatch s u s p e n d start() stop(), or Also, wait() makes

The Java Thread Lifecycle - Resulting LTS

● end, run and dispat h are not members of the class Thread

● states 0 to 4 correspond to CREATED, TERMINATED, RUNNABLE, RUNNING, and

NON−RUNNABLE respectively

COMP2310 Lectures 2 & 3: Processes and Threads 2014 ◭◭ ◭ • ◮ ◮◮ × 20

Page 21: Processes and Threads - Home - CECS - ANUcourses.cecs.anu.edu.au/courses/COMP2310/lectures/...resume() yield() Running dispatch s u s p e n d start() stop(), or Also, wait() makes

The CountDown Timer Example● recall the FSP definition:

COUNTDOWN (N=3) = ( start −> COUNTDOWN[N℄ ),

COUNTDOWN[i:0..N℄ = ( when (i>0) ti k −> COUNTDOWN [i−1℄

| when (i==0) beep −> STOP

| stop −> STOP ).

● resulting class diagram:

● the class CountDown derives from Applet;

it contains the implementation of the run() method required by Thread

COMP2310 Lectures 2 & 3: Processes and Threads 2014 ◭◭ ◭ • ◮ ◮◮ × 21

Page 22: Processes and Threads - Home - CECS - ANUcourses.cecs.anu.edu.au/courses/COMP2310/lectures/...resume() yield() Running dispatch s u s p e n d start() stop(), or Also, wait() makes

The CountDown Timer Classpubli lass CountDown extends Applet

implements Runnable {Thread ounter; int i;

final stati int N = 10;

publi void init() {...}

publi void start () {

ounter = new Thread(this);

i = N; ounter.start ();

}

publi void stop() {

ounter = null;

}

publi void run() {

while(true) {

if ( ounter == null) return;

if (i>0) { ti k(); −−i; }

if (i==0) { beep(); return ;}}

}

private void ti k() {...}

private void beep() {...}}

COUNTDOWN model:

start

stop

COUNTDOWN[i=N℄ process (CD[i℄):

recursion as a while loop

when(i>0) ti k −> CD[i−1℄

when(i==0) beep −> STOP

STOP when run() returns

ti k

beep

COMP2310 Lectures 2 & 3: Processes and Threads 2014 ◭◭ ◭ • ◮ ◮◮ × 22

Page 23: Processes and Threads - Home - CECS - ANUcourses.cecs.anu.edu.au/courses/COMP2310/lectures/...resume() yield() Running dispatch s u s p e n d start() stop(), or Also, wait() makes

The CountDown Timer: Execution

Concurrency: processes & threads

©Magee/Kramer

CountDown

counter thread

stop()

new Thread(this)

target.run()

createdcounter.start()

counter=null

alive

terminated

tick()

tick()

CountDown execution

start()init()

● there is a race hazard

here; can get

... −> stop −> beep

● exposed by terminal

version of program

CountDownTerm.java

● how can this happen?

COMP2310 Lectures 2 & 3: Processes and Threads 2014 ◭◭ ◭ • ◮ ◮◮ × 23

Page 24: Processes and Threads - Home - CECS - ANUcourses.cecs.anu.edu.au/courses/COMP2310/lectures/...resume() yield() Running dispatch s u s p e n d start() stop(), or Also, wait() makes

Process and Threads: Review

● concepts:

■ process – unit of concurrency, (as opposed to “the execution of a program”)

● models:

■ LTS to model processes as state machines – sequences of atomic actions

■ FSP to specify processes using prefix (−>), choice ( | ) and recursion

■ Q: what do these correspond to in procedural programming languages?

● practice:

■ Java threads to implement processes

■ thread lifecycle – created, running, runnable, non-runnable, terminated

COMP2310 Lectures 2 & 3: Processes and Threads 2014 ◭◭ ◭ • ◮ ◮◮ × 24


Recommended