28
A Tool Suite for Concurrent Programming Marian V. Iordache School of Engineering and Eng. Tech. LeTourneau University October 21, 2011 CS Seminar LeTourneau University A Tool Suite for Concurrent Programming 1

A Tool Suite for Concurrent Programmingmviordache.name/acts/actscp.pdf · 2017. 8. 20. · Petri nets model concurrent systems. Petri net models have been used in: distributed algorithms,

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: A Tool Suite for Concurrent Programmingmviordache.name/acts/actscp.pdf · 2017. 8. 20. · Petri nets model concurrent systems. Petri net models have been used in: distributed algorithms,

A Tool Suite for Concurrent Programming

Marian V. Iordache

School of Engineering and Eng. Tech.

LeTourneau University

October 21, 2011

CS Seminar

LeTourneau University

A Tool Suite for Concurrent Programming 1

Page 2: A Tool Suite for Concurrent Programmingmviordache.name/acts/actscp.pdf · 2017. 8. 20. · Petri nets model concurrent systems. Petri net models have been used in: distributed algorithms,

... Blessed be the name of God for ever and ever: for wisdom and mightare his: ... he giveth wisdom unto the wise, and knowledge to them thatknow understanding (Daniel 2:20-21).

Then answered Jesus and said unto them, Verily, verily, I say unto you, TheSon can do nothing of himself, but what he seeth the Father do: for whatthings soever he doeth, these also doeth the Son likewise (John 5:19).

All things were made by him; and without him was not any thing made thatwas made (John 1:3).

And he is before all things, and by him all things consist (Colossians 1:17).

... Christ came, who is over all, God blessed for ever. Amen (Romans 9:5).

A Tool Suite for Concurrent Programming 2

Page 3: A Tool Suite for Concurrent Programmingmviordache.name/acts/actscp.pdf · 2017. 8. 20. · Petri nets model concurrent systems. Petri net models have been used in: distributed algorithms,

Motivation

• Concurrent programming is difficult because of shared resources.

• These can cause race conditions.

• Race conditions denote the situations in which the result depends on theorder of execution of concurrent operations.

A Tool Suite for Concurrent Programming 3

Page 4: A Tool Suite for Concurrent Programmingmviordache.name/acts/actscp.pdf · 2017. 8. 20. · Petri nets model concurrent systems. Petri net models have been used in: distributed algorithms,

Motivation Race Conditions

PROCESS #1

...

(1) u = x

(2) u = u + 10

(3) x = u...

PROCESS #2

...

(a) v = x

(b) v = v + 20

(c) x = v...

• Initial value of shared variable x is x = 30.

• Correct result: x = 60.

• Possible results:

◦ x = 40 for operation sequence (1), (a), (2), (b), (c), (3).

◦ x = 50 for operation sequence (1), (a), (2), (3), (b), (c).

◦ x = 60 for operation sequence (1), (2), (3), (a), (b), (c).

A Tool Suite for Concurrent Programming 4

Page 5: A Tool Suite for Concurrent Programmingmviordache.name/acts/actscp.pdf · 2017. 8. 20. · Petri nets model concurrent systems. Petri net models have been used in: distributed algorithms,

Motivation Problems

• Locks can be used to ensure that critical operation sequences are notinterrupted.

– Generally, it is difficult to find the best way to use locks in a program.– Improper use of locks may result in∗ deadlocks;∗ sequential execution of parallel code.

• Concurrent programs are hard to debug.

A Tool Suite for Concurrent Programming 5

Page 6: A Tool Suite for Concurrent Programmingmviordache.name/acts/actscp.pdf · 2017. 8. 20. · Petri nets model concurrent systems. Petri net models have been used in: distributed algorithms,

A Concurrency Tool Suite

A Concurrency Tool Suite (ACTS): software that generates automaticallythe concurrency control code.

Based on an input specification, the software generates files containing user code, embedded

concurrency control code, and supervision code.

The output files are written in C.

Compared to other software for concurrent programming:

• Permits general interprocess synchronizations.

• Permits concurrency constraints that can be described in terms of linear inequalities

(such as generalized mutual exclusion and fairness constraints).

• Provides a deadlock prevention tool. If the specification allows reaching deadlock

states, this tool can be used to prevent deadlock.

• ...

• It is free open-source software.

A Tool Suite for Concurrent Programming 6

Page 7: A Tool Suite for Concurrent Programmingmviordache.name/acts/actscp.pdf · 2017. 8. 20. · Petri nets model concurrent systems. Petri net models have been used in: distributed algorithms,

Specifications

The specification describes the concurrent entities as state machines.

The specification is in terms of sets of concurrent entities.

A set may have any number of elements (zero included). This number isnot fixed and may change during the execution of the generated program.

User code is associated with each state of a state machine.

A Tool Suite for Concurrent Programming 7

Page 8: A Tool Suite for Concurrent Programmingmviordache.name/acts/actscp.pdf · 2017. 8. 20. · Petri nets model concurrent systems. Petri net models have been used in: distributed algorithms,

Specifications Defining Processes/Threads

1. process prc { /* set of processes */

2. places: p1 p2 p3

3. transitions: t1 t2 t3

4. (p1, t1, p2); (p2, t2, p3); (p3, t3, p1)

5. }

1

p3

t 1

t 2

t 3

2p

p

1. thread thr { /* set of threads */

2. places: p1

3. transitions: t1 t2 t3

4. (t1, p1); (p1, t2);

5. (p1, t3) { j >= 1; } /* conditional tran. */

6. } t 2 t

p

3

1

t 1

A Tool Suite for Concurrent Programming 8

Page 9: A Tool Suite for Concurrent Programmingmviordache.name/acts/actscp.pdf · 2017. 8. 20. · Petri nets model concurrent systems. Petri net models have been used in: distributed algorithms,

Specifications Defining Processes/Threads

Each state of a state machine can be associated with user code.

1. prc.p2 { /* User code for state ’p2’ of any process in the set ’prc’ */

2. i = i + 2;

3. v = read data(f, i);

4. }

The initial number of elements of a set and the initial state of each elementcan be specified:

initialize: prc(p1:1, p2:2) /* In the beginning ’prc’ will have three processes: one in the

state ’p1’ and two in the state ’p2’. */

A Tool Suite for Concurrent Programming 9

Page 10: A Tool Suite for Concurrent Programmingmviordache.name/acts/actscp.pdf · 2017. 8. 20. · Petri nets model concurrent systems. Petri net models have been used in: distributed algorithms,

Specifications Constraints

Synchronization:

sync prc.t1 thr.t3 gr.t2 /* requests that all transitions in the list be synchronized. */

Safety constraints:

prc.p2 + thr.p1 <= 1 /* mutual exclusion: the number of processes ’prc’ in the state

’p2’ plus the number of threads ’thr’ in the state ’p1’ must not exceed 1. */

2*prc.p1 - 3*thr.p1 <= 5 /* any linear inequality may be included */

Liveness:

live: prc.t1 gr.t0 /* all transitions in the list must be “live”, that is, all potential deadlocks

affecting them must be eliminated. */

See example and documentation directories for other features.

A Tool Suite for Concurrent Programming 10

Page 11: A Tool Suite for Concurrent Programmingmviordache.name/acts/actscp.pdf · 2017. 8. 20. · Petri nets model concurrent systems. Petri net models have been used in: distributed algorithms,

Specifications Some Concurrency Idioms

Data Parallelism: natural, specifications are in terms of sets ofthreads/processes.

Fork/Join: Synchronization involving a source/sink transition of theforked/joined process or thread.

Barrier: Implemented with a synchronization instruction.

Barriers have one limitation:

• A synchronization involving two terms synchronizes some element of thefirst set with some element of the second set. There is no distinctionbetween the elements of a set.

• If a specific element of set A should be synchronized with another specificelement of set B, it is necessary to write the specs such that no otherelements of A/B are available at the time of the synchronization.

A Tool Suite for Concurrent Programming 11

Page 12: A Tool Suite for Concurrent Programmingmviordache.name/acts/actscp.pdf · 2017. 8. 20. · Petri nets model concurrent systems. Petri net models have been used in: distributed algorithms,

Code Generation

The software generates C code for each specified thread or process set. It contains the

user code with embedded supervision code.

The software generates also the C code of a supervisor process.

Why a supervisor process and not locks? So that more general coordination constraints

can be implemented.

The supervisor communicates with the specified threads and processes by means of

unnamed pipes.

PROCESS 2

. . .

. . .

SUPERVISOR PROCESS

. . .

. . .

PROCESS 1

. . .

PROCESS M

. . .

. . .

A Tool Suite for Concurrent Programming 12

Page 13: A Tool Suite for Concurrent Programmingmviordache.name/acts/actscp.pdf · 2017. 8. 20. · Petri nets model concurrent systems. Petri net models have been used in: distributed algorithms,

Code Generation

.

.. ...

MAKE FILE

MAKE

SUPERVISOR.C

LIBRARIES

SPECS FILE

PROC−TYPE−1.EXEPROC−TYPE−1.C

PROC−TYPE−N.C

SUPERVISOR.EXE

SOFTWARESYNTHESISTOOLS

PROC−TYPE−N.EXE

Note: Thread source files are compiled and linked together with thesupervisor file.

A Tool Suite for Concurrent Programming 13

Page 14: A Tool Suite for Concurrent Programmingmviordache.name/acts/actscp.pdf · 2017. 8. 20. · Petri nets model concurrent systems. Petri net models have been used in: distributed algorithms,

Algorithm Petri Nets

State machines synchronized on transitions make up a Petri net.

c

a

gb

e

a

b

c d

e

g

f

d

b

TYPE A

e

g

f

hh

COMPOSITIONTYPE B TYPE C

2p1

p4

2pp1 p3

p8p7

p4 p5

p6p6

p8

p p3

p5

p7

A Tool Suite for Concurrent Programming 14

Page 15: A Tool Suite for Concurrent Programmingmviordache.name/acts/actscp.pdf · 2017. 8. 20. · Petri nets model concurrent systems. Petri net models have been used in: distributed algorithms,

Algorithm Petri Nets

Petri nets model concurrent systems.

Petri net models have been used in: distributed algorithms, flexible manufacturing, program

specification, communication protocols, and others.

An ordinary Petri net structure

is a tuple N = (P, T, F ) where:

P - is the set of places

T - is the set of transitions

F - is the set of transition arcs

A marking µ is a vector containing

the number of tokens for each

place.

A Petri net structure N of initial

marking µ0 is denoted as (N , µ0).

TOKEN

TRANSITIONS

TRANSITION ARCS

PLACES

3t2t

1t

5p4p

3p

1p p2

A Tool Suite for Concurrent Programming 15

Page 16: A Tool Suite for Concurrent Programmingmviordache.name/acts/actscp.pdf · 2017. 8. 20. · Petri nets model concurrent systems. Petri net models have been used in: distributed algorithms,

Algorithm Petri Nets

− fires,2t

t 1

µ = [1 0]1Τ µ = [0 1]2

Τ

− fires,

1 µ

µ µ1 2

2µ2

p

1t

2p 1

t

2

1

p1p

2t

1t

2t

t

t − not enabledt − enabled

t21 µµt − fires,

1µ = [1 2 3 0 1]Τ 2µ = [0 1 2 1 2]Τ

5p

4p

3p t

p2

1p

t 5p

4p

3p

p2

1p

A Tool Suite for Concurrent Programming 16

Page 17: A Tool Suite for Concurrent Programmingmviordache.name/acts/actscp.pdf · 2017. 8. 20. · Petri nets model concurrent systems. Petri net models have been used in: distributed algorithms,

Algorithm Supervisory Control

A supervisor enforces constraints on the operation of a Petri net.

A transition is uncontrollable if a supervisor cannot inhibit its firing.

A transition is unobservable if a supervisor cannot observe its firing.

Given a plant Petri net and a specification on the

operation of the plant Petri net, design a supervisor

represented also as a Petri net, so that the plant Petri

net satisfies the specification when connected together

with the supervisor in closed loop.

Plant + Supervisor = Closed-loop

Control places: the places of the supervisor.

inputsEvents

Control

SUPERVISOR

PLANT

A Tool Suite for Concurrent Programming 17

Page 18: A Tool Suite for Concurrent Programmingmviordache.name/acts/actscp.pdf · 2017. 8. 20. · Petri nets model concurrent systems. Petri net models have been used in: distributed algorithms,

Algorithm Supervisory Control

µ1 + µ2 ≤ 1

t

C 1

2

p1

t

1t

4tp

3

2

3

t 2

C

p

2

1

2pt 4

t 1

t

v1 − v3 ≤ 0 (vi: how many times ti has fired.)

q1 ≤ µ2

t

C 3

p

2

1t

3t4tp

21

A Tool Suite for Concurrent Programming 18

Page 19: A Tool Suite for Concurrent Programmingmviordache.name/acts/actscp.pdf · 2017. 8. 20. · Petri nets model concurrent systems. Petri net models have been used in: distributed algorithms,

Algorithm Deadlock Prevention

Let’s illustrate deadlock

prevention on a flexible

manufacturing example.

Each machine can only

process one part at a

time.

The robot moves the

parts.

The buffer is the only

storage space available

to the robot.

MACHINE 1

MACHINE 2

ROBOT

BUFFER

PARTS IN PARTS OUT

PARTS INPARTS OUT

p

3t

p2

t

2t

1p

1

p

6p

12t

10t

11t

9t

8t

7t

5p

6t

5t

4p

4t

3p 7

p8

p9

p10

p11

12p

13

A Tool Suite for Concurrent Programming 19

Page 20: A Tool Suite for Concurrent Programmingmviordache.name/acts/actscp.pdf · 2017. 8. 20. · Petri nets model concurrent systems. Petri net models have been used in: distributed algorithms,

Algorithm Deadlock Prevention

Each machine can only

process one part at a

time.

The robot moves the

parts.

The buffer is the only

storage space available

to the robot.

PARTS OUT

PARTS IN PARTS OUT

PARTS IN

MACHINE 1

MACHINE 2

ROBOT

BUFFER

p

3t

p2

t

2t

1p

1

p

6p

12t

10t

11t

9t

8t

7t

5p

6t

5t

4p

4t

3p 7

p8

p9

p10

p11

12p

13

A Tool Suite for Concurrent Programming 20

Page 21: A Tool Suite for Concurrent Programmingmviordache.name/acts/actscp.pdf · 2017. 8. 20. · Petri nets model concurrent systems. Petri net models have been used in: distributed algorithms,

Algorithm Deadlock Prevention

Each machine can only

process one part at a

time.

The robot moves the

parts.

The buffer is the only

storage space available

to the robot.

PARTS OUT

PARTS IN PARTS OUT

PARTS IN

MACHINE 1

MACHINE 2

ROBOT

BUFFER

p

3t

p2

t

2t

1p

1

p

6p

12t

10t

11t

9t

8t

7t

5p

6t

5t

4p

4t

3p 7

p8

p9

p10

p11

12p

13

A Tool Suite for Concurrent Programming 21

Page 22: A Tool Suite for Concurrent Programmingmviordache.name/acts/actscp.pdf · 2017. 8. 20. · Petri nets model concurrent systems. Petri net models have been used in: distributed algorithms,

Algorithm Deadlock Prevention

Each machine can only

process one part at a

time.

The robot moves the

parts.

The buffer is the only

storage space available

to the robot.

PARTS OUT

PARTS IN PARTS OUT

PARTS IN

MACHINE 1

MACHINE 2

ROBOT

BUFFER

p

3t

p2

t

2t

1p

1

p

6p

12t

10t

11t

9t

8t

7t

5p

6t

5t

4p

4t

3p 7

p8

p9

p10

p11

12p

13

A Tool Suite for Concurrent Programming 22

Page 23: A Tool Suite for Concurrent Programmingmviordache.name/acts/actscp.pdf · 2017. 8. 20. · Petri nets model concurrent systems. Petri net models have been used in: distributed algorithms,

Algorithm Deadlock Prevention

Each machine can only

process one part at a

time.

The robot moves the

parts.

The buffer is the only

storage space available

to the robot.

PARTS OUT

PARTS IN PARTS OUT

PARTS IN

MACHINE 1

MACHINE 2

ROBOT

BUFFER

p

3t

p2

2

t

t

1p

1

p

6p

12t

10t

11t

9t

8t

7t

5p

6t

5t

4p

4t

3p 7

p8

p9

p10

p11

12p

13

A Tool Suite for Concurrent Programming 23

Page 24: A Tool Suite for Concurrent Programmingmviordache.name/acts/actscp.pdf · 2017. 8. 20. · Petri nets model concurrent systems. Petri net models have been used in: distributed algorithms,

Algorithm Deadlock Prevention

Each machine can only

process one part at a

time.

The robot moves the

parts.

The buffer is the only

storage space available

to the robot.

PARTS OUT

PARTS IN PARTS OUT

PARTS IN

MACHINE 1

MACHINE 2

ROBOT

BUFFER

p

3t

t

p2

2t

1p

1

p

6p

12t

10t

11t

9t

8t

7t

5p

6t

5t

4p

4t

3p 7

p8

p9

p10

p11

12p

13

A Tool Suite for Concurrent Programming 24

Page 25: A Tool Suite for Concurrent Programmingmviordache.name/acts/actscp.pdf · 2017. 8. 20. · Petri nets model concurrent systems. Petri net models have been used in: distributed algorithms,

Algorithm Deadlock Prevention

Each machine can only

process one part at a

time.

The robot moves the

parts.

The buffer is the only

storage space available

to the robot.

PARTS OUT

PARTS IN PARTS OUT

PARTS IN

MACHINE 1

MACHINE 2

ROBOT

BUFFER

p

3t

p

t

2

2t

1p

1

p

6p

12t

10t

11t

9t

8t

7t

5p

6t

5t

4p

4t

3p 7

p8

p9

p10

p11

12p

13

A Tool Suite for Concurrent Programming 25

Page 26: A Tool Suite for Concurrent Programmingmviordache.name/acts/actscp.pdf · 2017. 8. 20. · Petri nets model concurrent systems. Petri net models have been used in: distributed algorithms,

Algorithm Deadlock Prevention

Deadlock, as no conti-

nuation is possible from

the current state.

Problem: How to

constrain the markings

to ensure deadlock

cannot occur?

PARTS INPARTS OUT

MACHINE 1

MACHINE 2

ROBOT

BUFFER

PARTS IN PARTS OUT

12p

p13

11pp

10p

9p

8p

7

6p

12t

10t

11t

9t

8t

7t

5p

6t

5t

4p

4t

3p

3t

p2

2t

1p

1t

A Tool Suite for Concurrent Programming 26

Page 27: A Tool Suite for Concurrent Programmingmviordache.name/acts/actscp.pdf · 2017. 8. 20. · Petri nets model concurrent systems. Petri net models have been used in: distributed algorithms,

Algorithm Deadlock Prevention

The inequalities to be enforced:

µ3 + µ6 + µ7 + µ12 ≥ 1

µ4 + µ7 + µ8 + µ11 ≥ 1

µ4 + µ6 + µ7 + µ8 + µ12 ≥ 1

2µ3 + µ4 + 2µ6 + 2µ7+

µ8 + µ11 + 2µ12 ≥ 3

In addition to the inequalities above, the

initial marking µ0 of the plant must

satisfy:

µ0,2 + µ0,6 + µ0,12 ≥ 1

µ0,3 + µ0,7 + µ0,11 ≥ 1

µ0,4 + µ0,8 + µ0,10 ≥ 1

2

2

94

8t

t t

2t

10t3t

13p

p12

11p

10p

9p

8p

7p

6p

12t

11t

7t

5p

6t

5t

4p

3p

p2

1p

1tC 4

C 2

C 3

C 1

A Tool Suite for Concurrent Programming 27

Page 28: A Tool Suite for Concurrent Programmingmviordache.name/acts/actscp.pdf · 2017. 8. 20. · Petri nets model concurrent systems. Petri net models have been used in: distributed algorithms,

For Future Work

• Specification language.

– Problem: For complex enough protocols specifications can be large.

– For future work: Add high level instructions.

• The deadlock prevention tool generates constraints that guarantee a deadlock-free

operation of the Petri net representing the software.

– Problem: There may be deadlocks that are not visible in the Petri net model.

– For future work: Implement responsiveness algorithms [Iordache, 2010 CDC].

– Problem: The current deadlock prevention procedure is not guaranteed to converge

and has high computational complexity. At this time it is not known whether these

issues are likely to manifest themselves in practice.

– For future work: Implement other algorithms or develop new algorithms.

– Problem: Deadlock prevention assumes (partially) repetitive structures.

– For future work: Generalize the deadlock prevention approach.

A Tool Suite for Concurrent Programming 28