39
Concurrent Programming CT074-3-2 Introduction & Overview

Week 2 What is Concurrent Programming

Embed Size (px)

DESCRIPTION

Concurrent Programming

Citation preview

Page 1: Week 2 What is Concurrent Programming

Concurrent Programming CT074-3-2

Introduction & Overview

Page 2: Week 2 What is Concurrent Programming

CT074-3-2 Concurrent Programming 2

Prerequisites for this module

• Fundamentals of Software Development

Page 3: Week 2 What is Concurrent Programming

CT074-3-2 Concurrent Programming 3

Learning Outcomes

At the end of this module, you should be able to do the following:

1. Explain and apply the fundamental concepts of concurrency in the design and construction of a concurrent system using Java application.

2. Discuss the safety aspects of multi threaded system3. Discuss the need for encapsulation in concurrent

systems

Page 4: Week 2 What is Concurrent Programming

CT074-3-2 Concurrent Programming 4

This module will give you the opportunity to develop the following skills

• Understanding a specific body of knowledge and skills.

• Analyse• Formulate a practical solution to a problem,

making effective use of time and resources available.

Skills

Page 5: Week 2 What is Concurrent Programming

CT074-3-2 Concurrent Programming 5

What is expected of you

• PARTICIPATION IN CLASS

• GROUP DISCUSSIONS (IN CLASS)

• GROUP DISCUSSIONS (OUT OF CLASS)

• IN CLASS EXERCISES

Page 6: Week 2 What is Concurrent Programming

CT074-3-2 Concurrent Programming 6Slide 6 (of 11)

What Support is available for you (I)

1. Lea,D. (1999), Concurrent Programming in Java, 2nd Edition, , Addison Wesley, ISBN: 0-201-31009-0

2. Wellings, A.(2004) Concurrent and Real-Time Programming in JAVA, , Wiley, ISBN: 047084437X

3. Magee, J. and Kramer, J. (2006) Concurrency: State Models & Java Programs, 2nd Edition, Wiley, ISBN:0470093552

4. Ben-Ari (2006), Principles of Concurrent and Distributed Programming, , Prentice-Hall, ISBN: 0-321-31283-X

Page 7: Week 2 What is Concurrent Programming

CT074-3-2 Concurrent Programming 7

Module Coverage

1 What is Concurrent Programming?•Introduction•Concurrency as abstract parallelism•Multitasking•The terminology of concurrency•Multiple computers•The challenge of concurrent programming2 Concurrent Programming Abstraction•The role of abstraction•Concurrent execution as interleaving of atomic statements•Justification of the abstraction•Arbitrary interleaving•Atomic statements•Correctness•Fairness3 Programming Concepts•Parameter passing modes•Java classes•modifiers,•lightweight classes,•enumerated types,•get and set properties,•generics

4 Concurrency technology•Java Threads•Delegates•Events•event handlers.5 Graphics technology –•Java•Visual Studio•GDI+ 6 Concurrency concepts•Concurrency types•Round Robin•Scheduler•Priority7 Concurrency concepts part 2•Processes v Threads•Synchronisation•Asynchronicity•Mutual exclusion8 Advantages of concurrency•Standard safety issues•liveness,•starvation•race-condition. 

Page 8: Week 2 What is Concurrent Programming

CT074-3-2 Concurrent Programming 8

9 Classical problems•Semaphore•Producer / Consumer•Thread-safe concurrent GUIs.10 Semaphores•Process states•Definition of the semaphore type•Semaphore invariants•The critical section problem for N processes11 Semaphores part 2•Order of execution problems•The producer–consumer problem•Definitions of semaphores•The problem of the dining philosophers

12 Monitors•Introduction•Declaring and using monitors•Condition variables•The producer–consumer problem13 Real-Time Systems•Introduction•Definitions•Reliability and repeatability•Synchronous systems•Asynchronous systems.14 Module Review 15 Assignment presentations 16 Assignment feedback

Page 9: Week 2 What is Concurrent Programming

CT074-3-2 Concurrent Programming 9

1. Lecturer Consultation Hours

2. Email: [email protected]

3. Software:(i) Jcreator/Eclipse/IDE(ii) JDK 1.X

What Support is available for you (II)

Page 10: Week 2 What is Concurrent Programming

CT074-3-2 Concurrent Programming 10

Assessment Details

In course - 100%

Assignment - 100% Individual

Page 11: Week 2 What is Concurrent Programming

CT074-3-2 Concurrent Programming 11

What is a Concurrent Program?

A sequential program has a single thread of control.

A concurrent program has multiple threads of control allowing it perform multiple computations in parallel and to control multiple external activities which occur at the same time.

Page 12: Week 2 What is Concurrent Programming

CT074-3-2 Concurrent Programming 12

Page 13: Week 2 What is Concurrent Programming

CT074-3-2 Concurrent Programming 13

Page 14: Week 2 What is Concurrent Programming

CT074-3-2 Concurrent Programming 14

Concurrent and Distributed Software?

CommunicationNetwork

Interacting, concurrent software components of a system:

single machine -> shared memory interactions

multiple machines -> network interactions

Page 15: Week 2 What is Concurrent Programming

CT074-3-2 Concurrent Programming 15

Why Concurrent Programming?Performance gain from multiprocessing hardware

parallelism.

Increased application throughput

an I/O call need only block one thread.

Increased application responsiveness

high priority thread for user requests.

More appropriate structure

for programs which interact with the environment,

control multiple activities and handle multiple

events.

Page 16: Week 2 What is Concurrent Programming

CT074-3-2 Concurrent Programming 16

The Promise of Concurrency

• Speed– If a task takes time t on one processor, shouldn’t it

take time t/n on n processors?• Availability

– If one process is busy, another may be ready to help• Distribution

– Processors in different locations can collaborate to solve a problem or work together

• Humans do it so why can’t computers?– Vision, cognition appear to be highly parallel activities

Page 17: Week 2 What is Concurrent Programming

CT074-3-2 Concurrent Programming 17

Same meaning?

• Concurrency: At least two tasks are making progress at the same time frame.• Not necessarily at the same time• Include techniques like time-slicing• Can be implemented on a single processing unit• Concept more general than parallelism

• Parallelism: At least two tasks execute literally at the same time. – Requires hardware with multiple processing units

Page 18: Week 2 What is Concurrent Programming

CT074-3-2 Concurrent Programming 18

Reading a file from disk takes 5 seconds and processing it takes 2 seconds

5 seconds reading file A 2 seconds processing file A 5 seconds reading file B 2 seconds processing file B ----------------------- 14 seconds total

Page 19: Week 2 What is Concurrent Programming

CT074-3-2 Concurrent Programming 19

5 seconds reading file A 5 seconds reading file B + 2 seconds processing file A 2 seconds processing file B ----------------------- 12 seconds total

Reading a file from disk takes 5 seconds and processing it takes 2 seconds

Page 20: Week 2 What is Concurrent Programming

CT074-3-2 Concurrent Programming 20

Page 21: Week 2 What is Concurrent Programming

CT074-3-2 Concurrent Programming 21

Concurrency vs. Parallelism

Page 22: Week 2 What is Concurrent Programming

CT074-3-2 Concurrent Programming 22

Do I need to know about concurrent programming?

Therac - 25 computerised radiation therapy machine

Concurrent programming errors contributed to accidents causing deaths and serious injuries.

Mars Rover

Problems with interaction between concurrent tasks caused periodic software resets reducing availability for exploration.

Concurrency is widespread but error prone.

Page 23: Week 2 What is Concurrent Programming

CT074-3-2 Concurrent Programming 23

Question

• If we have as much hardware as we want, do we get as much parallelism as we wish?

• If we have 2 cores, do we get 2x speedup?

Page 24: Week 2 What is Concurrent Programming

CT074-3-2 Concurrent Programming 24

Multitasking

Page 25: Week 2 What is Concurrent Programming

CT074-3-2 Concurrent Programming 25

Multitasking

• Multitasking is a concept of performing multiple tasks (also known as processes) over a certain period of time by executing them concurrently

Page 26: Week 2 What is Concurrent Programming

CT074-3-2 Concurrent Programming 26

Example: Rendering a Web page

• Page is a shared resource• Multiple concurrent activities in the Web

browser– Thread for each image load– Thread for text rendering– Thread for user input (e.g., “Stop” button)

• Cannot all write to page simultaneously!– Big challenge in concurrent programming:

managing access to shared resources

Page 27: Week 2 What is Concurrent Programming

CT074-3-2 Concurrent Programming 27

Page 28: Week 2 What is Concurrent Programming

CT074-3-2 Concurrent Programming 28

Page 29: Week 2 What is Concurrent Programming

CT074-3-2 Concurrent Programming 29

Page 30: Week 2 What is Concurrent Programming

CT074-3-2 Concurrent Programming 30

Implicit and explicit concurrency

• In principle, most programs may be considered concurrent in that they are likely to: – contain independent processing steps (at the block,

statement, or expression level) that may be executed in parallel; or

– trigger device operations that may proceed in parallel with the execution of the program. This may be termed implicit concurrency.

• Explicit concurrency is where concurrent behavior is specified by the program designer.

Page 31: Week 2 What is Concurrent Programming

CT074-3-2 Concurrent Programming 31

Multiple computers

• A concurrent program defines actions that may be performed simultaneously.

• A parallel program is a concurrent program that is designed for execution on parallel hardware.

• A distributed program is a parallel program designed for execution on a network of autonomous processors that do not share main memory

Page 32: Week 2 What is Concurrent Programming

CT074-3-2 Concurrent Programming 32

• Thus, concurrent program is a generic term used to describe any program involving actual or potential parallel behavior; parallel and distributed programs are sub-classes of concurrent program that are designed for execution in specific parallel processing environments.

Page 33: Week 2 What is Concurrent Programming

CT074-3-2 Concurrent Programming 34

Programming practice in Java

Java is

widely available, generally accepted and portable

provides sound set of concurrency features

Hence Java is used for all the illustrative examples, the demonstrations and the exercises. Subsequent lectures will explain how to construct Java programs such as the Cruise Control System.

“Toy” problems are also used as they exemplify particular aspects of concurrent programming problems!

Page 34: Week 2 What is Concurrent Programming

CT074-3-2 Concurrent Programming 35

The Challenges of Concurrency• Concurrent programs are harder to get right

– Folklore: need at least an order of magnitude in speedup for concurrent program to be worth the effort

• Some problems are inherently sequential– Theory – circuit evaluation is P-complete– Practice – many problems need coordination and

communication among sub-problems• Specific issues

– Communication – send or receive information– Synchronization – wait for another process to act– Atomicity – do not stop in the middle and leave a mess

Page 35: Week 2 What is Concurrent Programming

CT074-3-2 Concurrent Programming 36

slide 36

Language Support for Concurrency

• Threads– Think of a thread as a system “object” containing the

state of execution of a sequence of function calls– Each thread needs a separate run-time stack– Pass threads as arguments, return as function results

• Communication abstractions– Synchronous communication– Asynchronous buffers that preserve message order

• Concurrency control– Locking and mutual exclusion– Atomicity is more abstract, less commonly provided

Page 36: Week 2 What is Concurrent Programming

CT074-3-2 Concurrent Programming 37

The challenge of concurrent programming

• Concurrent programming is difficult– Carefully coordinate access to shared data– Race conditions may create scheduler-dependent

bugs– Hard to detect and reproduce

• Programming languages offer features to support concurrent programming– Synchronization mechanisms:

• semaphores, locks, monitors

– Still have to deal with deadlocks, granularity issues

Page 37: Week 2 What is Concurrent Programming

CT074-3-2 Concurrent Programming 38

The challenge of concurrent programming

• Need to synchronize the execution of different processes and to enable them to communicate.

Page 38: Week 2 What is Concurrent Programming

CT074-3-2 Concurrent Programming 39

The challenge of concurrent programming

Page 39: Week 2 What is Concurrent Programming

CT074-3-2 Concurrent Programming 40

Q & A

Question and Answer Session