Concurrency Important and difficult (Ada slides copied from Ed Schonberg)

Slides:



Advertisements
Similar presentations
How to Build Multi- threaded Applications in.NET Mazen S. Alzogbi Technology Specialist Microsoft Corporation.
Advertisements

Practice Session 7 Synchronization Liveness Deadlock Starvation Livelock Guarded Methods Model Thread Timing Busy Wait Sleep and Check Wait and Notify.
Concurrent Programming Abstraction & Java Threads
1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
Global Environment Model. MUTUAL EXCLUSION PROBLEM The operations used by processes to access to common resources (critical sections) must be mutually.
Ch 7 B.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
1 CSC321 §2 Concurrent Programming Abstraction & Java Threads Section 2 Concurrent Programming Abstraction & Java Threads.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Practice Session 7 Synchronization Liveness Guarded Methods Model
Monitors Chapter 7. The semaphore is a low-level primitive because it is unstructured. If we were to build a large system using semaphores alone, the.
Chapter 7 Threads  Threads & Processes  Multi Threading  Class Thread  Synchronized Methods  Wait & Notify.
CS444/CS544 Operating Systems Synchronization 2/16/2006 Prof. Searleman
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Concurrency in Ada Programming Languages 1 Robert Dewar.
Concurrency in Ada What concurrency is all about Relation to operating systems Language facilities vs library packages POSIX threads Ada concurrency Real.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
Chapter 11: Distributed Processing Parallel programming Principles of parallel programming languages Concurrent execution –Programming constructs –Guarded.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Organization of Programming Languages-Cheng (Fall 2004) Concurrency u A PROCESS or THREAD:is a potentially-active execution context. Classic von Neumann.
A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.
Concurrency - 1 Tasking Concurrent Programming Declaration, creation, activation, termination Synchronization and communication Time and delays conditional.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
1 Thread II Slides courtesy of Dr. Nilanjan Banerjee.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings 1.
Concurrency (Based on:Concepts of Programming Languages, 8th edition, by Robert W. Sebesta, 2007)
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Threads in Java. History  Process is a program in execution  Has stack/heap memory  Has a program counter  Multiuser operating systems since the sixties.
12/1/98 COP 4020 Programming Languages Parallel Programming in Ada and Java Gregory A. Riccardi Department of Computer Science Florida State University.
111 © 2002, Cisco Systems, Inc. All rights reserved.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Introduction to Concurrency.
1 Concurrency Architecture Types Tasks Synchronization –Semaphores –Monitors –Message Passing Concurrency in Ada Java Threads.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
1 Program5 Due Friday, March Prog4 user_thread... amount = … invoke delegate transact (amount)... mainThread... Total + = amount … user_thread...
Using a simple Rendez-Vous mechanism in Java
ICS 313: Programming Language Theory Chapter 13: Concurrency.
Java Thread and Memory Model
Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread.
15.1 Threads and Multi- threading Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Multithreading. Multithreaded Programming A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is.
13-1 Chapter 13 Concurrency Topics Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads C# Threads.
C H A P T E R E L E V E N Concurrent Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Multithreading & Synchronized Algoritma Pemrograman 3 Sistem Komputer – S1 Universitas Gunadarma 1.
1 5-High-Performance Embedded Systems using Concurrent Process (cont.)
1 G53SRP: Java Concurrency Control (2) – wait/notify Chris Greenhalgh School of Computer Science.
Agenda  Quick Review  Finish Introduction  Java Threads.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Concurrent Programming in Java Based on Notes by J. Johns (based on Java in a Nutshell, Learning Java) Also Java Tutorial, Concurrent Programming in Java.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Java Thread Programming
Multithreading / Concurrency
Multi Threading.
Advanced Topics in Concurrency and Reactive Programming: Asynchronous Programming Majeed Kassis.
Multithreading Chapter 23.
Monitors Chapter 7.
Multithreading.
Background and Motivation
Monitors Chapter 7.
Concurrency: Mutual Exclusion and Process Synchronization
Threaded Programming in Python
Monitors Chapter 7.
Software Engineering and Architecture
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Concurrency Important and difficult (Ada slides copied from Ed Schonberg)

Asynchronous concurrency Two threads of computation proceed “simultaneously”. Don’t know the relative timing in advance. Threads must communicate, and they must sometimes synchronize.

Difficulties Non-deterministic. What happens may depend on the relative timing. A program is correct only if it is correct for all possible interleavings of steps. For 2 thread each with N steps there are C(2N,N) ≈ 2 N interleavings. Interaction with multi-process OS (or not?). Scott has a detailed account.

Architectures Low-level architecture and peripherals. Universal, but below the PL level. Multiple CPUs. Not uncommon, but rarely enough for all the threads you want. Pseudo-parallelism. “Time sharing”. CPU executes some of one thread, then changes to another. Computers on a network. Languages for distributed system are different, reflecting the high cost of communication.

Common forms of synchronization Mutual exclusion. Condition synchronization. Barriers

Mutual exclusion Only one thread at a time can enter its critical section. Shared resources: e.g. printer, terminal, speaker. Shared memory.

Race condition Thread A: Thread B: X = X+10; X = X+20; Assembly A1: LOAD R1, X B1: LOAD R2,X A2: ADD R1, 10 B2: ADD R2, 20 A3: STORE R1, X B3: STORE R2,X

Race condition (cntd) X = 10 R1 R2 A1: LOAD R1,X 10 B1: LOAD R2,X 10 A2: ADD R1, B2: ADD R2, B3: STORE R2, X 30 A3: STORE R1,X 20

Condition synchronization Suspend the thread until some condition is met. In a complex calculation, thread A may have to wait for the result of B in order to continue. In a web search engine, a thread requests a web page from a server, then waits until the page arrives

Barriers Make sure that all the threads have reached a “barrier point” before any proceed past it. E.g. in simulation of a physical system, have a thread for each physical part, compute behavior over a time step. Make sure that all parts finish one time step before any proceed to the next.

Tasks in Ada Individual tasks and task types. Task declaration task boss; task body boss is begin … end Task type declaration type task worker; type Worker_id is access worker; task body worker is begin … end

Task Declarations Individual tasks and task types A task can be a component of a composite The number of tasks in a program is not fixed at compile time. W1, W2: worker; type Crew is array (integer range <>) of worker; First_Shift: Crew(1.. 10);

Task Activation: When does a task start running? Individual task at start of program. If statically allocated, at next begin. If dynamically allocated, at the point of allocation. declare W1, W2: Worker; Joe: Worker_ID := new Worker; // starts working at once. Third_Shift: Crew(1..N) of Worker begin \\ start W1, W2, Third_Shift … end ; \\ wait for W1, W2, Third_Shift, to complete \\ (not Joe).

Task Services A task can perform some actions (entries) on request from another task. The declaration of the task specifies the available entries. A task can also execute actions on its own behalf. task type Device is entry Read (X: out Integer); entry Write(X: Integer); end Device;

Synchronization: Rendezvous Caller makes explicit request: entry call. Callee (server) states its availability; accept statement. If server is not available, caller blocks and queues up on the entry for later service. If both present and ready, parameters are transmitted to server. Server performs body of entry Out parameters are transmitted to caller. Both caller and server continue execution.

Example: 1 item shared buffer task Buffer is entry Put(X: in Item); entry Get(X: out Item); end; task body Buffer is V: Item; begin loop accept Put(X: in Item) do V := X; end Put; accept Get(X: in Item) do X := V; end Get; end loop; end Buffer: To call: Buffer.Put(…), Buffer.Get(…)

Example: Mutual Exclusion task type Mutex is entry Lock(); entry Unlock(); end; task body Mutex is begin loop accept Lock() do ; end Lock; accept Unlock() do ; end Unlock; end loop; end Mutex;

task XMutex: Mutex; … Thread A Thread B XMutex.Lock(); X := X+10; X := X+20; XMutex.Unlock();

Delays and Time A delay statement can be executed anywhere at any time to make current task quiescent for a stated duration or until a specified time: delay 0.2; -- unit is seconds -- type is Duration. delay until Noon;

Conditional communication Need to protect against excessive delays, deadlock, starvation, caused by missing or malfunctioning task Timed entry: Caller waits for rendezvous a stated amount of time: select D.Write(997); -- D is a task or delay 0.2 end select

Conditional communication (ii) Conditional entry call: Caller ready for rendezvous only if no one else is queued and rendezvous can begin at once. select D.Write(997); else Put_Line(“Device busy”); end select; Print message if call cannot be accepted immediately.

Conditional communication (iii) The server may accept a call only if the internal state of the task is appropriate: select when not full => accept Write(V: integer); or when not empty => accept Read(V: integer); or delay 0.2; end select If several guards are satisfied and callers are present, then any one of the calls may be accepted: Non-determinism.

Readers/Writers problem Problem: Various tasks want to read and write to the same data (a chunk of data of some size). Writers must be mutually exclusive with one another and with readers, but readers need not be.

accept with and without body accept Write do V := X; end -- caller must wait until “end” accept Start; -- caller must synchronize but need not wait after acceptance.

Common barrier Problem: N threads must move through iterations of a loop in lock-step. Controller: loop for I in 1.. NThreads) loop accept finish; end loop; for I in 1.. NThreads) loop accept continue; end loop; end loop;

Common barrier (cntd) Each of the computational threads has the following form: for I in 1.. NumIterations loop … body of iteration … Controller.finish(); Controller.continue(); end loop;

Protected variables Built-in mechanism for mutual exclusion and readers/writers on data.

Concurrency in Java A thread is an object. Two ways to define a class of threads –extend class Thread and override run(); –implement interface runnable and define run(); interface runnable { public void run (); };

Creating a thread by extending Thread. class plum extends Thread { public void run() { … } } plum P = new plum(); // creates thread P P.start(); // starts P running. The start method is inherited from Thread and calls the “run” method. Alternatively, you can write new Plum().start();

Creating a thread by implementing Runnable class Pear implements Runnable { public void run() { … } Thread t1 = new Thread(new Pear()); t1.start(); or new Thread (new Pear()).start();

Pros and Cons of implementing Runnable Advantage: You can derive the new Thread class from a non-Thread class. Disadvantage: You cannot directly access Thread methods in the run method. But there is a workaround: class of I implements Runnable { public void run { Thread t = Thread.currentThread(); try { t.sleep(1000); } catch (InterruptedException ie) {return;} } }

Example of non-interacting threads Finding primality of N by dividing the range of potential factors into intervals and assigning each interval to a thread (primality.java).

Mutual exclusion in Java Static methods: static synchronized void IncrX(int I) { X = X+I; } Only one such method can run at a time. Mutual exclusion on an object; static object XMutex = new object(); synchronized(XMutex) { X = X+I; } Static class members: 1 instance per class.

Mutual exclusion over a method Declaring a method “synchronized” means that calls to this method for a particular object of the class are mutually exclusive.

Mutual exclusion over a method (example) class account { int Value = 0; synchronized void deposit(int D) { X = X+D; } } account Fred, George; Fred.deposit(10) and Fred.deposit(20) are mutually exclusive. Fred.deposit(10) and George.deposit(20) are not.

Wait/notify in Java analogous to Ada rendezvous.