Top Banner
1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: [email protected] URL: http://www.dgs.monash.edu.au/~rajkumar Concurrent Programming with Threads
168

1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: [email protected] URL: rajkumar.

Mar 26, 2015

Download

Documents

Riley Cross
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

1

Rajkumar BuyyaSchool of Computer Science and Software Engineering

Monash Technology

Melbourne, Australia

Email: [email protected]

URL: http://www.dgs.monash.edu.au/~rajkumar

Concurrent Programming with Threads

Page 2: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

2

Objectives

Explain the parallel computing right from architecture, OS, programming paradigm, and applications

Explain the multithreading paradigm, and all aspects of how to use it in an applicationCover all basic MT conceptsExplore issues related to MTContrast Solaris, POSIX, Java threadsLook at the APIs in detailExamine some Solaris, POSIX, and Java code

examples Debate on: MPP and Cluster Computing

Page 3: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

3

Agenda

Overview of Computing Operating Systems Issues Threads Basics Multithreading with Solaris and POSIX threads Multithreading in Java Distributed Computing Grand Challenges Solaris, POSIX, and Java example code

Page 4: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

4

P PP P P PMicrokernelMicrokernel

Multi-Processor Computing System

Threads InterfaceThreads Interface

Hardware

Operating System

ProcessProcessor ThreadPP

Applications

Computing Elements

Programming paradigms

Page 5: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

5

Architectures Compilers Applications P.S.Es Architectures Compilers

Applications P.S.Es

SequentialEra

ParallelEra

1940 50 60 70 80 90 2000 2030

Two Eras of Computing

Commercialization R & D Commodity

Page 6: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

6

History of Parallel Processing

PP can be traced to a tablet dated around 100 BC.

Tablet has 3 calculating positions. Infer that multiple positions:

Reliability/ Speed

Page 7: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

7

Motivating Factors

d d d Just as we learned to fly, not by constructing a machine that flaps its wings like birds, but by applying aerodynamics principles demonstrated by nature...

We modeled PP after those of biological species.

Page 8: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

8

Aggregated speed with

which complex calculations

carried out by individual neurons

response is slow (ms) - demonstrate

feasibility of PP

Motivating Factors

Page 9: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

9

Why Parallel Processing?

Computation requirements are ever increasing -- visualization, distributed databases, simulations, scientific prediction (earthquake), etc..

Sequential architectures reaching physical limitation (speed of light, thermodynamics)

Page 10: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

10

Technical Computing

Solving technology problems using

computer modeling, simulation and analysis

Life SciencesLife Sciences

Mechanical Design & Analysis (CAD/CAM)Mechanical Design & Analysis (CAD/CAM)

AerospaceAerospace

GeographicInformationSystems

GeographicInformationSystems

Page 11: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

11

No. of Processors

C.P

.I.

1 2 . . . .

Computational Power Improvement

Multiprocessor

Uniprocessor

Page 12: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

12

Age

Gro

wth

5 10 15 20 25 30 35 40 45 . . . .

Computational Power Improvement

Vertical Horizontal

Page 13: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

13

The Tech. of PP is mature and can be exploited commercially; significant R & D work on development of tools & environment.

Significant development in Networking technology is paving a way for heterogeneous computing.

Why Parallel Processing?

Page 14: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

14

Hardware improvements like Pipelining, Superscalar, etc., are non-scalable and requires sophisticated Compiler Technology.

Vector Processing works well for certain kind of problems.

Why Parallel Processing?

Page 15: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

15

Parallel Program has & needs ...

Multiple “processes” active

simultaneously solving a given

problem, general multiple processors.

Communication and synchronization

of its processes (forms the core of

parallel programming efforts).

Page 16: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

16

Processing Elements Architecture

Page 17: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

17

Simple classification by Flynn: (No. of instruction and data streams)

SISD - conventional SIMD - data parallel, vector computing MISD - systolic arrays MIMD - very general, multiple

approaches.

Current focus is on MIMD model, using general purpose processors.

(No shared memory)

Processing Elements

Page 18: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

18

SISD : A Conventional Computer

Speed is limited by the rate at which computer can transfer information internally.

ProcessorProcessorData Input Data Output

Instru

ctions

Ex:PC, Macintosh, Workstations

Page 19: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

19

The MISDArchitecture

More of an intellectual exercise than a practical configuration. Few built, but commercially not available

Data InputStream

Data OutputStream

Processor

A

Processor

B

Processor

C

InstructionStream A

InstructionStream B

Instruction Stream C

Page 20: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

20

SIMD Architecture

Ex: CRAY machine vector processing, Thinking machine cm*

Ci<= Ai * Bi

InstructionStream

Processor

A

Processor

B

Processor

C

Data Inputstream A

Data Inputstream B

Data Inputstream C

Data Outputstream A

Data Outputstream B

Data Outputstream C

Page 21: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

21

Unlike SISD, MISD, MIMD computer works asynchronously.

Shared memory (tightly coupled) MIMD

Distributed memory (loosely coupled) MIMD

MIMD Architecture

Processor

A

Processor

B

Processor

C

Data Inputstream A

Data Inputstream B

Data Inputstream C

Data Outputstream A

Data Outputstream B

Data Outputstream C

InstructionStream A

InstructionStream B

InstructionStream C

Page 22: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

22

MEMORY

BUS

Shared Memory MIMD machine

Comm: Source PE writes data to GM & destination retrieves it Easy to build, conventional OSes of SISD can be easily be ported Limitation : reliability & expandability. A memory component or

any processor failure affects the whole system. Increase of processors leads to memory contention.

Ex. : Silicon graphics supercomputers....

MEMORY

BUS

Global Memory SystemGlobal Memory System

ProcessorA

ProcessorA

ProcessorB

ProcessorB

ProcessorC

ProcessorC

MEMORY

BUS

Page 23: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

23

MEMORY

BUS

Distributed Memory MIMD

Communication : IPC on High Speed Network. Network can be configured to ... Tree, Mesh, Cube, etc. Unlike Shared MIMD

easily/ readily expandable Highly reliable (any CPU failure does not affect the whole

system)

ProcessorA

ProcessorA

ProcessorB

ProcessorB

ProcessorC

ProcessorC

MEMORY

BUS

MEMORY

BUS

MemorySystem A

MemorySystem A

MemorySystem B

MemorySystem B

MemorySystem C

MemorySystem C

IPC

channel

IPC

channel

Page 24: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

24

Laws of caution.....

Speed of computers is proportional to the square of their cost. i.e.. cost = Speed

Speedup by a parallel computer increases as the logarithm of the number of processors.

S

P

log 2P

C

S

(speed = cost2)

Speedup = log2(no. of processors)

Page 25: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

25

Caution....

Very fast development in PP and related area

have blurred concept boundaries, causing lot of

terminological confusion : concurrent computing/

programming, parallel computing/ processing,

multiprocessing, distributed computing, etc..

Page 26: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

26

It’s hard to imagine a field that changes as rapidly as

computing.

Page 27: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

27

Computer Science is an Immature Science.

(lack of standard taxonomy, terminologies)

Caution....

Page 28: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

28

There is no strict delimiters for contributors to the area of parallel processing : CA, OS, HLLs, databases, computer networks, all have a role to play.

This makes it a Hot Topic of Research

Caution....

Page 29: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

29

Parallel Programming Paradigms

Multithreading

Task level parallelism

Page 30: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

30

Serial Vs. Parallel

QPlease

COUNTER

COUNTER 1

COUNTER 2

Page 31: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

31

High Performance Computing

Parallel Machine : MPP

function1( ){ //......function stuff}

function2( ){ //......function stuff}

Serial Machine

function1 ( ): function2 ( ): Single CPUTime : add (t1, t2)

function1( ) || function2 ( ) massively parallel system containing thousands of CPUsTime : max (t1, t2)

t1

t2

Page 32: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

32

Single and Multithreaded

Processes

Single-threaded Process

Single instruction stream Multiple instruction stream

Multiplethreaded ProcessThreads of

Execution

CommonAddress Space

Page 33: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

33

OS:Multi-Processing, Multi-Threaded

Application

Application Application

Application

CPU

Better Response Times in Multiple Application Environments

Higher Throughput for Parallelizeable Applications

CPU

CPU

CPU CPU CPU

Threaded Libraries, Multi-threaded I/OThreaded Libraries, Multi-threaded I/O

Page 34: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

34

Multi-threading, continued...Multi-threaded OS enables parallel, scalable I/O

Application

CPU CPU CPU

Application

Application

OS KernelMultiple, independent I/O requests can be satisfied simultaneously because all the major disk, tape, and network drivers have been multi-threaded, allowing any given driver to run on multiple CPUs simultaneously.

Page 35: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

35

Shared memory

segments, pipes,

open files or

mmap’d files

Shared memory

segments, pipes,

open files or

mmap’d files

Basic Process Model

DATADATA

STACK

TEXTTEXT

DATADATA

STACK

TEXTTEXT

processesprocessesShared Memory

maintained by kernel

Shared Memorymaintained by kernel processesprocesses

Page 36: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

36

What are Threads?

Thread is a piece of code that can execute in concurrence with other threads.

It is a schedule entity on a processor

Local stateGlobal/ shared statePCHard/Software Context

RegistersRegisters

HardwareContext

Status WordStatus Word

Program CounterProgram Counter

Running Thread Object

Page 37: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

37

Threaded Process Model

THREAD STACK

THREAD STACK

THREAD DATA

THREAD DATA

THREAD TEXT

THREAD TEXT

SHARED MEMORY

SHARED MEMORY

Threads within a process

Independent executables All threads are parts of a process hence communication easier and simpler.

Page 38: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

38

Code-GranularityCode ItemLarge grain(task level)Program

Medium grain(control level)Function (thread)

Fine grain(data level)Loop

Very fine grain(multiple issue)With hardware

Code-GranularityCode ItemLarge grain(task level)Program

Medium grain(control level)Function (thread)

Fine grain(data level)Loop

Very fine grain(multiple issue)With hardware

Levels of Parallelism

Levels of Parallelism

Task i-lTask i-l Task iTask i Task i+1Task i+1

func1 ( ){........}

func1 ( ){........}

func2 ( ){........}

func2 ( ){........}

func3 ( ){........}

func3 ( ){........}

a ( 0 ) =..b ( 0 ) =..

a ( 0 ) =..b ( 0 ) =..

a ( 1 )=..b ( 1 )=..

a ( 1 )=..b ( 1 )=..

a ( 2 )=..b ( 2 )=..

a ( 2 )=..b ( 2 )=..

++ xx LoadLoad

Task Control Data Multiple Issue

Task Control Data Multiple Issue

Page 39: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

39

Simple Thread ExampleSimple Thread Example

void *func ( ){

/* define local data */- - - - - - - - - - - - - - - - - - - - - - /* function code */- - - - - - - - - - -thr_exit(exit_value);

}

main ( ){

thread_t tid;int exit_value;- - - - - - - - - - -thread_create (0, 0, func (), NULL, &tid);- - - - - - - - - - -thread_join (tid, 0, &exit_value);- - - - - - - - - - -

}

void *func ( ){

/* define local data */- - - - - - - - - - - - - - - - - - - - - - /* function code */- - - - - - - - - - -thr_exit(exit_value);

}

main ( ){

thread_t tid;int exit_value;- - - - - - - - - - -thread_create (0, 0, func (), NULL, &tid);- - - - - - - - - - -thread_join (tid, 0, &exit_value);- - - - - - - - - - -

}

Page 40: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

40

Few Popular Thread Models

Few Popular Thread Models

POSIX, ISO/IEEE standard

Mach C threads, CMU

Sun OS LWP threads, Sun Microsystems

PARAS CORE threads, C-DAC

Java-Threads, Sun Microsystems

Chorus threads, Paris

OS/2 threads, IBM

Windows NT/95 threads, Microsoft

POSIX, ISO/IEEE standard

Mach C threads, CMU

Sun OS LWP threads, Sun Microsystems

PARAS CORE threads, C-DAC

Java-Threads, Sun Microsystems

Chorus threads, Paris

OS/2 threads, IBM

Windows NT/95 threads, Microsoft

Page 41: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

41

Multithreading - Uniprocessors

Multithreading - Uniprocessors

Concurrency Vs Parallelism Concurrency Vs Parallelism

ConcurrencyConcurrency ConcurrencyConcurrency

Number of Simulatneous execution units > no of CPUs

Number of Simulatneous execution units > no of CPUs

P1P1

P2P2

P3P3

time

time

CPU

Page 42: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

42

Multithreading - Multithreading - MultiprocessorsMultiprocessorsMultithreading - Multithreading - MultiprocessorsMultiprocessors

Concurrency Vs Parallelism Concurrency Vs Parallelism Concurrency Vs Parallelism Concurrency Vs Parallelism

P1P1

P2P2

P3P3

time

time

No of execution process = no of CPUsNo of execution process = no of CPUs

CPU

CPU

CPU

Page 43: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

43

Computational ModelComputational Model

Parallel Execution due to : Concurrency of threads on Virtual Processors Concurrency of threads on Physical Processor

True Parallelism :threads : processor map = 1:1

Parallel Execution due to : Concurrency of threads on Virtual Processors Concurrency of threads on Physical Processor

True Parallelism :threads : processor map = 1:1

User Level ThreadsUser Level Threads

Virtual ProcessorsVirtual Processors

Physical ProcessorsPhysical Processors

User-Level Schedule (User)

Kernel-Level Schedule (Kernel)

Page 44: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

44

General Architecture ofThread Model

General Architecture ofThread Model

Hides the details of machine architecture

Maps User Threads to kernel threads

Process VM is shared, state change in VM by one thread visible to other.

Hides the details of machine architecture

Maps User Threads to kernel threads

Process VM is shared, state change in VM by one thread visible to other.

Page 45: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

45

Process ParallelismProcess Parallelism

int add (int a, int b, int & result)// function stuffint sub(int a, int b, int & result)// function stuff

int add (int a, int b, int & result)// function stuffint sub(int a, int b, int & result)// function stuff

pthread t1, t2;

pthread-create(&t1, add, a,b, & r1);

pthread-create(&t2, sub, c,d, & r2);

pthread-par (2, t1, t2);

pthread t1, t2;

pthread-create(&t1, add, a,b, & r1);

pthread-create(&t2, sub, c,d, & r2);

pthread-par (2, t1, t2);

MISD and MIMD ProcessingMISD and MIMD Processing

abr1cdr2

abr1cdr2

addadd

subsub

Processor

Data

IS1

IS2

Processor

Page 46: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

46

do““

dn/2

dn2/+1

““dn

SortSort

Data

IS

Data ParallelismData Parallelism

sort( int *array, int count)

//......

//......

sort( int *array, int count)

//......

//......

pthread-t, thread1, thread2;““pthread-create(& thread1, sort, array, N/2);pthread-create(& thread2, sort, array, N/2);pthread-par(2, thread1, thread2);

pthread-t, thread1, thread2;““pthread-create(& thread1, sort, array, N/2);pthread-create(& thread2, sort, array, N/2);pthread-par(2, thread1, thread2);

SIMD ProcessingSIMD Processing

SortSort

Processor

Processor

Page 47: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

47

PurposePurpose Threads Model

Threads Model

Process Model

Process Model

Start execution of a new thread

Start execution of a new thread

Creation of a new thread

Creation of a new thread

Wait for completion of thread

Wait for completion of thread

Exit and destroy the thread

Exit and destroy the thread

thr_join()thr_join()wait( )wait( )

exec( )exec( )

exit( )exit( )

fork ( )fork ( )

[ thr_create() builds the new thread and starts the execution

[ thr_create() builds the new thread and starts the execution

thr_create( )thr_create( )

thr_exit()thr_exit()

Process and Threaded models

Process and Threaded models

Page 48: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

48

Code ComparisonCode Comparison

Segment (Process)

main ( )

{

fork ( );

fork ( );

fork ( );

}

Segment (Process)

main ( )

{

fork ( );

fork ( );

fork ( );

}

Segment(Thread)

main()

{

thread_create(0,0,func(),0,0);

thread_create(0,0,func(),0,0);

thread_create(0,0,func(),0,0);

}

Segment(Thread)

main()

{

thread_create(0,0,func(),0,0);

thread_create(0,0,func(),0,0);

thread_create(0,0,func(),0,0);

}

Page 49: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

49

Printing ThreadPrinting Thread

Editing Thread

Editing Thread

Page 50: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

50

Independent ThreadsIndependent Threadsprinting()

{

- - - - - - - - - - - -

}

editing()

{

- - - - - - - - - - - -

}

main()

{

- - - - - - - - - - - -

id1 = thread_create(printing);

id2 = thread_create(editing);

thread_run(id1, id2);

- - - - - - - - - - - -

}

printing()

{

- - - - - - - - - - - -

}

editing()

{

- - - - - - - - - - - -

}

main()

{

- - - - - - - - - - - -

id1 = thread_create(printing);

id2 = thread_create(editing);

thread_run(id1, id2);

- - - - - - - - - - - -

}

Page 51: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

51

Cooperative threads - File Copy

Cooperative threads - File Copy

reader()

{

- - - - - - - - - -

lock(buff[i]);

read(src,buff[i]);

unlock(buff[i]);

- - - - - - - - - -

}

reader()

{

- - - - - - - - - -

lock(buff[i]);

read(src,buff[i]);

unlock(buff[i]);

- - - - - - - - - -

}

writer()

{

- - - - - - - - - -

lock(buff[i]);

write(src,buff[i]);

unlock(buff[i]);

- - - - - - - - - -

}

writer()

{

- - - - - - - - - -

lock(buff[i]);

write(src,buff[i]);

unlock(buff[i]);

- - - - - - - - - -

}

buff[0]buff[0]

buff[1]buff[1]

Cooperative Parallel Synchronized Threads

Cooperative Parallel Synchronized Threads

Page 52: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

52

RPC CallRPC Call

func(){

/* Body */}

func(){

/* Body */}

RPC(func)RPC(func)

................

ClientClient

ServerServer

Network

Network

Page 53: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

53

ServerThreads

Message PassingFacility

Server ProcessClient Process

Client Process

User Mode

Kernel Mode

Multithreaded Server

Page 54: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

54

Compiler Thread

Preprocessor Thread

Multithreaded Compiler

SourceCode

ObjectCode

Page 55: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

55

Thread Programming models

1. The boss/worker model

2. The peer model

3. A thread pipeline

Page 56: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

56

taskXtaskX

taskYtaskY

taskZtaskZ

main ( )main ( )

WorkersProgram

Files

Resources

Databases

Disks

SpecialDevices

Boss

Input (Stream)

The boss/worker model

Page 57: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

57

Examplemain() /* the boss */

{

forever {

get a request;

switch( request )

case X: pthread_create(....,taskX);

case X: pthread_create(....,taskX);

....

}

}

taskX() /* worker */

{

perform the task, sync if accessing shared resources

}

taskY() /* worker */

{

perform the task, sync if accessing shared resources

}

....

--Above runtime overhead of creating thread can be solved by thread pool

* the boss thread creates all worker thread at program initialization

and each worker thread suspends itself immediately for a wakeup call

from boss

Page 58: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

58

The peer model

taskXtaskX

taskYtaskY

WorkersProgram

Files

Resources

Databases

Disks

SpecialDevices

taskZtaskZ

Input(static)

Input(static)

Page 59: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

59

Examplemain()

{

pthread_create(....,thread1...task1);

pthread_create(....,thread2...task2);

....

signal all workers to start

wait for all workers to finish

do any cleanup

}

}

task1() /* worker */

{

wait for start

perform the task, sync if accessing shared resources

}

task2() /* worker */

{

wait for start

perform the task, sync if accessing shared resources

}

Page 60: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

60

A thread pipeline

Resources Files

Databases

Disks

Special Devices

Files

Databases

Disks

Special Devices

Files

Databases

Disks

Special Devices

Stage 1Stage 1 Stage 2Stage 2 Stage 3Stage 3

Program Filter Threads

Input (Stream)

Page 61: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

61

Examplemain()

{

pthread_create(....,stage1);

pthread_create(....,stage2);

....

wait for all pipeline threads to finish

do any cleanup

}

stage1() {

get next input for the program

do stage 1 processing of the input

pass result to next thread in pipeline

}

stage2(){

get input from previous thread in pipeline

do stage 2 processing of the input

pass result to next thread in pipeline

}

stageN()

{

get input from previous thread in pipeline

do stage N processing of the input

pass result to program output.

}

Page 62: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

62

Multithreaded Matrix Multiply...

X

A

=

B C

C[1,1] = A[1,1]*B[1,1]+A[1,2]*B[2,1]..….C[m,n]=sum of product of corresponding elements in row of A and column of B.

Each resultant element can be computed independently.

Page 63: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

63

Multithreaded Matrix Multiplytypedef struct {

int id; int size;

int row, column;

matrix *MA, *MB, *MC;

} matrix_work_order_t;

main()

{

int size = ARRAY_SIZE, row, column;

matrix_t MA, MB,MC;

matrix_work_order *work_orderp;

pthread_t peer[size*zize];

...

/* process matrix, by row, column */

for( row = 0; row < size; row++ )

for( column = 0; column < size; column++)

{

id = column + row * ARRAY_SIZE;

work_orderp = malloc( sizeof(matrix_work_order_t));

/* initialize all members if wirk_orderp */

pthread_create(peer[id], NULL, peer_mult, work_orderp);

} }

/* wait for all peers to exist*/ for( i =0; i < size*size;i++)

pthread_join( peer[i], NULL );

}

Page 64: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

64

Multithreaded Server...

void main( int argc, char *argv[] )

{

int server_socket, client_socket, clilen;

struct sockaddr_in serv_addr, cli_addr;

int one, port_id;

#ifdef _POSIX_THREADS

pthread_t service_thr;

#endif

port_id = 4000; /* default port_id */

if( (server_socket = socket( AF_INET, SOCK_STREAM, 0 )) < 0 )

{

printf("Error: Unable to open socket in parmon server.\n");

exit( 1 );

}

memset( (char*) &serv_addr, 0, sizeof(serv_addr));

serv_addr.sin_family = AF_INET;

serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);

serv_addr.sin_port = htons( port_id );

setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(one));

Page 65: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

65

Multithreaded Server...

if( bind( server_socket, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0 )

{

printf( "Error: Unable to bind socket in parmon server->%d\n",errno );

exit( 1 );

}

listen( server_socket, 5);

while( 1 )

{

clilen = sizeof(cli_addr);

client_socket = accept( server_socket, (struct sockaddr *)&serv_addr, &clilen );

if( client_socket < 0 )

{ printf( "connection to client failed in server.\n" ); continue;

}

#ifdef POSIX_THREADS

pthread_create( &service_thr, NULL, service_dispatch, client_socket);

#else

thr_create( NULL, 0, service_dispatch, client_socket, THR_DETACHED, &service_thr);

#endif

}

}

Page 66: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

66

Multithreaded Server

// Service function -- Thread Funtion

void *service_dispatch(int client_socket)

{

…Get USER Request

if( readline( client_socket, command, 100 ) > 0 )

{

…IDENTI|FY USER REQUEST

….Do NECESSARY Processing

…..Send Results to Server

}

…CLOSE Connect and Terminate THREAD

close( client_socket );

#ifdef POSIX_THREADS

pthread_exit( (void *)0);

#endif

}

Page 67: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

67

The Value of MT

• Program structure• Parallelism• Throughput• Responsiveness• System resource usage• Distributed objects• Single source across platforms (POSIX)• Single binary for any number of CPUs

Page 68: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

68

To thread or not to thread

To thread or not to thread

Improve efficiency on uniprocessor systems

Use multiprocessor Hardware

Improve Throughput Simple to implement Asynchronous I/O

Leverage special features of the OS

Improve efficiency on uniprocessor systems

Use multiprocessor Hardware

Improve Throughput Simple to implement Asynchronous I/O

Leverage special features of the OS

Page 69: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

69

To thread or not to thread

To thread or not to thread

If all operations are CPU intensive do not go far on multithreading

Thread creation is very cheap, it is not free thread that has only five lines of

code would not be useful

If all operations are CPU intensive do not go far on multithreading

Thread creation is very cheap, it is not free thread that has only five lines of

code would not be useful

Page 70: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

70

DOS - The Minimal OS

UserSpace

KernelSpace

DOSData

Stack & Stack Pointer Program Counter

UserCode

GlobalData

DOSCode

Hardware

DOS

Page 71: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

71

Multitasking OSs

Process

UserSpace

KernelSpace

Hardware

UNIX

Process Structure

(UNIX, VMS, MVS, NT, OS/2 etc.)

Page 72: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

72

Multitasking Systems

Hardware

The Kernel

P1 P2 P3 P4

Processes

(Each process is completely independent)

Page 73: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

73

Multithreaded Process

UserCode

GlobalData

The Kernel

Process Structure

(Kernel state and address space are shared)

T1’s SP T3’sPC T1’sPC T2’sPC

T1’s SP

T2’s SP

Page 74: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

74

Kernel Structures

Process ID

UID GID EUID EGID CWD.

PrioritySignal MaskRegistersKernel Stack

CPU State

File Descriptors

Signal Dispatch Table

Memory Map

Process ID

UID GID EUID EGID CWD.

File Descriptors

Signal Dispatch Table

Memory Map

Traditional UNIX Process Structure Solaris 2 Process Structure

LWP 2 LWP 1

Page 75: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

75

Scheduling Design Options

M:1HP-UNIX

1:1DEC, NT, OS/1, AIX. IRIX

M:M

2-level

Page 76: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

76

SunOS Two-Level Thread Model

Proc 1 Proc 2 Proc 3 Proc 4 Proc 5

Traditionalprocess

User

LWPsKernelthreads

Kernel

Hardware Processors

Page 77: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

77

Thread Life Cycle

main() main(){ ... { pthread_create( func, arg); thr_create( ..func..,arg..); ... ...} }void * func(){ ....}

pthread_exit()

T2

T1

pthread_create(...func...)

POSIX Solaris

Page 78: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

78

Waiting for a Thread to Exit

main() main(){ ... { pthread_join(T2); thr_join( T2,&val_ptr); ... ...} }void * func(){ ....}

pthread_exit()

T2

T1

pthread_join()

POSIX Solaris

Page 79: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

79

Scheduling States: Simplified View of Thread State

Transitions

RUNNABLE

SLEEPINGSTOPPED

ACTIVE

Stop

Continue

Preempt Stop

Stop Sleep

Wakeup

Page 80: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

80

Preemption

The process of rudely interrupting a thread and forcing it to relinquish its LWP (or CPU) to another.

CPU2 cannot change CPU3’s registers directly. It can only issue a hardware interrupt to CPU3. It is up to CPU3’s interrupt handler to look at CPU2’s request and decide what to do.Higher priority threads always preempt lower priority threads.

Preemption ! = Time slicingAll of the libraries are preemptive

Page 81: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

81

EXIT Vs. THREAD_EXIT

The normal C function exit() always causes the process to exit. That means all of the process -- All the threads.The thread exit functions:

UI : thr_exit()POSIX : pthread_exit()OS/2 : DosExitThread() and _endthread()NT : ExitThread() and endthread()

all cause only the calling thread to exit, leaving the process intact and all of the other threads running. (If no other threads are running, then exit() will be called.)

Page 82: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

82

Cancellation

Cancellation is the means by which a thread can tell another thread that it should exit.

main() main() main(){... {... {...pthread_cancel (T1); DosKillThread(T1); TerminateThread(T1)} } }There is no special relation between the killer of a thread and the victim. (UI threads must “roll their own” using signals)

(pthread exit)

(pthread cancel()

T1

T2

POSIX OS/2 Windows NT

Page 83: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

83

Cancellation State and Type

State PTHREAD_CANCEL_DISABLE (Cannot be cancelled) PTHREAD_CANCEL_ENABLE (Can be cancelled, must consider

type)

Type PTHREAD_CANCEL_ASYNCHRONOUS (any

time what-so-ever) (not generally used)

PTHREAD_CANCEL_DEFERRED (Only at cancellation points)

(Only POSIX has state and type)(OS/2 is effectively always “enabled

asynchronous”)(NT is effectively always “enabled asynchronous”)

Page 84: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

84

Cancellation is Always Complex!

It is very easy to forget a lock that’s being held or a resource that should be freed.

Use this only when you absolutely require it.

Be extremely meticulous in analyzing the possible thread states.

Document, document, document!

Page 85: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

85

Returning Status

POSIX and UI A detached thread cannot be “joined”. It cannot

return status. An undetached thread must be “joined”, and can

return a status.

OS/2 Any thread can be waited for No thread can return status No thread needs to be waited for.

NT No threads can be waited for Any thread can return status

Page 86: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

86

Suspending a Thread

main()

{

... thr_suspend(T1); ... thr_continue(T1); ...}

continue()

T2

T1

suspend()

Solaris:

* POSIX does not support thread suspension

Page 87: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

87

Proposed Uses of

Suspend/Continue

Garbage Collectors Debuggers Performance Analysers Other Tools?These all must go below the API, so they don’t

count. Isolation of VM system “spooling” (?!) NT Services specify that a service should b

suspendable (Questionable requirement?)

Be Careful

Page 88: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

88

Do NOT Think about Scheduling!

Think about Resource AvailabilityThink about SynchronizationThink about Priorities

Ideally, if you’re using suspend/ continue, you’re making a mistake!

Page 89: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

89

Synchronization

Websters: “To represent or arrange events to indicate coincidence or coexistence.”

Lewis : “To arrange events so that they occur in a specified order.”

* Serialized access to controlled resources.

Synchronization is not just an MP issue. It is not even strictly an MT issue!

Page 90: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

90

Threads Synchronization : On shared memory : shared variables -

semaphores On distributed memory :within a task : semaphores Across the tasks : By passing

messages

Threads Synchronization : On shared memory : shared variables -

semaphores On distributed memory :within a task : semaphores Across the tasks : By passing

messages

Page 91: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

91

Unsynchronized Shared Data is a Formula for

Disaster

Thread1 Thread2

temp = Your - > BankBalance;

dividend = temp * InterestRate;

newbalance = dividend + temp;

Your->Dividend += dividend; Your->BankBalance+= deposit;

Your->BankBalance = newbalance;

Page 92: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

92

Atomic Actions

An action which must be started and completed with no possibility of interruption.A machine instruction could need to be

atomic. (not all are!)A line of C code could need to be atomic. (not

all are)An entire database transaction could need to

be atomic. All MP machines provide at least one complex

atomic instruction, from which you can build anything.

A section of code which you have forced to be atomic is a Critical Section.

Page 93: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

93

Critical Section(Good Programmer!)

Critical Section(Good Programmer!)

reader()

{

- - - - - - - - - -

lock(DISK);

...........

...........

...........

unlock(DISK);

- - - - - - - - - -

}

reader()

{

- - - - - - - - - -

lock(DISK);

...........

...........

...........

unlock(DISK);

- - - - - - - - - -

}

writer()

{

- - - - - - - - - -

lock(DISK);

..............

..............

unlock(DISK);

- - - - - - - - - -

}

writer()

{

- - - - - - - - - -

lock(DISK);

..............

..............

unlock(DISK);

- - - - - - - - - -

}

Shared Data

T1T2

Page 94: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

94

Critical Section(Bad Programmer!)

Critical Section(Bad Programmer!)

reader()

{

- - - - - - - - - -

lock(DISK);

...........

...........

...........

unlock(DISK);

- - - - - - - - - -

}

reader()

{

- - - - - - - - - -

lock(DISK);

...........

...........

...........

unlock(DISK);

- - - - - - - - - -

}

writer()

{

- - - - - - - - - -

..............

..............

- - - - - - - - - -

}

writer()

{

- - - - - - - - - -

..............

..............

- - - - - - - - - -

}

Shared Data

T1T2

Page 95: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

95

Lock Shared Data!

Globals Shared data structures Static variables

(really just lexically scoped global variables)

Page 96: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

96

Mutexes

item = create_and_fill_item();

mutex_lock( &m );

item->next = list;

list = item;

mutex_unlock(&m);

mutex_lock( &m );

this_item = list;

list = list_next;

mutex_unlock(&m);

.....func(this-item);

POSIX and UI : Owner not recorded, block in priority order.

OS/2 and NT. Owner recorded, block in FIFO order.

Thread 1 Thread2

Page 97: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

97

Synchronization Variables in Shared Memory (Cross

Process)

Process 1 Process 2

S SShared MemoryS

S

SynchronizationVariable

Thread

Page 98: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

98

SynchronizationProblems

Page 99: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

99

Deadlocks

lock( M1 );

lock( M2 );

lock( M2 );

lock( M1 );

Thread 1 Thread 2

Thread1 is waiting for the resource(M2) locked by Thread2 andThread2 is waiting for the resource (M1) locked by Thread1

Page 100: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

100

Avoiding Deadlocks Establish a hierarchy : Always lock Mutex_1 before Mutex_2, etc..,. Use the trylock primitives if you must violate the hierarchy.

{

while (1)

{ pthread_mutex_lock (&m2);

if( EBUSY |= pthread mutex_trylock (&m1))

break;

else

{ pthread _mutex_unlock (&m1);

wait_around_or_do_something_else();

}

}

do_real work(); /* Got `em both! */

} Use lockllint or some similar static analysis program to scan your

code for hierarchy violations.

Page 101: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

101

Race Conditions

A race condition is where the results of a program are different depending upon the timing of the events within the program.

Some race conditions result in different answers and are clearly bugs.

Thread 1 Thread 2

mutex_lock (&m) mutex_lock (&m)

v = v - 1; v = v * 2;mutex_unlock (&m) mutex_unlock (&m)

--> if v = 1, the result can be 0 or 1based on which thread gets chance to enter CR first

Page 102: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

102

Operating System Issues

Page 103: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

103

Library Goals

Make it fast! Make it MT safe! Retain UNIX semantics!

Page 104: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

104

Are Libraries Safe ?

getc() OLD implementation: extern int get( FILE * p )

{

/* code to read data */

}

getc() NEW implementation: extern int get( FILE * p )

{

pthread_mutex_lock(&m);

/* code to read data */

pthread_mutex_unlock(&m);

}

Page 105: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

105

ERRNO

In UNIX, the distinguished variable errno is used to hold the error code for any system calls that fail.

Clearly, should two threads both be issuing system calls around the same time, it would not be possible to figure out which one set the value for errno.

Therefore errno is defined in the header file to be a call to thread-specific data.

This is done only when the flag_REENTRANT (UI)

_POSIX_C_SOURCE=199506L (POSIX) is passed to the compiler, allowing older, non-MT programs to continue to run.

There is the potential for problems if you use some libraries which are not reentrant. (This is often a problem when using third party libraries.)

Page 106: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

106

Are Libraries Safe?

MT-Safe This function is safe MT-Hot This function is safe and fast MT-Unsafe This function is not MT-safe,

but was compiled with _REENTRANT

Alternative Call This function is not safe, but there is a similar function (e.g. getctime_r())

MT-Illegal This function wasn’t even compiled with _REENTRANT and therefore can only be called from the main thread.

Page 107: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

107

Threads Debugging Interface

Debuggers Data inspectors Performance monitors Garbage collectors Coverage analyzers

Not a standard interface!

Page 108: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

108

The APIs

Page 109: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

109

Different Thread Specifications

Functionality UI Threads POSIX Thteads NT Threads OS/2 Threads

Design Philosophy Base Near-Base Complex ComplexPrimitives Primitives Primitives

PrimitivesScheduling Classes Local/ Global Local/Global Global GlobalMutexes Simple Simple Complex ComplexCounting Semaphores Simple Simple Buildable BuildableR/W Locks Simple Buildable Buildable BuildableCondition Variables Simple Simple Buildable BuildableMultiple-Object Buildable Buildable Complex ComplexSynchronizationThread Suspension Yes Impossible Yes YesCancellation Buildable Yes Yes YesThread-Specific Data Yes Yes Yes YesSignal-Handling Primitives Yes Yes n/a n/aCompiler Changes

Required No No Yes NoVendor Libraries MT-safe? Moat Most All? All?ISV Libraries MT-safe? Some Some Some Some

Page 110: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

110

POSIX and Solaris API Differences

thread cancellation

scheduling policies

sync attributes

thread attributes

continue

suspend

semaphore vars

concurrency setting

reader/ writer vars

daemon threads

join

exit key creation

priorities sigmask create

thread specific data

mutex vars kill

condition vars

POSIX API Solaris API

Page 111: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

111

Error Return Values

Many threads functions return an error value which can be looked up in errno.h.

Very few threads functions set errno(check man pages).

The “lack of resources” errors usually mean that you’ve used up all your virtual memory, and your program is likely to crash very soon.

Page 112: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

112

Attribute Objects

UI, OS/2, and NT all use flags and direct arguments to indicate what the special details of the objects being created should be. POSIX requires the use of “Attribute objects”:

thr_create(NULL, NULL, foo, NULL, THR_DETACHED);

Vs:

pthread_attr_t attr;

pthread_attr_init(&attr);

pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);

pthread_create(NULL, &attr, foo, NULL);

Page 113: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

113

Attribute Objects

Although a bit of pain in the *** compared to passing all the arguments directly, attribute objects allow the designers of the threads library more latitude to add functionality without changing the old interfaces. (If they decide they really want to, say, pass the signal mask at creation time, they just add a function pthread_attr_set_signal_mask() instead of adding a new argument to pthread_create().)There are attribute objects for:Threads

stack size, stack base, scheduling policy, scheduling class, scheduling scope, scheduling inheritance, detach state.Mutexes

Cross process, priority inheritanceCondition Variables

Cross process

Page 114: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

114

Attribute Objects

Attribute objects must be:

Allocated

Initialized

Values set (presumably)

Used

Destroyed (if they are to be free’d)

pthread_attr_t attr;

pthread_attr_init (&attr);

pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)’

pthread_create(NULL, &attr, foo, NULL);

pthread_attr_destroy (&attr);

Page 115: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

115

Thread Attribute Objects

pthread_attr_t;Thread attribute object type:

pthread_attr_init (pthread_mutexattr_t *attr)pthread_attr_destroy (pthread_attr_t *attr)

pthread_attr_getdetachstate (pthread_attr_t *attr, in *state)

pthread_attr_setdetachstate (pthread_attr_t *attr, int state)

Can the thread be joined?:pthread_attr_getscope(pthread_attr_t *attr, in *scope)pthread_attr_setscope(pthread_attr_t *attr, int scope)

Page 116: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

116

Thread Attribute Objects

pthread_attr_getinheritpolicy(pthread_attr_t *attr, int *policy)pthread_attr_setinheritpolicy(pthread_attr_t *attr, int policy)

Will the policy in the attribute object be used?pthread_attr_getschedpolicy(pthread_attr_t *attr, int *policy)pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)

Will the scheduling be RR, FIFO, or OTHER?pthread_attr_getschedparam(pthread_attr_t *attr, struct sched

param *param)pthread_attr_setschedparam(pthread attr_t *attr, struct sched

param *param);What will the priority be?

Page 117: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

117

Thread Attribute Objects

pthread_attr_getinheritsched(pthread_attr_t *attr, int *inheritsched)

pthread_attr_setinheritsched(pthread_attr_t *attr, int inheritsched)

Will the policy in the attribute object be used?

pthread_attr_getstacksize(pthread_attr_t *attr, int *size)

pthread_attr_setstacksize(pthread_attr_t *attr, int size)

How big will the stack be?

pthread_attr_getstackaddr (pthread_attr_t *attr, size_t *base)

pthread_attr_setstackaddr(pthread_attr_t *attr, size_t base)

What will the stack’s base address be?

Page 118: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

118

Mutex Attribute Objects

pthread_mutexattr_t;

mutex attribute object type

pthread_mutexattr_init(pthread_mutexattr_t *attr)

pthread_mutexattr_destroy(pthread_mutexattr_t *attr)

pthread_mutexattr_getshared(pthread_mutexattr_t*attr, int shared)

pthread_mutexattr_setpshared (pthread_mutex attr_t *attr,

int shared)

Will the mutex be shared across processes?

Page 119: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

119

Mutex Attribute Objects

pthread_mutexattr_getprioceiling(pthread_mutexattr_t

*attr, int *ceiling)pthread_mutexattr_setprioceiling(pthread_mutexattr_t

*attr, int *ceiling)What is the highest priority the thread owning this mutex can acquire?

pthread_mutexattr_getprotocol (pthread_mutexattr_t*attr, int *protocol)

pthread_mutexattr_setprotocol (pthread_mutexattr_t*attr, int protocol)

Shall the thread owning this mutex inherit priorities from waiting threads?

Page 120: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

120

Condition Variable Attribute Objects

pthread_condattr_t;CV attribute object type

pthread_condattr_init(pthread_condattr_t * attr)pthread_condattr_destroy(pthread_condattr_t *attr)pthread_condattr_getpshared (pthread_condattr_t

*attr, int *shared)pthread_condattr_setpshared (pthread_condattr_t

*attr, int shared)

Will the mutex be shared across processes?

Page 121: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

121

Creation and Destruction (UI & POSIX)

int thr_create(void *stack_base, size_t stacksize,

void *(*start_routine) (void *), void

* arg, long flags, thread_t thread);

void thr_exit (void *value_ptr);

int thr_join (thread_t thread, void **value_ptr);

int pthread_create (pthread_t *thread, const

pthread_attr_t *attr, void *

(*start_routine) (void *), void *arg);

void pthread_exit (void *value_ptr);

int pthread_join (pthread_t thread, void

**value_ptr);

int pthread_cancel (pthread_t thread);

Page 122: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

122

Suspension (UI & POSIX)

int thr_suspend (thread_t target)

int thr_continue (thread_t target)

Page 123: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

123

Changing Priority (UI & POSIX)

int thr_setpriority(thread_t thread, int priority)

int thr_getpriority(thread_t thread, int *priority)

int pthread_getschedparam(pthread_t thread, int

*policy, struct sched param

*param)

int pthread_setschedparam(pthread_t thread, int

policy, struct sched param *param)

Page 124: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

124

Readers / Writer Locks (UI)

int rwlock_init (rwlock_t *rwlock, int type, void *arg);

int rw_rdlock (rwlock_t *rwlock);

int rw_wrlock (rwlock_t *rwlock);

int rw_tryrdlock (rwlock_t *rwlock);

int rw_trywrlock (rwlock_t *rwlock);

int rw_unlock (rwlock_t *rwlock);

int rw_destroy (rwlock_t *rwlock);

Page 125: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

125

(Counting) Semaphores (UI & POSIX)

int sema_init (sema_t *sema,

unsigned int sema_count,

int type, void *arg)

int sema_wait (sema_t *sema)

int sema_post (sema_t *sema)

int sema_trywait (sema_t *sema)

int sema_destroy (sema_t *sema)

int sem_init (sem_t *sema, int pshared, unsigned int count)

int sem_post (sem_t *sema)

int sem_trywait (sem_t *sema)

int sem_destroy (sem_t *sema)

(POSIX semaphores are not part of pthread. Use the libposix4.so and posix4.h)

Page 126: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

126

Condition Variables (UI & POSIX)

int cond_init(contd_t *cond, int type, void *arg)int cond_wait(cond_t *cond, mutex_t *mutex);int cond_signal(cond_t *cond)int cond_broadcast(cond_t *cond)int cond_timedwait(cond_t *cond, mutex_t *mutex, timestruc_t

*abstime)int cond_destroy (cond_t *cond)

int pthread_cond_init(pthread_cond_t *cond,pthread_condattr_t *attr)int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t

*mutex)int pthread_cond_signal (pthread_cond_t *cond)int pthread_cond_broadcast(pthread_cond_t *cond, pthread_mutex_t

*mutex, struct timespec *abstime)int pthread_cond_destroy(pthread_cond_t *cond)

Page 127: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

127

Signals (UI & POSIX)

int thr_sigsetmask(int how, const sigset_t *set, sigset_t *oset);

int thr_kill(thread_t target thread, int sig)

int sigwait(sigset_t *set)

int pthread_sigmask(int how, const sigset_t *set, sigset_t *oset);

int pthread_kill(thread_t target_thread, int sig)

int sigwait(sigset_t *set, int *sig)

Page 128: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

128

Cancellation (POSIX)

int pthread_cancel (pthread_thread_t thread)

int pthread cleanup_pop (int execute)

int pthread_cleanup_push (void (*funtion) (void *),

void *arg)

int pthread_setcancelstate (int state, int *old_state)

int pthread_testcancel (void)

Page 129: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

129

Other APIs

thr_self(void)thr_yield()

int pthread_atfork(void (*prepare) (void),void (*parent) (void),void (*child) (void)

pthread_equal (pthread_thread_t tl, pthread_thread_t t2)

pthread_once (pthread_once_t *once_control, void(*init_routine) (void))

pthread_self (void)

pthread_yield()

(Thread IDs in Solaris recycle every 2^32 threads, or about once a month if you do create/exit as fast as possible.)

Page 130: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

130

Compiling

Page 131: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

131

Solaris Libraries

Solaris has three libraries: libthread.so, libpthread.so, libposix4.so

Corresponding new include files: synch.h, thread.h, pthread.h, posix4.h

Bundled with all O/S releasesRunning an MT program requires no extra

effortCompiling an MT program requires only a

compiler (any compiler!)Writing an MT program requires only a

compiler (but a few MT tools will come in very handy)

Page 132: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

132

Compiling UI under Solaris

Compiling is no different than for non-MT programs libthread is just another system library in /usr/lib Example:

%cc -o sema sema.c -lthread -D_REENTRANT%cc -o sema sema.c -mt

All multithreaded programs should be compiled using the _REENTRANT flag Applies for every module in a new application If omitted, the old definitions for errno, stdio would

be used, which you don’t want All MT-safe libraries should be compiled using

the _REENTRANT flag, even though they may be used single in a threaded program.

Page 133: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

133

Compiling POSIX under Solaris

Compiling is no different than for non-MT programs libpthread is just another system library in /usr/lib Example :

%cc-o sema sema.c -lpthread -lposix4 -D_POSIX_C_SOURCE=19956L

All multithreaded programs should be compiled using the _POSIX_C_SOURCE=199506L flag Applies for every module in a new application If omitted, the old definitions for errno, stdio

would be used, which you don’t want All MT-safe libraries should be compiled using

the _POSIX_C_SOURCE=199506L flag, even though they may be used single in a threaded program

Page 134: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

134

Compiling mixed UI/POSIX under Solaris

If you just want to use the UI thread functions (e.g., thr_setconcurrency())

%cc-o sema sema.c -1thread -1pthread -1posix4 D_REENTRANT -

_POSIX_PTHREAD_SEMANTICS

If you also want to use the UI semantics for fork(), alarms, timers, sigwait(), etc.,.

Page 135: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

135

Summary

Threads provide a more natural programming paradigm

Improve efficiency on uniprocessor systems

Allows to take full advantage of multiprocessor Hardware

Improve Throughput: simple to implement asynchronous I/O

Leverage special features of the OS

Many applications are already multithreaded

MT is not a silver bullet for all programming problems.

Threre is already standard for multithreading--POSIX

Multithreading support already available in the form of language syntax--Java

Threads allows to model the real world object (ex: in Java)

Threads provide a more natural programming paradigm

Improve efficiency on uniprocessor systems

Allows to take full advantage of multiprocessor Hardware

Improve Throughput: simple to implement asynchronous I/O

Leverage special features of the OS

Many applications are already multithreaded

MT is not a silver bullet for all programming problems.

Threre is already standard for multithreading--POSIX

Multithreading support already available in the form of language syntax--Java

Threads allows to model the real world object (ex: in Java)

Page 136: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

136

Java

Multithreading in Java

Page 137: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

137

Java - An Introduction

Java - The new programming language from Sun Microsystems

Java -Allows anyone to publish a web page with Java code in it

Java - CPU Independent language Created for consumer electronics Java - James , Arthur Van , and others Java -The name that survived a patent

search Oak -The predecessor of Java Java is “C++ -- ++ “

Page 138: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

138

Object Oriented Languages -A

comparisonFeature C++ Objective

CAda Java

Encapsulation Yes Yes Yes YesInheritance Yes Yes No YesMultiple Inherit. Yes Yes No NoPolymorphism Yes Yes Yes YesBinding (Early or Late) Both Both Early LateConcurrency Poor Poor Difficult YesGarbage Collection No Yes No YesGenericity Yes No Yes NoClass Libraries Yes Yes Limited Yes

Page 139: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

139

Sun defines Java as:

Simple and PowerfulSimple and Powerful SafeSafe Object OrientedObject Oriented RobustRobust Architecture Neutral and PortableArchitecture Neutral and Portable Interpreted and High PerformanceInterpreted and High Performance Threaded Threaded DynamicDynamic

Page 140: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

140

Java Integrates

Power of Compiled Languages

and

Flexibility of Interpreted Languages

Page 141: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

141

Classes and Objects

Classes and Objects Method Overloading Method Overriding Abstract Classes Visibility modifiers

default

public

protected

private protected , private

Page 142: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

142

Threads

Java has built in thread support for Multithreading

Synchronization Thread Scheduling Inter-Thread Communication:

currentThread start setPriority

yield run getPriority

sleep stop suspend

resume Java Garbage Collector is a low-priority

thread

Page 143: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

143

Ways of Multithreading in Java

Create a class that extends the Thread class Create a class that implements the Runnable interface

1st Method: Extending the Thread class class MyThread extends Thread

{

public void run()

{

// thread body of execution

}

} Creating thread:

MyThread thr1 = new MyThread(); Start Execution:

thr1.start();

Page 144: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

144

2nd method: Threads by implementing Runnable interface

class ClassName implements Runnable

{

.....

public void run()

{

// thread body of execution

}

} Creating Object:

ClassName myObject = new ClassName(); Creating Thread Object:

Thread thr1 = new Thread( myObject ); Start Execution:

thr1.start();

Page 145: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

145

Thread Class Members...public class java.lang.Thread extends java.lang.Object

implements java.lang.Runnable

{

// Fields

public final static int MAX_PRIORITY;

public final static int MIN_PRIORITY;

public final static int NORM_PRIORITY;

// Constructors

public Thread();

public Thread(Runnable target);

public Thread(Runnable target, String name);

public Thread(String name);

public Thread(ThreadGroup group, Runnable target);

public Thread(ThreadGroup group, Runnable target, String name);

public Thread(ThreadGroup group, String name);

// Methods

public static int activeCount();

public void checkAccess();

public int countStackFrames();

public static Thread currentThread();

public void destroy();

public static void dumpStack();

public static int enumerate(Thread tarray[]);

public final String getName();

Page 146: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

146

...Thread Class Members.public final int getPriority(); // 1 to 10 priority-pre-emption at mid.

public final ThreadGroup getThreadGroup();

public void interrupt();

public static boolean interrupted();

public final boolean isAlive();

public final boolean isDaemon();

public boolean isInterrupted();

public final void join();

public final void join(long millis);

public final void join(long millis, int nanos);

public final void resume();

public void run();

public final void setDaemon(boolean on);

public final void setName(String name);

public final void setPriority(int newPriority);

public static void sleep(long millis);

public static void sleep(long millis, int nanos);

public void start();

public final void stop();

public final void stop(Throwable obj);

public final void suspend();

public String toString();

public static void yield();

}

Page 147: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

147

Manipulation of Current Thread// CurrentThreadDemo.java

class CurrentThreadDemo {

public static void main(String arg[]) {

Thread ct = Thread.currentThread();

ct.setName( "My Thread" );

System.out.println("Current Thread : "+ct);

try {

for(int i=5; i>0; i--) {

System.out.println(" " + i);

Thread.sleep(1000);

}

}

catch(InterruptedException e) {

System.out.println("Interrupted."); }

}

}

Run:

Current Thread : Thread[My Thread,5,main]

5

4

3

2

1

Page 148: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

148

Creating new Thread...

// ThreadDemo.java

class ThreadDemo implements Runnable

{

ThreadDemo()

{

Thread ct = Thread.currentThread();

System.out.println("Current Thread : "+ct);

Thread t = new Thread(this,"Demo Thread");

t.start();

try

{

Thread.sleep(3000);

}

catch(InterruptedException e)

{

System.out.println("Interrupted.");

}

System.out.println("Exiting main thread.");

}

Page 149: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

149

...Creating new Thread.public void run() {

try {

for(int i=5; i>0; i--) {

System.out.println(" " + i);

Thread.sleep(1000);

} }

catch(InterruptedException e) {

System.out.println("Child interrupted.");

}

System.out.println("Exiting child thread.");

}

public static void main(String args[]) {

new ThreadDemo();

}

}

Run:

Current Thread : Thread[main,5,main]

5

4

3

Exiting main thread.

2

1

Exiting child thread.

Page 150: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

150

Thread Priority...// HiLoPri.java

class Clicker implements Runnable {

int click = 0;

private Thread t;

private boolean running = true;

public Clicker(int p)

{

t = new Thread(this);

t.setPriority(p);

}

public void run()

{

while(running)

click++;

}

public void start()

{

t.start();

}

public void stop()

{

running = false;

}

}

Page 151: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

151

...Thread Priorityclass HiLoPri

{

public static void main(String args[])

{

Thread.currentThread().setPriority(Thread.MAX_PRIORITY);

Clicker Hi = new Clicker(Thread.NORM_PRIORITY+2);

Clicker Lo = new Clicker(Thread.NORM_PRIORITY-2);

Lo.start();

Hi.start();

try {

Thread.sleep(10000);

}

catch (Exception e)

{ }

Lo.stop();

Hi.stop();

System.out.println(Lo.click + " vs. " + Hi.click);

}

}

Run1: (on Solaris)

0 vs. 956228

Run2: (Window 95)

304300 vs. 4066666

Page 152: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

152

The Java monitor model

Method 1

Method 2

Block 1Key

Threads

Monitor (synchronised) solves race-condition problem

Page 153: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

153

Threads Synchronisation...// Synch.java: race-condition without synchronisation

class Callme {

// Check synchronized and unsynchronized methods

/* synchronized */ void call(String msg)

{

System.out.print("["+msg);

try {

Thread.sleep(1000);

}

catch(Exception e)

{ }

System.out.println("]");

}

}

class Caller implements Runnable

{

String msg;

Callme Target;

public Caller(Callme t, String s)

{

Target = t;

msg = s;

new Thread(this).start();

}

Page 154: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

154

...Threads Synchronisation. public void run() {

Target.call(msg);

}

}

class Synch {

public static void main(String args[]) {

Callme Target = new Callme();

new Caller(Target, "Hello");

new Caller(Target, "Synchronized");

new Caller(Target, "World");

}

}

Run 1: With unsynchronized call method (race condition)

[Hello[Synchronized[World]

]

]

Run 2: With synchronized call method

[Hello]

[Synchronized]

[World]

Run3: With Synchronized object

synchronized(Target)

{ Target.call(msg); }

The output is the same as Run2

Page 155: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

155

Queue (no inter-threaded communication)...

// pc.java: produce and consumer

class Queue

{

int n;

synchronized int get()

{

System.out.println("Got : "+n);

return n;

}

synchronized void put(int n)

{

this.n = n;

System.out.println("Put : "+n);

}

}

class Producer implements Runnable

{

Queue Q;

Producer(Queue q)

{

Q = q;

new Thread( this, "Producer").start();

}

Page 156: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

156

Queue (no inter-threaded communication)...

public void run()

{

int i = 0;

while(true)

Q.put(i++);

}

}

class Consumer implements Runnable

{

Queue Q;

Consumer(Queue q)

{

Q = q;

new Thread( this, "Consumer").start();

}

public void run()

{

while(true)

Q.get();

}

}

Page 157: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

157

...Queue (no inter-threaded communication).

class PC

{

public static void main(String[] args)

{

Queue Q = new Queue();

new Producer(Q);

new Consumer(Q);

}

}

Run:

Put: 1

Got: 1

Got: 1

Got: 1

Put: 2

Put: 3

Got: 3

^C

Page 158: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

158

Queue (interthread communication)...

// PCnew.java: produce-consumenr with interthread communication

class Queue

{

int n;

boolean ValueSet = false;

synchronized int get()

{

try

{

if(!ValueSet)

wait();

}

catch(InterruptedException e)

{

}

System.out.println("Got : "+n);

ValueSet = false;

notify();

return n;

}

Page 159: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

159

Queue (interthread communication)...

synchronized void put(int n)

{

try {

if(ValueSet)

wait();

}

catch(InterruptedException e)

{ }

this.n = n;

System.out.println("Put : "+n);

ValueSet = true;

notify();

}

}

class Producer implements Runnable

{

Queue Q;

Producer(Queue q)

{

Q = q;

new Thread( this, "Producer").start();

}

Page 160: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

160

Queue (interthread communication)...

public void run()

{

int i = 0;

while(true)

Q.put(i++);

}

}

class Consumer implements Runnable

{

Queue Q;

Consumer(Queue q)

{

Q = q;

new Thread( this, "Consumer").start();

}

public void run()

{

while(true)

Q.get();

}

}

Page 161: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

161

...Queue (no interthread communication).

class PCnew

{

public static void main(String[] args)

{

Queue Q = new Queue();

new Producer(Q);

new Consumer(Q);

}

}

Run:

Put : 0

Got : 0

Put : 1

Got : 1

Put : 2

Got : 2

Put : 3

Got : 3

Put : 4

Got : 4

^C

Page 162: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

162

Deadlock...

// DeadLock.java

class A

{

synchronized void foo(B b)

{

String name = Thread.currentThread().getName();

System.out.println(name + " entered A.foo");

try

{

Thread.sleep(1000);

}

catch(Exception e)

{

}

System.out.println(name + " trying to call B.last()");

b.last();

}

synchronized void last()

{

System.out.println("Inside A.last");

}

}

Page 163: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

163

Deadlock...

class B

{

synchronized void bar(A a)

{

String name = Thread.currentThread().getName();

System.out.println(name + " entered B.bar");

try

{

Thread.sleep(1000);

}

catch(Exception e)

{

}

System.out.println(name + " trying to call A.last()");

a.last();

}

synchronized void last()

{

System.out.println("Inside B.last");

}

}

Page 164: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

164

...Deadlock.class DeadLock implements Runnable {

A a = new A();

B b = new B();

DeadLock() {

Thread.currentThread().setName("Main Thread");

new Thread(this).start();

a.foo(b);

System.out.println("Back in the main thread.");

}

public void run() {

Thread.currentThread().setName("Racing Thread");

b.bar(a);

System.out.println("Back in the other thread");

}

public static void main(String args[]) {

new DeadLock();

}

}

Run:

Main Thread entered A.foo

Racing Thread entered B.bar

Main Thread trying to call B.last()

Racing Thread trying to call A.last()

^C

Page 165: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

165

Grand Challenges (Is PP Practical?)

Grand Challenges (Is PP Practical?)

Need OS and Compiler support to use multiprocessor machines.

Ideal would be for the user to be unaware if the problem is running on sequential or parallel hardware - a long way to go.

With Highspeed Networks and improved microprocessor performance, multiple stand-alone machines can also be used as a parallel machine - a Popular Trend. (appealing vehicle for parallel computing)

Language standards have to evolve. (Portability). Re-orientation of thinking

Sequential Parallel

Need OS and Compiler support to use multiprocessor machines.

Ideal would be for the user to be unaware if the problem is running on sequential or parallel hardware - a long way to go.

With Highspeed Networks and improved microprocessor performance, multiple stand-alone machines can also be used as a parallel machine - a Popular Trend. (appealing vehicle for parallel computing)

Language standards have to evolve. (Portability). Re-orientation of thinking

Sequential Parallel

Page 166: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

166

Grand Challenges (Is PP Practical?)

Grand Challenges (Is PP Practical?)

Language standards have to evolve. (Portability).

Re-orientation of thinkingSequential Parallel

Language standards have to evolve. (Portability).

Re-orientation of thinkingSequential Parallel

Page 167: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

167

Breaking High Performance Computing BarriersBreaking High Performance Computing Barriers

21002100 2100 2100 2100

2100 2100 2100 2100

Single

Processor

Shared

Memory

LocalParallelCluster

GlobalParallelCluster

G

F

L

O

P

S

Page 168: 1 Rajkumar Buyya School of Computer Science and Software Engineering Monash Technology Melbourne, Australia Email: rajkumar@ieee.org URL: rajkumar.

168

Thank You ...Thank You ...