Team • Lecturer ◦ Karol Ostrovský • Course issues: ppht_snabela_cs.chalmers.se • Other issues: karol_snabela_chalmers.se
Concurrent Programming
• Assistants
Introduction
◦ Nick Smallbone – nicsma_snabela_chalmers.se ◦ Filippo Del Tedesco – tedesco_snabela_chalmers.se
PPHT09 – Introduction
The Message from this Course
Introduction
• Should you forget everything from this course, please, remember at least this saying:
• Why concurrent programming? ◦ In general ◦ In this course
• Practical course information • Gentle start
Use the right tool for the job.
PPHT09 – Introduction
◦ Java ◦ JR
3
Why? • Where is John von Neumann? • Using the processor efficiently in the presence of I/O presence of I/O
2
PPHT09 – Introduction
4
PPHT09 – Introduction
6
Why? • Modelling inherently concurrent systems
Press any key…
◦ Example: Software controllers which handle responses from several physical sources h i l
◦ Operating systems ◦ Distributed systems ◦ Real‐time systems
• The real world is not sequential!
PPHT09 – Introduction
5
1
Why? − today’s hype
Concurrency vs Parallelism
• Multi‐core/Many‐core/Multi‐processor • Performing computationally expensive tasks using multi‐X hardware
Parallell programmering ≠ parallel programming Parallell programmering ≠
• Parallel ◦ p physically at the same time y y
• Concurrent ◦ logically at the same time, but might be implemented without any real parallelism
• The book covers parallel programming too – but it will not be the focus of this course PPHT09 – Introduction
7
Course Goals – General
PPHT09 – Introduction
8
Course Goals – Practical • Understanding of a range of programming language constructs for concurrent programming
• Introduction to the problems common to many computing disciplines: ◦ Operating systems ◦ Distributed systems ◦ Real‐time systems
• Ability to apply these in practice to synchronisation problems in concurrent programming problems in concurrent programming
• Appreciation of the problems of concurrent programming
• Practical knowledge of the programming techniques of modern concurrent programming languages
◦ Classic synchronisation problems
PPHT09 – Introduction
9
Practical Information
PPHT09 – Introduction
10
Course Literature
• Two lectures per week • Optional weekly exercises
• Gregory R. Andrews ◦ Foundations of Multithreaded, Parallel, and Distributed Programming ◦ Main course book
◦ Optional but very useful as they often contain examples leading to the assignments
• Mordechai (Moti) Ben‐Ari
• Three programming assignments – “labs”
◦ Principles of Concurrent and Distributed Programming (Second edition) ◦ Recommended reading
◦ Supervision/helpers available in lab rooms
• Written Exam ◦ 4 hours ◦ closed book PPHT09 – Introduction
11
PPHT09 – Introduction
12
2
Course Communication
Gentle Start
• Web pages: intended to answer most basic questions
• Introduction to concurrent programming • Basic understanding
◦ http://www.cse.chalmers.se/edu/course/TDA381/ ◦ Tip: don’t search for JR, use local resources
◦ Concurrent programming concepts • Threads/Processes • State, Execution, Scheduling
◦ Synchronisation problems
• Small changes to assignments possible
• Introduction to programming languages ◦ Java ◦ JR
• E‐mail:
[email protected] PPHT09 – Introduction
13
PPHT09 – Introduction
Your Summer Job
14
Solution in JR import edu.ucdavis.jr.*; import javax.swing.*;
• Cremona decide to employ experts to increase sales. Their solution:
public class Main { public static void main(String[] args) { JFlash window = new JFlash("Cremona"); SwingUtilities.invokeLater(window); while (true) { window.flash("Buy @ Cremona!"); JR.nap(3000); } }
Buy @ Cremona ! • The message must be flashed every three seconds } PPHT09 – Introduction
15
PPHT09 – Introduction
Next Summer
16
Timeline
• The program does not increase sales as predicted. A psychologist is called in to help:
• The program is now more complex… Time 0
◦ An additional message is needed: the sign must flash “Free beer!” every 5 seconds
Buy… 3
Free… 5 6
Buy…
9
PPHT09 – Introduction
17
Buy… Free… 10 PPHT09 – Introduction
18
3
Revised Code
while ( true ) { if ( next_buy < next_beer) { JR.nap(next_buy); window.flash("Buy @ Cremona"); next_beer = next_beer - next_buy; next_buy = buy_pause; } else if ( next_buy > next_beer ) { JR.nap(next_beer); window.flash("Free Beer!"); next buy = next buy - next beer; next_buy next_buy next_beer; next_beer = beer_pause; } else { JR.nap(next_buy); window.flash("Buy @ Cremona - Free Beer!"); next_buy = buy_pause; next_beer = beer_pause; } //the same end
//the same init JFlash window = new JFlash("Cremona"); SwingUtilities.invokeLater(window); final int buy_pause buy pause = 3000; final int beer_pause = 5000; int next_buy = buy_pause; int next_beer = beer_pause; //continues on the next slide
PPHT09 – Introduction
19
Simple Concurrent Processes
20
Simple Concurrent Processes //some init
• A more natural solution is to run the two simple algorithms concurrently:
private process buy { while (true) { window.flash("Buy @ Cremona!"); JR.nap(buy_pause); }}
while (true) { window.flash("Buy @ Cremona!"); JR.nap(buy_pause); }
private process beer { while (true) { window.flash("Free Beer!"); JR.nap(beer_pause); }}
while (true) { window.flash("Free Beer!"); JR.nap(beer_pause); } PPHT09 – Introduction
PPHT09 – Introduction
//some end 21
Java Threads
PPHT09 – Introduction
22
Programming Threads • Providing thread run() method
• Java threads are a bit different from JR’s simple process declaration
◦ inheritance
◦ But there is more to processes in JR than that
class Buy extends Thread { //some init public void run() { while (true) { window.flash("Buy @ Cremona!"); //add napping here } } }
• Java threading framework ◦ The Thread class provides the API and generic behaviours ◦ A concrete thread must provide a run() method which is the code that the thread will execute when started
PPHT09 – Introduction
23
PPHT09 – Introduction
24
4
Programming Threads
Running Java Threads
• Providing thread run() method
• Invoking the run() method in a new thread ◦ Inheritance
◦ implement interface Runnable class Buy implements Runnable { //some init public void run() { while (true) { window.flash("Buy @ Cremona!"); //add napping here } } } PPHT09 – Introduction
buyThread = new Buy(…); buyThread.start(); y ();
◦ Interface buyThread = new Thread(new Buy(…)); buyThread.start();
25
PPHT09 – Introduction
Running Java Threads
Napping in Java
• Using anonymous inner classes
• A sleeping thread can be interrupted, hence the need for the catch/try clause.
buyThread = new Thread() { public void run() { while (true) { window.flash("Buy i d fl h("B @ C Cremona!"); !") //add napping here } } }; buyThread.start();
PPHT09 – Introduction
26
try { Thread.sleep(milliseconds); } catch (InterruptedException e) { //Panic: do something here! }
• More on this later. 27
Processes and Threads
PPHT09 – Introduction
28
Concurrent Programming Languages
• A JR process is similar to a Java thread
• Using concurrent programming languages we are going to ◦ Explore concurrency problems and solutions ◦ Understand how modern programming languages support concurrent programming i
• Terminological confusion: A multi‐threaded Java program and a multi‐process JR program both run as a single OS process.
• Main course programming languages
• More about this later
◦ JR ◦ Java ◦ Erlang PPHT09 – Introduction
29
PPHT09 – Introduction
30
5
Process Scheduling
Scheduling
• On a pure von Neumann system threads appear to run at the same time but in fact their execution must be interleaved P running
Q running
• The job of switching between threads is performed by the scheduler ◦ Part of the run‐time system, or ◦ Performed using the operating system’s processes and scheduler d h d l
Thread P
• Many different methods of scheduling exist
Thread Q
time PPHT09 – Introduction
31
Scheduling – Continued
PPHT09 – Introduction
Types of Process Behaviour • Independent processes
• Two extremes:
A Process
◦ Relatively rare; Rather uninteresting
◦ Cooperative scheduling • a thread runs until it is willing to release the processor (e.g. sleep, wait for I/O, or termination)
• Competing ◦ Typical in OS and yp networks, due to shared resources
◦ Pre‐emptive scheduling Pre emptive scheduling • a thread is interrupted in order to let other threads continue (e.g. time‐slicing)
• Cooperating
• Erlang has a pre‐emptive scheduler • Most modern JVM’s are also pre‐emptive
PPHT09 – Introduction
32
◦ Processes combine to solve a common task
33
Types of Process Behaviour
PPHT09 – Introduction
34
Types of Process Behaviour
• Designing concurrent systems is concerned with synchronisation and communication between processes • Independent processes
• Competing ◦ Typical in OS and networks, due to shared resources
◦ Relatively rare; Rather uninteresting
PPHT09 – Introduction
35
PPHT09 – Introduction
36
6
Types of Process Behaviour
Types of Process Behaviour
• Competing
• Competing
◦ Typical in OS and networks, due to shared resources
◦ Typical in OS and networks, due to shared resources
Deadlock
Starvation
PPHT09 – Introduction
37
PPHT09 – Introduction
Types of Process Behaviour
38
Atomicity
• Cooperating
• An atomic action is something that is guaranteed to execute without interruption • Since the execution of different threads is interleaved, what are the atomic actions?
◦ Processes combine to solve a common task ◦ Synchronisation
◦ Single instructions? ◦ Basic code blocks? ◦ Answer: might not be specified by the language design. We have to assume the worst! Context switch can occur anywhere, also in the middle of a statement. PPHT09 – Introduction
39
Atomicity
PPHT09 – Introduction
40
Example: The Liseberg Counter
• What if flash is not atomic for the Cremona display?
• How many people are in Liseberg at any given time? ◦ Each entrance has turnstiles which record when a person enters or leaves:
while (true) { window.flash("Buy @ Cremona!"); JR.nap(buy_pause); } while (true) { window.flash(“Free Beer!"); JR.nap(beer_pause); } PPHT09 – Introduction
East Gate
41
counter
West Gate
PPHT09 – Introduction
42
7
Simulation
Simulation – Quiescence
private int counter = 0; private enum Dir {East, West}; public void enter() {
counter++;
//constructor public Main() { try { JR.registerQuiescenceAction(done); } catch (QuiescenceRegistrationException e) { e.printStackTrace(); } }
}
public process Turnstile((Dir i : Dir.values())) { for(int j = 0; j