Concurrency: processes & threads1 ©Magee/Kramer 2 nd Edition Chapter 2 Processes & Threads.

Slides:



Advertisements
Similar presentations
1 Multithreaded Programming in Java. 2 Agenda Introduction Thread Applications Defining Threads Java Threads and States Examples.
Advertisements

Concepts & Notations. Acknowledgements  The material in this tutorial is based in part on: Concurrency: State Models & Java Programming, by Jeff Magee.
Concurrent Programming Abstraction & Java Threads
Concurrency: introduction1 ©Magee/Kramer 2 nd Edition Concurrency State Models and Java Programs Jeff Magee and Jeff Kramer.
Multi-threaded applications SE SE-2811 Dr. Mark L. Hornick 2 What SE1011 students are told… When the main() method is called, the instructions.
CSC321 §6 Modelling Processes using FSP 1 Section 6 Modelling Processes using FSP.
Concurrency: safety & liveness properties1 ©Magee/Kramer 2 nd Edition COMP60611 Fundamentals of Parallel and Distributed Systems Lecture 14 Safety and.
1 CSC321 §2 Concurrent Programming Abstraction & Java Threads Section 2 Concurrent Programming Abstraction & Java Threads.
1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.
2. Java and Concurrency Prof. O. Nierstrasz Selected material © Magee and Kramer.
Algorithm Programming Concurrent Programming in Java Bar-Ilan University תשס"ח Moshe Fresko.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L19 (Chapter 24) Multithreading.
CP — Concurrent Programming 2. Java and Concurrency Prof. O. Nierstrasz Wintersemester 2005 / 2006.
Concurrency ©Magee/Kramer February 05, 2007 Claus Brabrand University of Aarhus Concurrency Spring 2007.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
Ceng Operating Systems Chapter 2.1 : Processes Process concept Process scheduling Interprocess communication Deadlocks Threads.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Threads Chapter 4 Reading: 4.1,4.4, Process Characteristics l Unit of resource ownership - process is allocated: n a virtual address space to.
50.003: Elements of Software Construction Week 5 Basics of Threads.
Concurrency: shared objects & mutual exclusion1 ©Magee/Kramer 2 nd Edition Chapter 4 Shared Objects & Mutual Exclusion.
9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC Lecture 8 (Multiple) Threads Semantics.
Concurrency: introduction1 ©Magee/Kramer Concurrency State Models and Java Programs Jeff Magee and Jeff Kramer.
Chapter 15 Multithreading F Threads Concept  Creating Threads by Extending the Thread class  Creating Threads by Implementing the Runnable Interface.
Java Programming: Advanced Topics
Concurrency: processes & threads1 ©Magee/Kramer Chapter 2 Processes & Threads.
Lecture 5 : JAVA Thread Programming Courtesy : MIT Prof. Amarasinghe and Dr. Rabbah’s course note.
Definitions & Scenarios. Acknowledgements  This tutorial is based in part on Concurrency: State Models & Java Programming by Jeff Magee and Jeff Kramer.
Concurrency: introduction1 ©Magee/Kramer 2 nd Edition COMP60611 Fundamentals of Parallel and Distributed Systems Lecture 5 Introduction to Concurrency:
Multi-Threaded Application CSNB534 Asma Shakil. Overview Software applications employ a strategy called multi- threaded programming to split tasks into.
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
© 2004, D. J. Foreman 2-1 Concurrency, Processes and Threads.
Computer Engineering Rabie A. Ramadan Lecture 8. Agenda 2 Introduction Thread Applications Defining Threads Java Threads and States Priorities Accessing.
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
2015 Concurrency: processes & threads 1 ©Magee/Kramer 2 nd Edition Chapter 2 Processes & Threads.
CSC321 §6 Modelling Processes using FSP 1 Chapter 6 Modelling Processes using FSP.
1 Chapter 2.1 : Processes Process concept Process concept Process scheduling Process scheduling Interprocess communication Interprocess communication Threads.
1 Web Based Programming Section 8 James King 12 August 2003.
Internet Security CSCE 813 Communicating Sequential Processes.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
Chapter 6 (6.7 & 6.9 only) - Threads & Mutex Threads –A Thread is a basic unit of CPU utilization consisting of a program counter, register set and stack.
Formal Methods for Software Engineering Part II: Modelling & Analysis of System Behaviour.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
Multithreading in JAVA
2015 Concurrency: logical properties 1 ©Magee/Kramer 2 nd Edition Chapter 14 Logical Properties Satisfied? Not satisfied?
Multi-Threading in Java
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
13-1 Chapter 13 Concurrency Topics Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads C# Threads.
55:032 - Intro. to Digital DesignPage 1 VHDL and Processes Defining Sequential Circuit Behavior.
1 Model-Checking of Component-Based Real-Time Embedded Software Based on CORBA Event Service Yuanfang Zhang for Seminar CSE7216 Presentation based on Zonghua.
Threads b A thread is a flow of control in a program. b The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.
Chapter 13: Multithreading The Thread class The Thread class The Runnable Interface The Runnable Interface Thread States Thread States Thread Priority.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Concurrent Programming in JAVA Steve Pruett - Introduction Jaime Mendez – Thread Creation Adrian Garcia – Thread Management Eric Orozco – Thread Safety.
Chapter 4 – Thread Concepts
Formal Methods for Software Engineering
Chapter 13: Multithreading
Concurrency, Processes and Threads
Chapter 4 – Thread Concepts
Multithreaded Programming in Java
Introduction To Flowcharting
Multithreaded Programming in Java
Multithreading.
Multithreaded Programming
Concurrency, Processes and Threads
Chapter 15 Multithreading
Multithreading in java.
Concurrency, Processes and Threads
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Concurrency: processes & threads1 ©Magee/Kramer 2 nd Edition Chapter 2 Processes & Threads

Concurrency: processes & threads2 ©Magee/Kramer 2 nd Edition concurrent processes We structure complex systems as sets of simpler activities, each represented as a sequential process. Processes can overlap or be concurrent, so as to reflect the concurrency inherent in the physical world, or to offload time-consuming tasks, or to manage communications or other devices. Designing concurrent software can be complex and error prone. A rigorous engineering approach is essential. Model processes as finite state machines. Program processes as threads in Java. Concept of a process as a sequence of actions.

Concurrency: processes & threads3 ©Magee/Kramer 2 nd Edition processes and threads Concepts : processes - units of sequential execution. Models : finite state processes (FSP) to model processes as sequences of actions. labelled transition systems (LTS) to analyse, display and animate behavior. Practice : Java threads

Concurrency: processes & threads4 ©Magee/Kramer 2 nd Edition 2.1 Modeling Processes Models are described using state machines, known as Labelled Transition Systems LTS. These are described textually as finite state processes (FSP) and displayed and analysed by the LTSA analysis tool.  LTS - graphical form  FSP - algebraic form

Concurrency: processes & threads5 ©Magee/Kramer 2 nd Edition modeling processes A process is the execution of a sequential program. It is modeled as a finite state machine which transits from state to state by executing a sequence of atomic actions. a light switch LTS on  off  on  off  on  off  ………. a sequence of actions or trace Can finite state models produce infinite traces?

Concurrency: processes & threads6 ©Magee/Kramer 2 nd Edition FSP - action prefix If x is an action and P a process then (x-> P) describes a process that initially engages in the action x and then behaves exactly as described by P. ONESHOT = (once -> STOP). ONESHOT state machine (terminating process) Convention: actions begin with lowercase letters PROCESSES begin with uppercase letters

Concurrency: processes & threads7 ©Magee/Kramer 2 nd Edition FSP - action prefix & recursion SWITCH = OFF, OFF = (on -> ON), ON = (off-> OFF). Repetitive behaviour uses recursion: Substituting to get a more succinct definition: SWITCH = OFF, OFF = (on ->(off->OFF)). And again: SWITCH = (on->off->SWITCH).

Concurrency: processes & threads8 ©Magee/Kramer 2 nd Edition animation using LTSA Ticked actions are eligible for selection. In the LTS, the last action is highlighted in red. The LTSA animator can be used to produce a trace.

Concurrency: processes & threads9 ©Magee/Kramer 2 nd Edition FSP - action prefix TRAFFICLIGHT = (red->orange->green->orange -> TRAFFICLIGHT). LTS generated using LTSA: Trace: FSP model of a traffic light : red  orange  green  orange  red  orange  green …

Concurrency: processes & threads10 ©Magee/Kramer 2 nd Edition FSP - choice If x and y are actions then (x-> P | y-> Q) describes a process which initially engages in either of the actions x or y. After the first action has occurred, the subsequent behavior is described by P if the first action was x and Q if the first action was y. Who or what makes the choice? Is there a difference between input and output actions?

Concurrency: processes & threads11 ©Magee/Kramer 2 nd Edition FSP - choice DRINKS = (red->coffee->DRINKS |blue->tea->DRINKS ). LTS generated using LTSA: Possible traces? FSP model of a drinks machine :

Concurrency: processes & threads12 ©Magee/Kramer 2 nd Edition Non-deterministic choice Process (x-> P | x -> Q) describes a process which engages in x and then behaves as either P or Q. COIN = (toss->HEADS|toss->TAILS), HEADS= (heads->COIN), TAILS= (tails->COIN). Tossing a coin. Possible traces?

Concurrency: processes & threads13 ©Magee/Kramer 2 nd Edition Modeling failure How do we model an unreliable communication channel which accepts in actions and if a failure occurs produces no output, otherwise performs an out action? Use non-determinism... CHAN = (in->CHAN |in->out->CHAN ).

Concurrency: processes & threads14 ©Magee/Kramer 2 nd Edition Single slot buffer that inputs a value in the range 0 to 3 and then outputs that value: FSP - indexed processes and actions BUFF = (in[i:0..3]->out[i]-> BUFF). equivalent to or using a process parameter with default value: BUFF = (in[0]->out[0]->BUFF |in[1]->out[1]->BUFF |in[2]->out[2]->BUFF |in[3]->out[3]->BUFF ). BUFF(N=3) = (in[i:0..N]->out[i]-> BUFF). indexed actions generate labels of the form action. index

Concurrency: processes & threads15 ©Magee/Kramer 2 nd Edition const N = 1 range T = 0..N range R = 0..2*N SUM = (in[a:T][b:T]->TOTAL[a+b]), TOTAL[s:R] = (out[s]->SUM). index expressions to model calculation: FSP - indexed processes and actions Local indexed process definitions are equivalent to process definitions for each index value

Concurrency: processes & threads16 ©Magee/Kramer 2 nd Edition FSP - guarded actions The choice (when B x -> P | y -> Q) means that when the guard B is true then the actions x and y are both eligible to be chosen, otherwise if B is false then the action x cannot be chosen. COUNT (N=3) = COUNT[0], COUNT[i:0..N] = (when(i COUNT[i+1] |when(i>0) dec->COUNT[i-1] ).

Concurrency: processes & threads17 ©Magee/Kramer 2 nd Edition FSP - guarded actions COUNTDOWN (N=3) = (start->COUNTDOWN[N]), COUNTDOWN[i:0..N] = (when(i>0) tick->COUNTDOWN[i-1] |when(i==0)beep->STOP |stop->STOP ). A countdown timer which beeps after N ticks, or can be stopped.

Concurrency: processes & threads18 ©Magee/Kramer 2 nd Edition FSP - guarded actions What is the following FSP process equivalent to? const False = 0 P = (when (False) doanything->P). Answer: STOP

Concurrency: processes & threads19 ©Magee/Kramer 2 nd Edition FSP - process alphabets The alphabet of a process is the set of actions in which it can engage. Process alphabets are implicitly defined by the actions in the process definition. The alphabet of a process can be displayed using the LTSA alphabet window. Process: COUNTDOWN Alphabet: { beep, start, stop, tick }

Concurrency: processes & threads20 ©Magee/Kramer 2 nd Edition FSP - process alphabet extension Alphabet extension can be used to extend the implicit alphabet of a process: Alphabet of WRITER is the set {write[0..3]} (we make use of alphabet extensions in later chapters) WRITER = (write[1]->write[3]->WRITER) +{write[0..3]}.

Concurrency: processes & threads21 ©Magee/Kramer 2 nd Edition Revision & Wake-up Exercise In FSP, model a process FILTER, that exhibits the following repetitive behavior: inputs a value v between 0 and 5, but only outputs it if v <= 2, otherwise it discards it. FILTER = (in[v:0..5] -> DECIDE[v]), DECIDE[v:0..5] = ( ? ).

Concurrency: processes & threads22 ©Magee/Kramer 2 nd Edition 2.2 Implementing processes Modeling processes as finite state machines using FSP/LTS. Implementing threads in Java. Note: to avoid confusion, we use the term process when referring to the models, and thread when referring to the implementation in Java.

Concurrency: processes & threads23 ©Magee/Kramer 2 nd Edition Implementing processes - the OS view A (heavyweight) process in an operating system is represented by its code, data and the state of the machine registers, given in a descriptor. In order to support multiple (lightweight) threads of control, it has multiple stacks, one for each thread.

Concurrency: processes & threads24 ©Magee/Kramer 2 nd Edition threads in Java A Thread class manages a single sequential thread of control. Threads may be created and deleted dynamically. Thread run() MyThread run() The Thread class executes instructions from its method run(). The actual code executed depends on the implementation provided for run() in a derived class. class MyThread extends Thread { public void run() { // } Creating a thread object: Thread a = new MyThread();

Concurrency: processes & threads25 ©Magee/Kramer 2 nd Edition threads in Java Since Java does not permit multiple inheritance, we often implement the run() method in a class not derived from Thread but from the interface Runnable. Runnable run() MyRun run() public interface Runnable { public abstract void run(); } class MyRunimplements Runnable{ public void run() { //..... } } Thread target Creating a thread object: Thread b = new Thread(new MyRun());

Concurrency: processes & threads26 ©Magee/Kramer 2 nd Edition thread life-cycle in Java An overview of the life-cycle of a thread as state transitions: CreatedAlive Terminated new Thread() start() stop(), or run() returns stop() The predicate isAlive() can be used to test if a thread has been started but not terminated. Once terminated, it cannot be restarted (cf. mortals). start() causes the thread to call its run() method.

Concurrency: processes & threads27 ©Magee/Kramer 2 nd Edition thread alive states in Java Once started, an alive thread has a number of substates : RunnableNon-Runnable suspend() resume() yield() Running dispatch suspend() start() stop(), or run() returns Also, wait() makes a Thread Non-Runnable, and notify() makes it Runnable (used in later chapters). sleep() Alive

Concurrency: processes & threads28 ©Magee/Kramer 2 nd Edition Java thread lifecycle - an FSP specification THREAD = CREATED, CREATED = (start ->RUNNABLE |stop ->TERMINATED), RUNNING = ({suspend,sleep}->NON_RUNNABLE |yield ->RUNNABLE |{stop,end} ->TERMINATED |run ->RUNNING), RUNNABLE = (suspend ->NON_RUNNABLE |dispatch ->RUNNING |stop ->TERMINATED), NON_RUNNABLE = (resume ->RUNNABLE |stop ->TERMINATED), TERMINATED = STOP.

Concurrency: processes & threads29 ©Magee/Kramer 2 nd Edition Java thread lifecycle - an FSP specification end, run, dispatch are not methods of class Thread. States 0 to 4 correspond to CREATED, TERMINATED, RUNNABLE, RUNNING, and NON-RUNNABLE respectively.

Concurrency: processes & threads30 ©Magee/Kramer 2 nd Edition CountDown timer example COUNTDOWN (N=3) = (start->COUNTDOWN[N]), COUNTDOWN[i:0..N] = (when(i>0) tick->COUNTDOWN[i-1] |when(i==0)beep->STOP |stop->STOP ). Implementation in Java?

Concurrency: processes & threads31 ©Magee/Kramer 2 nd Edition CountDown timer - class diagram The class CountDown derives from Applet and contains the implementation of the run() method which is required by Thread. The class NumberCanvas provides the display canvas.

Concurrency: processes & threads32 ©Magee/Kramer 2 nd Edition CountDown class public class CountDown extends Applet implements Runnable { Thread counter; int i; final static int N = 10; AudioClip beepSound, tickSound; NumberCanvas display; public void init() {...} public void start() {...} public void stop() {...} public void run() {...} private void tick() {...} private void beep() {...} }

Concurrency: processes & threads33 ©Magee/Kramer 2 nd Edition CountDown class - start(), stop() and run() public void start() { counter = new Thread(this); i = N; counter.start(); } public void stop() { counter = null; } public void run() { while(true) { if (counter == null) return; if (i>0) { tick(); --i; } if (i==0) { beep(); return;} } COUNTDOWN Model start -> stop -> COUNTDOWN[i] process recursion as a while loop STOP when(i>0) tick -> CD[i-1] when(i==0)beep -> STOP STOP when run() returns

Concurrency: processes & threads34 ©Magee/Kramer 2 nd Edition CountDown counter thread start() new Thread(this) target.run() created counter.start() alive terminated init() tick() beep() CountDown execution

Concurrency: processes & threads35 ©Magee/Kramer 2 nd Edition CountDown counter thread stop() new Thread(this) target.run() created counter.start() counter=null alive terminated tick() CountDown execution start() init()

Concurrency: processes & threads36 ©Magee/Kramer 2 nd Edition Summary  Concepts process - unit of concurrency, execution of a program  Models LTS to model processes as state machines - sequences of atomic actions FSP to specify processes using prefix “->”, choice ” | ” and recursion.  Practice Java threads to implement processes. Thread lifecycle - created, running, runnable, non- runnable, terminated.