ICS 313: Programming Language Theory Chapter 13: Concurrency.

Slides:



Advertisements
Similar presentations
Operating Systems: Monitors 1 Monitors (C.A.R. Hoare) higher level construct than semaphores a package of grouped procedures, variables and data i.e. object.
Advertisements

Practice Session 7 Synchronization Liveness Deadlock Starvation Livelock Guarded Methods Model Thread Timing Busy Wait Sleep and Check Wait and Notify.
1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
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.
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 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
ISBN Chapter 13 Concurrency. Copyright © 2006 Addison-Wesley. All rights reserved.2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
Comparative Programming Languages hussein suleman uct csc304s 2003.
ISBN Chapter 13 Concurrency. Copyright © 2009 Addison-Wesley. All rights reserved.1-2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.
1 Chapter 13 © 2002 by Addison Wesley Longman, Inc Introduction - Concurrency can occur at four levels: 1. Machine instruction level 2. High-level.
Concurrency (Based on:Concepts of Programming Languages, 8th edition, by Robert W. Sebesta, 2007)
12/1/98 COP 4020 Programming Languages Parallel Programming in Ada and Java Gregory A. Riccardi Department of Computer Science Florida State University.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Concurrency Concurrency is the simultaneous execution of program code –instruction level – 2 or more machine instructions simultaneously –statement level.
(12.1) COEN Concurrency and Exceptions  Coroutines  Physical and logical concurrency  Synchronization – semaphores – monitors – message passing.
Chapter 13 Concurrency. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Introduction Concurrency can occur at four levels: –Machine instruction.
CS 355 – PROGRAMMING LANGUAGES Dr. X. 1-2 Chapter 13 Topics Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing.
CPS 506 Comparative Programming Languages
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 12 Concurrency can occur at four levels: 1. Machine instruction level 2. High-level language.
Lecture 13 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
ISBN Chapter 13 Concurrency. Copyright © 2009 Addison-Wesley. All rights reserved.1-2 Chapter 13 Topics Introduction to Subprogram-Level.
ISBN Chapter 13 Concurrency. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.13-2 Chapter 13 Topics Introduction Introduction.
1 Concurrency Architecture Types Tasks Synchronization –Semaphores –Monitors –Message Passing Concurrency in Ada Java Threads.
SEMAPHORE By: Wilson Lee. Concurrency Task Synchronization Example of semaphore Language Support.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chapter 7 -1 CHAPTER 7 PROCESS SYNCHRONIZATION CGS Operating System Concepts UCF, Spring 2004.
ISBN Chapter 13 Concurrency. Copyright © 2015 Pearson. All rights reserved.1-2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.
ISBN Chapter 13 Concurrency. Copyright © 2009 Addison-Wesley. All rights reserved.1-2 Chapter 13 Topics Introduction Introduction to Subprogram-Level.
Monitors and Blocking Synchronization Dalia Cohn Alperovich Based on “The Art of Multiprocessor Programming” by Herlihy & Shavit, chapter 8.
13-1 Chapter 13 Concurrency Topics Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads C# Threads.
CS533 – Spring Jeanie M. Schwenk Experiences and Processes and Monitors with Mesa What is Mesa? “Mesa is a strongly typed, block structured programming.
Chapter 13 Concurrency. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 13 Topics Introduction Introduction to Subprogram-Level Concurrency.
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
Chapter 13 Concurrency. Corrected and improved by Assoc. Prof. Zeki Bayram, EMU, North Cyprus. Original Copyright © 2012 Addison-Wesley. All rights reserved1-2.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Java Thread Programming
Chapter 13 Concurrency.
Multithreading / Concurrency
Chapter 13 Concurrency.
Lecture 12 Concepts of Programming Languages
Monitors Chapter 7.
Multithreading.
Multithreading.
Chapter 13 Concurrency.
Chapter 13 Concurrency.
Monitors Chapter 7.
Concurrency: Mutual Exclusion and Process Synchronization
Subject : T0152 – Programming Language Concept
Monitors Chapter 7.
Chapter 13 Concurrency.
Chapter 13 Concurrency.
Chapter 13 Concurrency.
Chapter 13 Concurrency.
“The Little Book on Semaphores” Allen B. Downey
Chapter 13 Concurrency.
Chapter 13 Concurrency.
Presentation transcript:

ICS 313: Programming Language Theory Chapter 13: Concurrency

Why Study Concurrency Make effective use of multiprocessor machines SIMD: Single Instruction Multiple Data - e.g., vector processors MIMD: Multiple Instruction Multiple Data - e.g., distributed systems Useful way to conceptualize some programming problems The world is inherently concurrent Programs are more modular when inessential timing dependencies are avoided

Categories of Concurrency Physical concurrency: Actually running at the same time on different processors Logical concurrency: Treated as if concurrent, but may be interleaved on shared processor(s) Quasi-concurrency: A single thread of control, e.g., coroutines At the level of language design the issues are the same

Concurrency Example (define (withdraw amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds"))

Why Help is Needed Can we just list the possible orders of events and make sure the wrong ones don’t happen? (set! balance (- balance amount)) How many possible orders for Peter: (a) retrieve balance (b) compute difference. (c) store balance Paul: (x) retrieve balance. (y) compute difference. (z) store balance (a, b, c, x, y, z) (a, b, x, c, y, z) (a, b, x, y, c, z) (a, b, x, y, z, c) (a, x, b, c, y, z) (a, x, b, y, c, z) … There are 20

Tasks May be started implicitly Invoker need not wait for completion Control may not return to invoker Scheduler manages sharing of processors between tasks Tasks can be New Runnable: on the task ready queue Running Blocked: waiting for some event to complete Dead

Synchronization Synchronization controls order of task execution Cooperation synchronization When one task needs the result of the concurrent task Example: Producer and consumer using a shared buffer Competition synchronization When one task is waiting for another task’s unrelated computation due to resource constraints Example: Two producers writing to the same event log

Deadlock Classic concurrency problem Task A takes control of resource X Task B takes control of resource Y Task A needs Y to continue, so waits for B Task B needs X to continue, so waits for A Need competition synchronization

Semaphores Guards controlling access are placed around code accessing the resource P(resource) - wait (passeren) (put your code to access resource here) V(resource) - release (vrygeren) Guards are implemented using a Semaphore data structure Integer counting some resource Queue storing tasks waiting for that resource

Wait and Release wait(semaphore, caller) if semaphore.counter > 0 then decrement semaphore.counter else semaphore.queue.enqueue(caller) run semaphore.queue.dequeue() release(semaphore, caller) if semaphore.queue.isEmpty() then increment semaphore.counter else semaphore.queue.enqueue(caller) run semaphore.qeueue.dequeue()

Evaluation of Semaphores Problem: Semaphore itself is a shared data object. How to prevent simultaneous access? Solution: some machines provide uninterruptable instructions Problem: Cannot statically check correctness of use If programmer leaves out a P or V, deadlock or resource overflow may result “An elegant synchronization mechanism for an ideal programmer”

Monitors Shared data structures become abstract data types Implementation of the ADT guarantees integrity (i.e. that shared data structures are not accessed simultaneously) Implementation may actually use semaphores, but the use of semaphores is generated by the compiler not the programmer

Processes in Concurrent Pascal Looks like procedure, but just a template type process_name = process(parameters) local declarations process body end Execution begun with init var myProcess : process_name … init myProcess(argments) Runs indefinitely except when blocked

Monitors in Concurrent Pascal type monitor_name = monitor(parameters) declarations of shared variables definitions of local procedures definitions of exported procedures initialization code end Access to shared variables is controlled

Example type databbuf = monitor declarations of buffer variables procedure entry deposit(item : integer); body procedure entry fetch(var item: integer); body initialization code end type producer = process(buffer: databuf); body type consumer = process(buffer : databuf); body var myProducer : producer; myConsumer : consumer; myBuffer : databaf; begin Init myBuffer, myProducer(myBuffer), myConsumer(myBuffer); end

Queues Programmer must still handle logic of resource limitations This is done with queue data type Delay: similar to wait except that always blocks caller Continue: similar to release but does nothing if queue is empty Programmer handles counting

Evaluation of Monitors Good for competition synchronization because they abstract issues of concurrent access Have same problems as semaphores for cooperation synchronization (due to delay/continue) Inappropriate model for distributed systems (no shared memory)

Message Passing Motivated by distributed systems Tasks make requests of other tasks Tasks should not have to accept requests until ready Multiple simultaneous requests should be handled nondeterministically Rendezvous: when one task is ready to send and the other task ready to receive a message

Ada Message Passing Tasks have specification and body task EXAMPLE is entry ENTRY(ITEM : in INTEGER); end EXAMPLE task body EXAMPLE is begin loop accept ENTRY(ITEM : in INTEGER) do … end ETNRY; end loop end EXAMPLE; Queue associated with each entry Caller blocked on call to entry until task reaches accept Task blocked on accept until call made to entry

Concurrency in Java Methods of class Thread run : a method that can run concurrently start : a method that calls run and returns immediately yield : surrender time slice sleep : stop for at least specified number of seconds Two approaches Extend Thread (overriding run with concurrent code) Implement Runnable and make a Thread that has this object as a component

Competition Synchronization Synchronized modifier on a method locks the instance class SharedObject { … public synchronized int accessObject(…) { … } public synchronized void updateObject(…) { … } } Only one thread will be able to run either accessObject or updateObject at a time Can also synchronize a statement or compound statement synchronized(evaluates-to-object) statement

Cooperation Synchronization Cooperation requires that processes communicate about whether one has provided what the other needs We can express this as an arbitrary boolean condition wait() is used to test to see if that condition is true, and queue thread if not try { while (!condition-test) wait(); do what needs to be done } catch(InterruptedExcepton e) {…} notify() is used to tell threads waiting on the queue that the condition should be checked again

Semaphores in Java We can implement semaphors and monitors using Java’s facilities public class Semaphore { private int value; public Semaphore (int initial) { value = initial; } synchronized public void up() { ++value; notify(); } synchronized public void down() throws InterruptedException { while (value==0) wait(); --value; } } (Thanks to Jeff Magee & Jeff KramerJeff MageeJeff Kramer

See examples dse.doc.ic.ac.uk/concurrency/book_applets/concurrency.html Semaphore Demonstration NestedMonitor FixedNestedMonitor Dining Philosphers

End