Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 17 Concurrent Programming Two roads diverged.

Slides:



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

Chapter 6: Process Synchronization
5.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 5: CPU Scheduling.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
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.
Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 91 Today’s class Mutual exclusion and synchronization 
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.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
6/16/2015 Chapter Eight Process Synchronisation. Index Objectives Concurrent processes and Asynchronous concurrent processes Process synchronisation Mutual.
Chapter 11: Distributed Processing Parallel programming Principles of parallel programming languages Concurrent execution –Programming constructs –Guarded.
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Real-Time Kernels and Operating Systems. Operating System: Software that coordinates multiple tasks in processor, including peripheral interfacing Types.
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.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
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.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
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.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
1 Concurrent Languages – Part 1 COMP 640 Programming Languages.
1 Processes, Threads, Race Conditions & Deadlocks Operating Systems Review.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Concurrent Programming. Concurrency  Concurrency means for a program to have multiple paths of execution running at (almost) the same time. Examples:
SEMAPHORE By: Wilson Lee. Concurrency Task Synchronization Example of semaphore Language Support.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
In Java processes are called threads. Additional threads are associated with objects. An application is associated with an initial thread via a static.
Java Thread and Memory Model
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C H A P T E R E L E V E N Concurrent Programming.
ProgrammingLanguages Programming Languages Parallel Programming languages.
Lecture 6: Monitors & Semaphores. Monitor Contains data and procedures needed to allocate shared resources Accessible only within the monitor No way for.
13-1 Chapter 13 Concurrency Topics Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads C# Threads.
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
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.
Processes. Process Concept Process Scheduling Operations on Processes Interprocess Communication Communication in Client-Server Systems.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Comunication&Synchronization threads 1 Programación Concurrente Benemérita Universidad Autónoma de Puebla Facultad de Ciencias de la Computación Comunicación.
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Operating Systems CMPSC 473 Signals, Introduction to mutual exclusion September 28, Lecture 9 Instructor: Bhuvan Urgaonkar.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Background on the need for Synchronization
CS533 Concepts of Operating Systems Class 3
Chapter 5: Process Synchronization
Monitors Chapter 7.
Midterm review: closed book multiple choice chapters 1 to 9
Process Synchronization
Concurrent Programming
Background and Motivation
Monitors Chapter 7.
Subject : T0152 – Programming Language Concept
CS533 Concepts of Operating Systems Class 3
Monitors Chapter 7.
Outline Chapter 2 (cont) Chapter 3: Processes Virtual machines
Chapter 3: Process Concept
Chapter 6 – Distributed Processing and File Systems
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 17 Concurrent Programming Two roads diverged in a yellow wood, And sorry I could not travel both... Robert Frost, The Road Not Taken

Copyright © 2006 The McGraw-Hill Companies, Inc. Contents 17.1 Concurrency Concepts 17.2 Synchronization Strategies 17.3 Synchronization in Java 17.4 Interprocess Communication 17.5 Concurrency in Other Languages

Copyright © 2006 The McGraw-Hill Companies, Inc. Concurrency occurs at many levels More realistic Can be more efficient Carries unique, fundamental complexities Traditionally studied in the context of operating systems Example: client-server application such as web browsing

Copyright © 2006 The McGraw-Hill Companies, Inc Concurrency Concepts Example: web browser rendering a page Page is a shared resource Thread for each image load Thread for text rendering Cannot all write to page simultaneously Thread for user input; e.g., Stop button

Copyright © 2006 The McGraw-Hill Companies, Inc. Multiprogramming: several programs loaded into memory and executed in an interleaved manner Scheduler: switches from one program or thread to another Time-sharing: allow multiple users to communicate with a computer simultaneously Process: an execution context, including registers, activation stack, next instruction to be executed, etc.

Copyright © 2006 The McGraw-Hill Companies, Inc. Defn: A concurrent program is a program designed to have two or more execution contexts. Such a program is said to be multithreaded, since more than one execution context can be active simultaneously. Parallel program: 2 or more threads simultaneously active. Distributed program: designed so that different pieces are on computers connected by a network. Concurrency: a program with multiple, active threads. Single threaded: all earlier programs.

Copyright © 2006 The McGraw-Hill Companies, Inc. States of a thread: 1.Created: but not yet ready to run 2.Runnable: ready to run; awaiting a processor 3.Running: executing 4.Blocked: waiting on some resource 5.Terminated: stopped

Copyright © 2006 The McGraw-Hill Companies, Inc. States of a Thread Figure 17.1

Copyright © 2006 The McGraw-Hill Companies, Inc. Inter-thread communication needs to occur: 1.Thread requires exclusive access to some resource 2.Thread needs to exchange data with another thread Can communicate via: 1.Shared variables 2.Message passing 3.Parameters

Copyright © 2006 The McGraw-Hill Companies, Inc. A race condition occurs when the resulting value of a variable depends on the execution order of two or more threads. Ex: c = c + 1 Machine level: 1.load c 2.add 1 3.store c If c initially 0: With 2 threads, can get 1 or 2 With n threads, can get 1, 2,..., n

Copyright © 2006 The McGraw-Hill Companies, Inc. A deadlock occurs when a thread is waiting for an event that will never happen. Necessary conditions for a deadlock to exist: 1.Threads claim exclusive access to resources. 2.Threads hold some resources while waiting for others. 3.Resources may not be removed from waiting threads (preemption). 4.A circular chain of threads exists in which each thread holds a resource needed by the next thread in the chain.

Copyright © 2006 The McGraw-Hill Companies, Inc. Semaphores A semaphore is an integer variable and an associated thread queue. Operations: P(s) -- if s > 0 then s-- else enqueue thread V(s) -- if a thread is enqueued then dequeue it else s++ Binary semaphore Counting semaphore

Copyright © 2006 The McGraw-Hill Companies, Inc. program SimpleProducerConsumer; var buffer : string; full : semaphore = 0; empty : semaphore = 1;... begin cobegin Producer; Consumer; coend; end.

Copyright © 2006 The McGraw-Hill Companies, Inc. procedure Producer; var tmp : string begin while (true) do begin produce(tmp); P(empty); { begin critical section } buffer := tmp; V(full); { end critical section } end;

Copyright © 2006 The McGraw-Hill Companies, Inc. procedure Consumer; var tmp : string begin while (true) do begin P(full); { begin critical section } tmp := buffer; V(empty); { end critical section } consume(tmp); end;

Copyright © 2006 The McGraw-Hill Companies, Inc. program ProducerConsumer; const size = 5; var buffer : array[1..size] of string; inn : integer = 0; out : integer = 0; lock : semaphore = 1; nonfull : semaphore = size; nonempty : semaphore = 0;...

Copyright © 2006 The McGraw-Hill Companies, Inc. procedure Producer; var tmp : string begin while (true) do begin produce(tmp); P(nonfull); P(lock); { begin critical section } inn := inn mod size + 1; buffer[inn] := tmp; V(lock); { end critical section } V(nonempty); end;

Copyright © 2006 The McGraw-Hill Companies, Inc. procedure Consumer; var tmp : string begin while (true) do begin P(nonempty); P(lock); { begin critical section } out = out mod size + 1; tmp := buffer[out]; V(lock); { end critical section } V(nonfull); consume(tmp); end;

Copyright © 2006 The McGraw-Hill Companies, Inc. Monitors Encapsulates a shared resource together with access functions. Locking is automatic. Condition -- thread queue signal wait

Copyright © 2006 The McGraw-Hill Companies, Inc. monitor Buffer; const size = 5; var buffer : array[1..size] of string; in : integer = 0; out : integer = 0; count : integer = 0; nonfull : condition; nonempty : condition;

Copyright © 2006 The McGraw-Hill Companies, Inc. procedure put(s : string); begin if (count = size) then wait(nonfull); in := in mod size + 1; buffer[in] := tmp; count := count + 1; signal(nonempty); end;

Copyright © 2006 The McGraw-Hill Companies, Inc. function get : string; var tmp : string begin if (count = 0) then wait(nonempty); out = out mod size + 1; tmp := buffer[out]; count := count - 1; signal(nonfull); get := tmp; end;

Copyright © 2006 The McGraw-Hill Companies, Inc. States of a Java Thread Figure 17.6

Copyright © 2006 The McGraw-Hill Companies, Inc. Example: Bouncing Balls State of a ball in motion: Location: x, y coordinates Direction and velocity: dx, dy Size in pixels Color (fun!)

Copyright © 2006 The McGraw-Hill Companies, Inc. Ball methods: Constructor move: one step (delta) paint

Copyright © 2006 The McGraw-Hill Companies, Inc. public class Ball { Color color = Color.red; int x; int y; int diameter = 10; int dx = 3; int dy = 6;

Copyright © 2006 The McGraw-Hill Companies, Inc. public Ball (int ix, int iy) { super( ); x = ix; y = iy; color = new Color(x % 256, y % 256, (x+y) % 256); dx = x % ; dy = y % ; }

Copyright © 2006 The McGraw-Hill Companies, Inc. public void move () { if (x = BouncingBalls.width) dx = - dx; if (y = BouncingBalls.height) dy = - dy; x += dx; y += dy; }

Copyright © 2006 The McGraw-Hill Companies, Inc. public void paint (Graphics g) { g.setColor(color); g.fillOval(x, y, diameter, diameter); }

Copyright © 2006 The McGraw-Hill Companies, Inc. public class BouncingBalls extends JPanel { public final static int width = 500; public final static int height = 400; private Ball ball = new Ball(128, 127); private Vector list = new Vector();

Copyright © 2006 The McGraw-Hill Companies, Inc. public BouncingBalls ( ) { setPreferredSize(new Dimension(width, height)); list.add(ball); addMouseListener(new MouseHandler()); BallThread bt = new BallThread(); bt.start( ); }

Copyright © 2006 The McGraw-Hill Companies, Inc. private class MouseHandler extends MouseAdapter { public void mousePressed(MouseEvent e) { Ball b = new Ball(e.getX(), e.getY()); list.add(b); } // mousePressed } // MouseHandler

Copyright © 2006 The McGraw-Hill Companies, Inc. private class BallThread extends Thread { public boolean cont; public void run( ) { cont = true; while (cont) { for (Ball b : list) { b.move(); } repaint( ); try { Thread.sleep(50); } catch (InterruptedException exc) { } } }}

Copyright © 2006 The McGraw-Hill Companies, Inc. public synchronized void paintChildren(Graphics g) { for (Ball b : list) { b.paint(g); }

Copyright © 2006 The McGraw-Hill Companies, Inc. public static void main(String[] args) { JFrame frame = new JFrame("Bouncing Balls"); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new BouncingBalls( )); frame.setLocation(50, 50); frame.pack(); frame.setVisible(true); }

Copyright © 2006 The McGraw-Hill Companies, Inc. Buffer Class Figure 17.13

Copyright © 2006 The McGraw-Hill Companies, Inc. Producer Class Figure 17.14

Copyright © 2006 The McGraw-Hill Companies, Inc. Consumer Class Figure 17.15

Copyright © 2006 The McGraw-Hill Companies, Inc. Bounded Buffer Class Figure 17.16

Copyright © 2006 The McGraw-Hill Companies, Inc. Sieve of Eratosthenes Figure 17.18

Copyright © 2006 The McGraw-Hill Companies, Inc. Test Drive for Sieve of Eratosthenes Figure 17.18