Chapter 2. Processes & Threads. Concurrency: processes & threads 1. Magee/Kramer

Similar documents
Processes & Threads. Concepts: processes - units of sequential execution.

CSCI 5828: Foundations of Software Engineering

Concurrent processes. Processes and Threads. Processes and threads. Going back to Concurrency. Modelling Processes. Modeling processes

Lecture 2: Modeling Introduction

Concurrent processes. Processes and Threads. A question asked by your friend. Warm-up exercises at beginning of Lecture 3. My question for you

Model Requirements and JAVA Programs MVP 2 1

COMP30112: Concurrency Topics 4.1: Concurrency Patterns - Monitors

Verification Finite-state process modeling and reachability analysis

2. Concurrency and Java

Introduction to modelling in FSP (Finite State Processes) and its application for analysis of Java Mutex problems

Lecture 7: Process & Thread Introduction

Shared Objects & Mutual Exclusion

Answer the first question and two further questions. i. action prefix ( -> ) [3 marks]

Communications Software Engineering Condition Synchronization and Liveness

Monitors & Condition Synchronization

Final Exam -- ELG 7187C

CSCI 5828: Foundations of Software Engineering

LTSA User Manual.

Q1. From post-it note. Question asked after Prev. Lecture. thread life-cycle in Java. Q2. Concurrency and parallelism. Definitions

Reintroduction to Concurrency

C340 Concurrency: Semaphores and Monitors. Goals

Concurrency. State Models and Java Programs. Jeff Magee and Jeff Kramer. Concurrency: introduction 1. Magee/Kramer

FSP Language Specification

Lecture 9: Introduction to Monitors

Concurrency in Java Prof. Stephen A. Edwards

Communications Software Engineering Analysis Model

CIS233J Java Programming II. Threads

Threads. Definitions. Process Creation. Process. Thread Example. Thread. From Volume II

Lecture 10: Introduction to Semaphores

Concurrency - Topics. Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads

COMP31212: Concurrency A Review of Java Concurrency. Giles Reger

CONCURRENT PROGRAMMING - EXAM

CSCI 5828: Foundations of Software Engineering

Shared Objects & Mutual Exclusion

Monitors & Condition Synchronization

Contents. 6-1 Copyright (c) N. Afshartous

Safety & Liveness Properties

Concurrent Execution

Chapter Machine instruction level 2. High-level language statement level 3. Unit level 4. Program level

Multithreaded Programming

CS455: Introduction to Distributed Systems [Spring 2019] Dept. Of Computer Science, Colorado State University

CS 455: INTRODUCTION TO DISTRIBUTED SYSTEMS [THREADS] Frequently asked questions from the previous class survey

G52CON: Concepts of Concurrency

Shared Objects & Mutual Exclusion

THE QUEEN S UNIVERSITY OF BELFAST

CMSC 132: Object-Oriented Programming II. Threads in Java

CPSC/ECE 3220 Fall 2017 Exam Give the definition (note: not the roles) for an operating system as stated in the textbook. (2 pts.

Concurrent Programming

Java Threads. Written by John Bell for CS 342, Spring 2018

JAVA and J2EE UNIT - 4 Multithreaded Programming And Event Handling

TIETS14 Introduction to Formal Specification

Multi-threaded programming in Java

Monitors & Condition Synchronisation

7. MULTITHREDED PROGRAMMING

Multithread Computing

Process Characteristics. Threads Chapter 4. Process Characteristics. Multithreading vs. Single threading

Threads Chapter 4. Reading: 4.1,4.4, 4.5

Monitors & Condition Synchronization

Computation Abstractions. Processes vs. Threads. So, What Is a Thread? CMSC 433 Programming Language Technologies and Paradigms Spring 2007

Concurrency: State Models & Design Patterns

Java Threads. COMP 585 Noteset #2 1

Chapter 13 Topics. Introduction. Introduction

Topic 7: Algebraic Data Types

Threads. Still Chapter 2 (Based on Silberchatz s text and Nachos Roadmap.) 3/9/2003 B.Ramamurthy 1

CMSC 433 Programming Language Technologies and Paradigms. Concurrency

Multithreaded Programming

Synchronization

The Java Memory Model

AC OB S. Multi-threaded FW framework (OS) for embedded ARM systems Torsten Jaekel, June 2014

CPS 506 Comparative Programming Languages. Programming Language Paradigms

CMSC 330: Organization of Programming Languages

Quiz on Tuesday April 13. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 4. Java facts and questions. Things to try in Java

Techniques of Java Programming: Concurrent Programming in Java

Overview. CMSC 330: Organization of Programming Languages. Concurrency. Multiprocessors. Processes vs. Threads. Computation Abstractions

Concurrent Programming using Threads

Basics of. Multithreading in Java

Java Threads and intrinsic locks

3C03 Concurrency: Starvation and Deadlocks

Advanced Flow of Control -- Introduction

Multithreaded Programming

G52CON: Concepts of Concurrency

Threads Questions Important Questions

Processes and Threads

Recursion. Chapter 7. Copyright 2012 by Pearson Education, Inc. All rights reserved

Multithreading Pearson Education, Inc. All rights reserved.

CSCI 136 Written Exam #2 Fundamentals of Computer Science II Spring 2015

Chapter 13. Concurrency ISBN

Multitasking Multitasking allows several activities to occur concurrently on the computer. A distinction is usually made between: Process-based multit

CSCI 5828: Foundations of Software Engineering

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

Multithreaded Programming Part II. CSE 219 Stony Brook University, Department of Computer Science

Parallel Programming Languages COMP360

Concurrency COMS W4115. Prof. Stephen A. Edwards Spring 2002 Columbia University Department of Computer Science

Runtime assertion checking of multithreaded Java programs

Implementing Coroutines. Faking Coroutines in Java

VALIDATION LED DEVELOPMENT OF OBJECT-ORIENTED SOFTWARE USING A MODEL VERIFIER

Linked List Implementation of Queues

Review: Program Execution. Memory program code program data program stack containing procedure activation records

Programming Language Concepts: Lecture 11

Definition: A thread is a single sequential flow of control within a program.

Transcription:

Chapter 2 Processes & Threads Concurrency: processes & threads 1

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. Concept of a process as a sequence of actions. Model processes as finite state machines. Program processes as threads in Java. Concurrency: processes & threads 2

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 & threads 3

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 & threads 4

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. on 0 1 a light switch LTS off on off on off on off. a sequence of actions or trace Concurrency: processes & threads 5

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). once 0 1 ONESHOT state machine (terminating process) Convention: actions begin with lowercase letters PROCESSES begin with uppercase letters Concurrency: processes & threads 6

FSP - action prefix & recursion Repetitive behaviour uses recursion: on SWITCH = OFF, OFF = (on -> ON), ON = (off-> OFF). Substituting to get a more succinct definition: SWITCH = OFF, OFF = (on ->(off->off)). And again: SWITCH = (on->off->switch). 0 1 Concurrency: processes & threads 7 off

animation using LTSA The LTSA animator can be used to produce a trace. Ticked actions are eligible for selection. In the LTS, the last action is highlighted in red. on 0 1 Concurrency: processes & threads 8 off

FSP - action prefix FSP model of a traffic light : TRAFFICLIGHT = (red->orange->green->orange -> TRAFFICLIGHT). LTS generated using LTSA: red orange green 0 1 2 3 Trace: orange red orange green orange red orange green Concurrency: processes & threads 9

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 & threads 10

FSP - choice FSP model of a drinks machine : DRINKS = (red->coffee->drinks blue->tea->drinks ). LTS generated using LTSA: red blue 0 1 2 Possible traces? coffee tea Concurrency: processes & threads 11

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), toss TAILS= (tails->coin). Tossing a coin. toss 0 1 2 Possible traces? heads Concurrency: processes & threads tails 12

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... in 0 1 in CHAN = (in->chan in->out->chan ). out Concurrency: processes & threads 13

FSP - indexed processes and actions Single slot buffer that inputs a value in the range 0 to 3 and then outputs that value: BUFF = (in[i:0..3]->out[i]-> BUFF). equivalent to BUFF = (in[0]->out[0]->buff in[1]->out[1]->buff in[2]->out[2]->buff in[3]->out[3]->buff ). or using a process parameter with default value: BUFF(N=3) = (in[i:0..n]->out[i]-> BUFF). Concurrency: processes & threads 14

FSP - constant & range declaration index expressions to model calculation: in.0.0 in.1.0 in.0.1 in.1.1 0 1 2 3 const N = 1 range T = 0..N range R = 0..2*N out.0 SUM = (in[a:t][b:t]->total[a+b]), TOTAL[s:R] = (out[s]->sum). Concurrency: processes & threads 15 out.1 out.2

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<n) inc->count[i+1] when(i>0) dec->count[i-1] ). inc dec inc Concurrency: processes & threads 16 inc 0 1 2 3 dec dec

FSP - guarded actions A countdown timer which beeps after N ticks, or can be stopped. COUNTDOWN (N=3) = (start->countdown[n]), COUNTDOWN[i:0..N] = (when(i>0) tick->countdown[i-1] when(i==0)beep->stop stop stop->stop ). stop stop start tick tick tick stop beep 0 1 2 3 4 5 Concurrency: processes & threads 17

FSP - guarded actions What is the following FSP process equivalent to? const False = 0 P = (when (False) doanything->p). Answer: STOP Concurrency: processes & threads 18

FSP - process alphabets The alphabet of a process is the set of actions in which it can engage. Alphabet extension can be used to extend the implicit alphabet of a process: WRITER = (write[1]->write[3]->writer) +{write[0..3]}. Alphabet of WRITER is the set {write[0..3]} (we make use of alphabet extensions in later chapters) Concurrency: processes & threads 19

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 & threads 20

Implementing processes - the OS view OS Process Data Code Descriptor Stack Descriptor Stack Descriptor Stack Descriptor Thread 1 Thread 2 Thread n 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 & threads 21

threads in Java A Thread class manages a single sequential thread of control. Threads may be created and deleted dynamically. Thread 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. MyThread run() class MyThread extends Thread { public void run() { //... } } Concurrency: processes & threads 22

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 target Thread run() MyRun run() public interface Runnable { public abstract void run(); } class MyRun implements Runnable{ public void run() { //... } } Concurrency: processes & threads 23

thread life-cycle in Java An overview of the life-cycle of a thread as state transitions: new Thread() Created start() causes the thread to call its run() method. start() Alive stop() stop(), or run() returns 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). Terminated Concurrency: processes & threads 24

thread alive states in Java Once started, an alive thread has a number of substates : start() Running sleep() suspend() yield() dispatch Runnable suspend() resume() Non-Runnable wait() also makes a Thread Non-Runnable, and notify() Runnable (used in later chapters). stop(), or run() returns Concurrency: processes & threads 25

Java thread lifecycle - an FSP specification THREAD = CREATED, CREATED = (start ->RUNNING 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 & threads 26

Java thread lifecycle - an FSP specification start yield stop sleep suspend resume 0 1 2 3 4 run end, run, dispatch are not methods of class Thread. stop end stop dispatch suspend States 0 to 4 correspond to CREATED, TERMINATED, RUNNING, NON-RUNNABLE, and RUNNABLE respectively. Concurrency: processes & threads 27 stop

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 & threads 28

CountDown timer - class diagram Applet Runnable The class NumberCanvas provides the display canvas. CountDown init() start() stop() run() tick() beep() counter display target Thread NumberCanvas setvalue() The class CountDown derives from Applet and contains the implementation of the run() method which is required by Thread. Concurrency: processes & threads 29

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 & threads 30

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 & threads 31

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, nonrunnable, terminated. Concurrency: processes & threads 32