1 concurrent and distributed computing dr jose santos room 16j03

23
1 Concurrent and Distributed Computing Dr Jose Santos Room 16J03 [email protected] http://scisweb.ulster.ac.uk

Upload: darrell-wilkinson

Post on 18-Jan-2018

218 views

Category:

Documents


0 download

DESCRIPTION

3 Introduction to Concurrent Systems Introduction to Concurrent Systems n Concurrency  A collection of programs is said to be concurrent if at a given point in time, any of the programs may be the next one to execute its next (atomic) instruction.  In practice processes executing programs may need to share a single CPU, in which case only one program at a time is selected to execute (i.e. time sharing or time slicing)  A concurrent system is a system which several components may be carrying out activities at a given point in time.  The components may be processes on computers, or they may be humans or machines.

TRANSCRIPT

Page 1: 1 Concurrent and Distributed Computing Dr Jose Santos Room 16J03

1

Concurrent and Distributed Computing

Dr Jose SantosRoom [email protected]://scisweb.ulster.ac.uk

Page 2: 1 Concurrent and Distributed Computing Dr Jose Santos Room 16J03

2

Website – Materials - Assessment Module Materials:

http://scisweb.ulster.ac.uk/~jose/COM577/index.php

Assessment: 2 CW Submissions (Week 6 and 10) – 50% each [25% of Module] Examination in May [75% of Module]

Coursework Details found on the website.

Course Structure: Weeks 1-6: Concurrent Systems Weeks 7-12: Distributed Systems Lectures on Monday - Practicals on Wednesdays

Programming Environment: JAVA

Page 3: 1 Concurrent and Distributed Computing Dr Jose Santos Room 16J03

3

Introduction to Concurrent Systems Concurrency

A collection of programs is said to be concurrent if at a given point in time, any of the programs may be the next one to execute its next (atomic) instruction.

In practice processes executing programs may need to share a single CPU, in which case only one program at a time is selected to execute (i.e. time sharing or time slicing)

A concurrent system is a system which several components may be carrying out activities at a given point in time.

The components may be processes on computers, or they may be humans or machines.

Page 4: 1 Concurrent and Distributed Computing Dr Jose Santos Room 16J03

4

Introduction to Concurrent Systems Why Concurrency

It is useful to schedule programs for concurrent execution because: System can make better use of resources

Run a compilation while user thinks about response to database results Calculate an aircraft structure while disk is reading a block Use a graphics processor to render a collection of objects in a

computer game while CPU computes the dynamics Some systems are inherently concurrent

Simultaneous database queries Two computers exchanging information Interactions involving networks

Can use several processors to solve a problem faster Virtual reality Prediction of weather Faster database operations

Page 5: 1 Concurrent and Distributed Computing Dr Jose Santos Room 16J03

5

Introduction to Concurrent Systems Types of Concurrency

Multiple computers in a network Multiple applications running on one computer Multiple processors in a computer (multiple processor cores on a chip) In fact, concurrency is essential in modern programming:

Web sites must handle multiple simultaneous users. Mobile apps need to do some of their processing on servers (“in the

cloud”). Graphical user interfaces almost always require background work that does

not interrupt the user. Being able to program with concurrency will still be important in the future.

Processor clock speeds are no longer increasing. Instead, we’re getting more cores with each new generation of chips.

So in the future, in order to get a computation to run faster, we’ll have to split up a computation into concurrent pieces.

Page 6: 1 Concurrent and Distributed Computing Dr Jose Santos Room 16J03

6

Introduction to Concurrent Systems Two Models for Concurrent Programming - 1

Shared memory In the shared memory model of concurrency, concurrent modules interact

by reading and writing shared objects in memory.

Other examples of the shared-memory model: A and B might be two processors (or processor cores) in the same

computer, sharing the same physical memory. A and B might be two programs running on the same computer, sharing a

common filesystem with files they can read and write. A and B might be two threads in the same Java program sharing the same

Java objects.

Page 7: 1 Concurrent and Distributed Computing Dr Jose Santos Room 16J03

7

Introduction to Concurrent Systems Two Models for Concurrent Programming – 2

Message Passing In the message-passing model, concurrent modules interact by sending

messages to each other through a communication channel. Modules send off messages, and incoming messages to each module are queued up for handling.

Examples of message passing include: A and B might be two computers in a network, communicating by network

connections. A and B might be a web browser and a web server – A opens a connection

to B, asks for a web page, and B sends the web page data back to A. A and B might be an instant messaging client and server. A and B might be two programs running on the same computer whose

input and output have been connected by a pipe.

Page 8: 1 Concurrent and Distributed Computing Dr Jose Santos Room 16J03

8

Introduction to Concurrent Systems Example of Sequential Activity (One Porter)

Holby Hospital started off with just one porter to take patients from the ambulance to the Treatment Room and then from the Treatment Room to the Ward.

Here is a program called Porter:Porter { Nmoved := 0 Nroom := 0 while (Nmoved < 6) answer bell if bell == AMBUL and Nroom < 4 collect patient enter treatment room unload patient from trolley Nroom := Nroom + 1 leave treatment room

{ Ambulance Arrives with Patient }

Page 9: 1 Concurrent and Distributed Computing Dr Jose Santos Room 16J03

9

Introduction to Concurrent Systems

return to cubby else if bell == TROOM enter treatment room load patient onto trolley leave treatment room Nroom := Nroom - 1 Nmoved := Nmoved + 1 deposit patient on ward return to cubby endif endwhile drink tea }

{ Request to take Patient from TreatmentRoom to Ward }

Page 10: 1 Concurrent and Distributed Computing Dr Jose Santos Room 16J03

10

Holby Hospital- One Porter

Introduction to Concurrent Systems

(Treatment Room Max 4 Patients; 1 Trolley)

(Ward)(Porter)(Ambulance)

Holby Hospital- Two Porters

AP(Porter) WP(Porter)

Page 11: 1 Concurrent and Distributed Computing Dr Jose Santos Room 16J03

11

Introduction to Concurrent Systems Example of Concurrent Activity (Two Porters)

The work for the porter got too heavy, so the hospital hired an extra porter. There are now two porters. The Ambulance porter takes patients from the ambulance to the treatment room and the Ward porter takes patients from the treatment room along to the wards.

These are the programs for the two porters:AP { do 6 times load patient on trolley enter treatment room unload patient onto bed leave treatment room od drink tea }

Page 12: 1 Concurrent and Distributed Computing Dr Jose Santos Room 16J03

12

Introduction to Concurrent Systems

WP { do six times enter treatment room load patient onto trolley leave treatment room deposit patient on ward od drink coffee }

Note. The code inside each processis itself sequential code

Page 13: 1 Concurrent and Distributed Computing Dr Jose Santos Room 16J03

13

Introduction to Concurrent Systems A Concurrent Program

program WPprogram APprogram Holby nMoved = 0 nRoom = 0 fork AP other commands fork WPend

nMoved and nRoom are global variables, sometimes called shared variables because they are available to program WP and program AP and the enclosing program

Global Variables

Ambulance Process Runs

Ward Process Runs

Page 14: 1 Concurrent and Distributed Computing Dr Jose Santos Room 16J03

14

Introduction to Concurrent Systems

Execution of a Concurrent Program The pseudocode above is an example of a concurrent program. The two

components AP and WP are sequential programs which have been composed into a concurrent program.

What happens is: 1. Initialisation - set global (shared variables) to zero. 2. Create an ambulance porter process and set it running. Continue on 3. Do the same with the ward porter. 4. The program as a whole finishes when the AP process and the WP

process have both finished.

Page 15: 1 Concurrent and Distributed Computing Dr Jose Santos Room 16J03

15

Introduction to Concurrent Systems

Synchronisation Three problems

1. The treatment room is too small to hold two trolleys: get collisions 2. The ward porter gets into the treatment room but find it empty. 3. Ambulance porter enters the treatment room but finds it full.

Correct Execution Each porter when carrying out instructions is a process. To avoid

problems the two processes must synchronise. Types of Synchronisation

There are two main types: mutual exclusion condition synchronisation

Page 16: 1 Concurrent and Distributed Computing Dr Jose Santos Room 16J03

16

Introduction to Concurrent Systems Mutual exclusion

Solves Problem 1. Only one porter can be in the room at a time. Each porter

excludes the other. Condition synchronisation.

Solves Problem 2: Ward porter enters treatment room only if there's at least one

patient in the room. Solves Problem 3:

Ambulance porter only enters room when there's at least one empty bed.

{ Wait if room is empty }

{ Wait if the room is full}

Page 17: 1 Concurrent and Distributed Computing Dr Jose Santos Room 16J03

17

Introduction to Concurrent Systems Concurrent Programs

Sequential Program A list of actions to be taken one after the other Examples: PORTER, AP, WP

Concurrent Program A program formed from the composition of two or more sequential

programs Example: AP and WP work together

Process The execution of a sequential program is called a process. Example: one of the porters while working

Concurrent Execution The co-ordinated activity of the processes produced by the sequential

components of a concurrent program

WP

AP Execute

Concurrently

Page 18: 1 Concurrent and Distributed Computing Dr Jose Santos Room 16J03

18

Introduction to Concurrent Systems Concurrent Execution as Interleaved Actions

Atomic Action A programming instruction which changes the state of a system

indivisibly. (It can't be interrupted by an instruction from another process)

Finite Progress Assumption Assume that we can't tell how fast the sequential components execute.

But we do assume that if there is one process which is able to proceed then it will. If more than one process can proceed then we must consider all the possibilities.

Interleaving An interleaving of a concurrent program is a list of the atomic

actions of its sequential programs. Each sequential program is listed in correct order in the interleaving, but the actions of different sequential components can be intermixed.

A history of a concurrent program is a possible interleaving of its atomic actions.

Page 19: 1 Concurrent and Distributed Computing Dr Jose Santos Room 16J03

19

Introduction to Concurrent Systems Casualty Inter-leavings

1. Undesirable History Time 12:10 AP: load patient onto trolley 12:20 AP: enter T room 12:22 WP: enter T room (CRASH) And more instructions in list...

2. Undesirable History Time 12:10 AP: collect patient 12:15 WP: enter T room 12:20 WP: load trolley (Problem 1) And more ...

Page 20: 1 Concurrent and Distributed Computing Dr Jose Santos Room 16J03

20

Introduction to Concurrent Systems 3. Desirable History

Time 12:10 AP: collect patient 12:20 AP: enter T room 12:22 AP: unload trolley 12:24 AP: leave T room 12:26 WP: enter T room 12:28 WP: load trolley 12:30 WP: leave T room 12:35 WP: deposit patient on ward ...... And so on

Aim To ensure that all possible histories of a concurrent program are

desirable ones.

Page 21: 1 Concurrent and Distributed Computing Dr Jose Santos Room 16J03

21

Introduction to Concurrent Systems Concurrent Programming Language Functionality

1. Express the behaviour of each actor in a concurrent system as a sequential program

2. Have some mechanism for starting and stopping the sequential programs.

3. Add extra constructs to sequential language to get synchronisation Examples:

Create processes (threads or conventional processes) Special machine instructions Semaphores Monitors Message passing Remote procedure calls Client-Server Paradigm Remote methods

Page 22: 1 Concurrent and Distributed Computing Dr Jose Santos Room 16J03

22

Introduction to Concurrent Systems Review Questions

1. List some computing solutions where concurrency occurs.

2. Resource sharing is often a source of problems in concurrency. List some resources that get shared.

3. What are the advantages of concurrency?

4. Classify the following as being to do with mutual exclusion, condition synchronisation or both

(a) Two database users want to write to the same record A(b) Drivers must wait for the traffic light to go green before proceeding(c) Process B uses results of Process A.(d) Only one train can be on a given set of track at a time.(e) A philosopher must wait for a fork to become free before picking it up.

Page 23: 1 Concurrent and Distributed Computing Dr Jose Santos Room 16J03

23

Introduction to Concurrent Systems

Summary Topics addressed:-

Introduction to Concurrency Sequential and Concurrent Activity Concurrent Program Execution Synchronization (mutual exclusion and condition synchronization) Concurrent Execution (Execution History) Concurrent Programming Language Functionality