11Concurrent Programming CS 4215: Programming Language - - PowerPoint PPT Presentation

11 concurrent programming
SMART_READER_LITE
LIVE PREVIEW

11Concurrent Programming CS 4215: Programming Language - - PowerPoint PPT Presentation

Spawning Concurrent Computation Shared Variables and Granularity of Concurrency Mutual Exclusion Monitors Implementation of Concurrent Constructs 11Concurrent Programming CS 4215: Programming Language Implementation Martin Henz March 30,


slide-1
SLIDE 1

Spawning Concurrent Computation Shared Variables and Granularity of Concurrency Mutual Exclusion Monitors Implementation of Concurrent Constructs

11—Concurrent Programming

CS 4215: Programming Language Implementation

Martin Henz

March 30, 2012

Generated on Wednesday 4 April, 2012, 10:28 CS 4215: Programming Language Implementation 11—Concurrent Programming

slide-2
SLIDE 2

Spawning Concurrent Computation Shared Variables and Granularity of Concurrency Mutual Exclusion Monitors Implementation of Concurrent Constructs

Sequential Execution

Languages covered so far are sequential; instructions are executed in a fixed order. Denotational semantics:

Application: function arguments must be values, Sequence: Output store of left is input store of right, etc

CS 4215: Programming Language Implementation 11—Concurrent Programming

slide-3
SLIDE 3

Spawning Concurrent Computation Shared Variables and Granularity of Concurrency Mutual Exclusion Monitors Implementation of Concurrent Constructs

Concurrent Execution

The world is concurrent. Computer programs that represent or simulate the real world, need concurrency. Computers run concurrently and need to communicate with each other. Computer programs to represent concurrency.

CS 4215: Programming Language Implementation 11—Concurrent Programming

slide-4
SLIDE 4

Spawning Concurrent Computation Shared Variables and Granularity of Concurrency Mutual Exclusion Monitors Implementation of Concurrent Constructs

Levels of Abstraction

High: communicating concurrent processes, Medium: shared-memory threads within one processor, Lowest: electrical signals within processor.

CS 4215: Programming Language Implementation 11—Concurrent Programming

slide-5
SLIDE 5

Spawning Concurrent Computation Shared Variables and Granularity of Concurrency Mutual Exclusion Monitors Implementation of Concurrent Constructs

Hardware Architectures

Single processor/single memory, Multiple processor/single memory, ... Large-scale distributed systems.

CS 4215: Programming Language Implementation 11—Concurrent Programming

slide-6
SLIDE 6

Spawning Concurrent Computation Shared Variables and Granularity of Concurrency Mutual Exclusion Monitors Implementation of Concurrent Constructs

Granularity of Concurrency

coarse-grained message-passing concurrency, fine-grained shared-memory concurrency. Discussion Distinction is getting blurred with the virtualization of computing (proxies).

CS 4215: Programming Language Implementation 11—Concurrent Programming

slide-7
SLIDE 7

Spawning Concurrent Computation Shared Variables and Granularity of Concurrency Mutual Exclusion Monitors Implementation of Concurrent Constructs

Programming Language Focus

Message passing does not pose programming language issues. Shared-memory concurrency requires careful programming language design.

CS 4215: Programming Language Implementation 11—Concurrent Programming

slide-8
SLIDE 8

Spawning Concurrent Computation Shared Variables and Granularity of Concurrency Mutual Exclusion Monitors Implementation of Concurrent Constructs Adding Concurrency to a Sequential Language Simplest Approach Alternative Syntax Object-oriented Approach Starting Threads in cPL

1 Spawning Concurrent Computation

Adding Concurrency to a Sequential Language Simplest Approach Alternative Syntax Object-oriented Approach Starting Threads in cPL

2 Shared Variables and Granularity of Concurrency 3 Mutual Exclusion 4 Monitors 5 Implementation of Concurrent Constructs

CS 4215: Programming Language Implementation 11—Concurrent Programming

slide-9
SLIDE 9

Spawning Concurrent Computation Shared Variables and Granularity of Concurrency Mutual Exclusion Monitors Implementation of Concurrent Constructs Adding Concurrency to a Sequential Language Simplest Approach Alternative Syntax Object-oriented Approach Starting Threads in cPL

Adding Concurrency to a Sequential Language

Main composition operator remains sequential. Paradigm: “communicating sequential processes” Issues:

How to create concurrent computation? How to synchronize concurrent computation?

CS 4215: Programming Language Implementation 11—Concurrent Programming

slide-10
SLIDE 10

Spawning Concurrent Computation Shared Variables and Granularity of Concurrency Mutual Exclusion Monitors Implementation of Concurrent Constructs Adding Concurrency to a Sequential Language Simplest Approach Alternative Syntax Object-oriented Approach Starting Threads in cPL

Simple Approach

(f x); thread (g y) end; (h z) Observations Every program starts in one thread. thread...end creates a new thread. When expression within thread...end terminates, the thread is discarded.

CS 4215: Programming Language Implementation 11—Concurrent Programming

slide-11
SLIDE 11

Spawning Concurrent Computation Shared Variables and Granularity of Concurrency Mutual Exclusion Monitors Implementation of Concurrent Constructs Adding Concurrency to a Sequential Language Simplest Approach Alternative Syntax Object-oriented Approach Starting Threads in cPL

Alternative Syntax

Functional language Chez Scheme makes use of functions as the way to specify what program a new thread executes. (fork-thread (lambda () ...)) Observation The program to be executed by the new thread is given by the body of the zero-argument function passed to fork-thread.

CS 4215: Programming Language Implementation 11—Concurrent Programming

slide-12
SLIDE 12

Spawning Concurrent Computation Shared Variables and Granularity of Concurrency Mutual Exclusion Monitors Implementation of Concurrent Constructs Adding Concurrency to a Sequential Language Simplest Approach Alternative Syntax Object-oriented Approach Starting Threads in cPL

Object-oriented Approach

Java uses classes and objects to define concurrent behavior. Example: class MyThread extends Thread { public void run() { ... } } ... someThread = new MyThread(); someThread.start();

CS 4215: Programming Language Implementation 11—Concurrent Programming

slide-13
SLIDE 13

Spawning Concurrent Computation Shared Variables and Granularity of Concurrency Mutual Exclusion Monitors Implementation of Concurrent Constructs Adding Concurrency to a Sequential Language Simplest Approach Alternative Syntax Object-oriented Approach Starting Threads in cPL

Starting Threads in cPL

E thread E end Convention 1 thread...end immediately evaluates to the boolean constant true. Convention 2 The result of evaluating E is ignored, once the thread terminates.

CS 4215: Programming Language Implementation 11—Concurrent Programming

slide-14
SLIDE 14

Spawning Concurrent Computation Shared Variables and Granularity of Concurrency Mutual Exclusion Monitors Implementation of Concurrent Constructs Example Granularity of Concurrency

1 Spawning Concurrent Computation 2 Shared Variables and Granularity of Concurrency

Example Granularity of Concurrency

3 Mutual Exclusion 4 Monitors 5 Implementation of Concurrent Constructs

CS 4215: Programming Language Implementation 11—Concurrent Programming

slide-15
SLIDE 15

Spawning Concurrent Computation Shared Variables and Granularity of Concurrency Mutual Exclusion Monitors Implementation of Concurrent Constructs Example Granularity of Concurrency

Example

let accountBalance = 20 in let withdraw = fun x -> if x > accountBalance then false else accountBalance := accountBalance - x; true end end in thread (withdraw 14) end; thread (withdraw 17) end; accountBalance end end

CS 4215: Programming Language Implementation 11—Concurrent Programming

slide-16
SLIDE 16

Spawning Concurrent Computation Shared Variables and Granularity of Concurrency Mutual Exclusion Monitors Implementation of Concurrent Constructs Example Granularity of Concurrency

Granularity of Concurrency

Lowest level: parallel write access to the same memory location; undefined behavior. Highest level: threads are executed atomically; once execution is started, no interference of other processes is possible. Middle way: Define level of granularity, and use interleaving execution. Interleaving in Virtual Machine Virtual-machine based implementations choose machine instructions as the granularity at which interleaving happens. Machine instructions are not interruptable.

CS 4215: Programming Language Implementation 11—Concurrent Programming

slide-17
SLIDE 17

Spawning Concurrent Computation Shared Variables and Granularity of Concurrency Mutual Exclusion Monitors Implementation of Concurrent Constructs Problem Semaphores Semaphores in cPL Example

1 Spawning Concurrent Computation 2 Shared Variables and Granularity of Concurrency 3 Mutual Exclusion

Problem Semaphores Semaphores in cPL Example

4 Monitors 5 Implementation of Concurrent Constructs

CS 4215: Programming Language Implementation 11—Concurrent Programming

slide-18
SLIDE 18

Spawning Concurrent Computation Shared Variables and Granularity of Concurrency Mutual Exclusion Monitors Implementation of Concurrent Constructs Problem Semaphores Semaphores in cPL Example

Problem of Mutual Exclusion

How can we protect a code section from being executed by multiple threads concurrently? How can we prevent withdraw from getting a negative balance?

CS 4215: Programming Language Implementation 11—Concurrent Programming

slide-19
SLIDE 19

Spawning Concurrent Computation Shared Variables and Granularity of Concurrency Mutual Exclusion Monitors Implementation of Concurrent Constructs Problem Semaphores Semaphores in cPL Example

Semaphore Operations

wait = fun s -> while \ (s > 0) do true end; s := s - 1 end signal = fun s -> s := s + 1 end Important requirement Both assignment operations need to execute atomically, without interruption by another thread. The test and decrement in wait must not be interrupted!

CS 4215: Programming Language Implementation 11—Concurrent Programming

slide-20
SLIDE 20

Spawning Concurrent Computation Shared Variables and Granularity of Concurrency Mutual Exclusion Monitors Implementation of Concurrent Constructs Problem Semaphores Semaphores in cPL Example

Semaphores in cPL

The operations wait and signal are provided as primitive

  • perators in prefix notation in cPL, similar to the empty operator.

Thus, the programmer can write signal s and wait s.

CS 4215: Programming Language Implementation 11—Concurrent Programming

slide-21
SLIDE 21

Spawning Concurrent Computation Shared Variables and Granularity of Concurrency Mutual Exclusion Monitors Implementation of Concurrent Constructs Problem Semaphores Semaphores in cPL Example

Example

let accountBalance = 20 s = 1 in let withdraw = fun x -> wait s; if x > accountBalance then false else accountBalance := accountBalance-x; true end; signal s end in ... end end

CS 4215: Programming Language Implementation 11—Concurrent Programming

slide-22
SLIDE 22

Spawning Concurrent Computation Shared Variables and Granularity of Concurrency Mutual Exclusion Monitors Implementation of Concurrent Constructs Synchronized Methods Example Details Wait/Notify Definition of Monitors

1 Spawning Concurrent Computation 2 Shared Variables and Granularity of Concurrency 3 Mutual Exclusion 4 Monitors

Synchronized Methods Example Details Wait/Notify Definition of Monitors

5 Implementation of Concurrent Constructs

CS 4215: Programming Language Implementation 11—Concurrent Programming

slide-23
SLIDE 23

Spawning Concurrent Computation Shared Variables and Granularity of Concurrency Mutual Exclusion Monitors Implementation of Concurrent Constructs Synchronized Methods Example Details Wait/Notify Definition of Monitors

Synchronized Methods

Execution of threads is restricted such that only one synchronized method invocation can operate on the same

  • bject at a time.

If a thread A is already executing synchronized method on an

  • bject, thread B suspends, when trying to enter synchronized

method.

CS 4215: Programming Language Implementation 11—Concurrent Programming

slide-24
SLIDE 24

Spawning Concurrent Computation Shared Variables and Granularity of Concurrency Mutual Exclusion Monitors Implementation of Concurrent Constructs Synchronized Methods Example Details Wait/Notify Definition of Monitors

Example in Java

class account { private int accountBalance; public synchronized void withdraw(int x) { if (x > accountBalance) false; else accountBalance = accountBalance - x } }

CS 4215: Programming Language Implementation 11—Concurrent Programming

slide-25
SLIDE 25

Spawning Concurrent Computation Shared Variables and Granularity of Concurrency Mutual Exclusion Monitors Implementation of Concurrent Constructs Synchronized Methods Example Details Wait/Notify Definition of Monitors

Details

Every object is associated with its own queue. When thread tries to enter a synchronized method for object currently executing, thread is placed in queue. Synchronized methods can call other methods (synchronized

  • r not).

When a thread terminates execution of synchronized method (through regular execution or through an exception), the next thread in queue is resumed.

CS 4215: Programming Language Implementation 11—Concurrent Programming

slide-26
SLIDE 26

Spawning Concurrent Computation Shared Variables and Granularity of Concurrency Mutual Exclusion Monitors Implementation of Concurrent Constructs Synchronized Methods Example Details Wait/Notify Definition of Monitors

Wait/Notify

Primitives wait(), and notify() available within synchronized methods. Thread enters object’s queue by calling a synchronized method, or by calling wait(). When synchronized method call returns, or when method calls wait(), another thread gets access to the object. If thread was put in the queue by a call to wait(), it must be “unfrozen” by a call to notify() or notifyAll(). notifyAll() “unfreezes” all threads that wait for the object; notify() picks random waiting thread.

CS 4215: Programming Language Implementation 11—Concurrent Programming

slide-27
SLIDE 27

Spawning Concurrent Computation Shared Variables and Granularity of Concurrency Mutual Exclusion Monitors Implementation of Concurrent Constructs Synchronized Methods Example Details Wait/Notify Definition of Monitors

Monitors

Definition Combination of synchronized methods and wait/notify built-ins Origin Pioneered by Per Brinch Hansen in the context of the language Concurrent Pascal

CS 4215: Programming Language Implementation 11—Concurrent Programming

slide-28
SLIDE 28

Spawning Concurrent Computation Shared Variables and Granularity of Concurrency Mutual Exclusion Monitors Implementation of Concurrent Constructs Compilation of Thread Creation Implementing Interleaving Thread Creation and Termination Semaphore Operations

1 Spawning Concurrent Computation 2 Shared Variables and Granularity of Concurrency 3 Mutual Exclusion 4 Monitors 5 Implementation of Concurrent Constructs

Compilation of Thread Creation Implementing Interleaving Thread Creation and Termination Semaphore Operations

CS 4215: Programming Language Implementation 11—Concurrent Programming

slide-29
SLIDE 29

Spawning Concurrent Computation Shared Variables and Granularity of Concurrency Mutual Exclusion Monitors Implementation of Concurrent Constructs Compilation of Thread Creation Implementing Interleaving Thread Creation and Termination Semaphore Operations

Basics

Choose interleaving execution of threads at the level of virtual machine instructions. Use virtual machine for imPL/oPL as starting point. Threads are running independently, each with their own set of registers.

CS 4215: Programming Language Implementation 11—Concurrent Programming

slide-30
SLIDE 30

Spawning Concurrent Computation Shared Variables and Granularity of Concurrency Mutual Exclusion Monitors Implementation of Concurrent Constructs Compilation of Thread Creation Implementing Interleaving Thread Creation and Termination Semaphore Operations

Compilation of Thread Creation

E ֒ → s thread E end ֒ → STARTTHREAD |s + 2|.s.ENDTHREAD

CS 4215: Programming Language Implementation 11—Concurrent Programming

slide-31
SLIDE 31

Spawning Concurrent Computation Shared Variables and Granularity of Concurrency Mutual Exclusion Monitors Implementation of Concurrent Constructs Compilation of Thread Creation Implementing Interleaving Thread Creation and Termination Semaphore Operations

Implementing Interleaving

Switching execution from thread to thread, also called “time-slicing” Keep queue of threads in the machine, each with its own registers machine picks a thread from the queue, and executes a certain number of instructions in that thread Then, it suspends the execution of the thread, and starts execution of the next thread in the queue. Terminology The process of saving and re-installing registers is called context switching.

CS 4215: Programming Language Implementation 11—Concurrent Programming

slide-32
SLIDE 32

Spawning Concurrent Computation Shared Variables and Granularity of Concurrency Mutual Exclusion Monitors Implementation of Concurrent Constructs Compilation of Thread Creation Implementing Interleaving Thread Creation and Termination Semaphore Operations

Execution of STARTTHREAD n

Set the program counter of the new thread to the address after the instruction, Set the environment of the new thread to the current environment, Initialize the operand and runtime stacks of the new thread to be empty stacks, Push true on operand stack of old thread, Increment program counter of old thread by n

CS 4215: Programming Language Implementation 11—Concurrent Programming

slide-33
SLIDE 33

Spawning Concurrent Computation Shared Variables and Granularity of Concurrency Mutual Exclusion Monitors Implementation of Concurrent Constructs Compilation of Thread Creation Implementing Interleaving Thread Creation and Termination Semaphore Operations

Exception Handling and Threads

Exceptions raised in a thread do not have any effect outside the

  • thread. When the execution of a THROW instruction reaches the

bottom of the runtime stack, the executing thread is terminated. Remark With this, oPL follows common practice among languages with threads and exception handling.

CS 4215: Programming Language Implementation 11—Concurrent Programming

slide-34
SLIDE 34

Spawning Concurrent Computation Shared Variables and Granularity of Concurrency Mutual Exclusion Monitors Implementation of Concurrent Constructs Compilation of Thread Creation Implementing Interleaving Thread Creation and Termination Semaphore Operations

Alternative

Copy the current runtime stack to the new thread upon thread creation, Record the parent thread in every thread, Exception that jumps beyond thread boundaries dequeue parent threads.

CS 4215: Programming Language Implementation 11—Concurrent Programming

slide-35
SLIDE 35

Spawning Concurrent Computation Shared Variables and Granularity of Concurrency Mutual Exclusion Monitors Implementation of Concurrent Constructs Compilation of Thread Creation Implementing Interleaving Thread Creation and Termination Semaphore Operations

Execution of ENDTHREAD

Deallocates the executing thread object, along with its registers.

CS 4215: Programming Language Implementation 11—Concurrent Programming

slide-36
SLIDE 36

Spawning Concurrent Computation Shared Variables and Granularity of Concurrency Mutual Exclusion Monitors Implementation of Concurrent Constructs Compilation of Thread Creation Implementing Interleaving Thread Creation and Termination Semaphore Operations

Compilation of Semaphore Operations

signal v ֒ → SIGNAL v wait v ֒ → WAIT v

CS 4215: Programming Language Implementation 11—Concurrent Programming

slide-37
SLIDE 37

Spawning Concurrent Computation Shared Variables and Granularity of Concurrency Mutual Exclusion Monitors Implementation of Concurrent Constructs Compilation of Thread Creation Implementing Interleaving Thread Creation and Termination Semaphore Operations

Execution of SIGNAL

s(pc) = SIGNAL x (os, pc, e, rs, h) ⇉s (deref(e, x, h) + 1.os, pc + 1, e, rs, update(e, x, deref(e, x, h) + 1)) Remark The heap is shared between different threads; other threads are not represented in the rule.

CS 4215: Programming Language Implementation 11—Concurrent Programming

slide-38
SLIDE 38

Spawning Concurrent Computation Shared Variables and Granularity of Concurrency Mutual Exclusion Monitors Implementation of Concurrent Constructs Compilation of Thread Creation Implementing Interleaving Thread Creation and Termination Semaphore Operations

Execution of WAIT

s(pc) = WAIT x (os, pc, e, rs, h) ⇉s (deref(e, x, h) − 1.os, pc + 1, e, rs, update(e, x, deref(e, x, h) − 1)) if deref(e, x, h) > 0

CS 4215: Programming Language Implementation 11—Concurrent Programming

slide-39
SLIDE 39

Spawning Concurrent Computation Shared Variables and Granularity of Concurrency Mutual Exclusion Monitors Implementation of Concurrent Constructs Compilation of Thread Creation Implementing Interleaving Thread Creation and Termination Semaphore Operations

Execution of WAIT

s(pc) = WAIT x (os, pc, e, rs, h) ⇉s (os, pc, e, rs, h) if deref(e, x, h) ≤ 0 Remark Executing thread keeps checking the semaphore variable. This behavior is called busy waiting.

CS 4215: Programming Language Implementation 11—Concurrent Programming

slide-40
SLIDE 40

Spawning Concurrent Computation Shared Variables and Granularity of Concurrency Mutual Exclusion Monitors Implementation of Concurrent Constructs Compilation of Thread Creation Implementing Interleaving Thread Creation and Termination Semaphore Operations

Last Week

Some challenge projects Wrapping up CS4215

CS 4215: Programming Language Implementation 11—Concurrent Programming