Concurrency - 1 Tasking Concurrent Programming Declaration, creation, activation, termination Synchronization and communication Time and delays conditional.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

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.
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.
Ch 7 B.
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
CSCC69: Operating Systems
Lecture 5 Concurrency and Process/Thread Synchronization     Mutual Exclusion         Dekker's Algorithm         Lamport's Bakery Algorithm.
1 Semaphores and Monitors CIS450 Winter 2003 Professor Jinhua Guo.
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 
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Concurrency - 1 Exceptions General mechanism for handling abnormal conditions Predefined exceptions: constraint violations, I/O errors, communication errors,
Concurrency in Ada Programming Languages 1 Robert Dewar.
Concurrency in Ada What concurrency is all about Relation to operating systems Language facilities vs library packages POSIX threads Ada concurrency Real.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
Concurrency CS 510: Programming Languages David Walker.
Concurrency - 1 Exceptions General mechanism for handling abnormal conditions Predefined exceptions: constraint violations, I/O errors, communication errors,
Inter Process Communication:  It is an essential aspect of process management. By allowing processes to communicate with each other: 1.We can synchronize.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
Concurrency - 1 Exceptions General mechanism for handling abnormal conditions Predefined exceptions: constraint violations, I/O errors, communication errors,
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
29-Jun-15 Java Concurrency. Definitions Parallel processes—two or more Threads are running simultaneously, on different cores (processors), in the same.
A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.
1 Exceptions and Concurrency Exceptions and Concurrency.
Comparative Programming Languages hussein suleman uct csc304s 2003.
1 Chapter 13 © 2002 by Addison Wesley Longman, Inc Introduction - Concurrency can occur at four levels: 1. Machine instruction level 2. High-level.
Experience with Processes and Monitors in Mesa
Concurrency (Based on:Concepts of Programming Languages, 8th edition, by Robert W. Sebesta, 2007)
Threads in Java. History  Process is a program in execution  Has stack/heap memory  Has a program counter  Multiuser operating systems since the sixties.
12/1/98 COP 4020 Programming Languages Parallel Programming in Ada and Java Gregory A. Riccardi Department of Computer Science Florida State University.
Lecture 5 : JAVA Thread Programming Courtesy : MIT Prof. Amarasinghe and Dr. Rabbah’s course note.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
1 Processes, Threads, Race Conditions & Deadlocks Operating Systems Review.
CPS 506 Comparative Programming Languages
1 Web Based Programming Section 8 James King 12 August 2003.
Semaphores, Locks and Monitors By Samah Ibrahim And Dena Missak.
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.
1 Concurrency Architecture Types Tasks Synchronization –Semaphores –Monitors –Message Passing Concurrency in Ada Java Threads.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
PZ12B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ12B - Synchronization and semaphores Programming Language.
Synchronization and semaphores Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
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
Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Concurrency.
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
13-1 Chapter 13 Concurrency Topics Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads C# Threads.
Ada Constructs Revisited 21 Oct Constructs to be Expanded Generics Tasking Elaboration.
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.
Comunication&Synchronization threads 1 Programación Concurrente Benemérita Universidad Autónoma de Puebla Facultad de Ciencias de la Computación Comunicación.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
1 Java Programming Java Programming II Concurrent Programming: Threads ( I)
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Lecture 12 Concepts of Programming Languages
Concurrency: Mutual Exclusion and Synchronization
Multithreading Chapter 23.
Condition Variables and Producer/Consumer
Condition Variables and Producer/Consumer
Multithreaded Programming
Subject : T0152 – Programming Language Concept
Synchronization and semaphores
Presentation transcript:

concurrency - 1 Tasking Concurrent Programming Declaration, creation, activation, termination Synchronization and communication Time and delays conditional communication Non-determinism

concurrency - 2 Concurrent programming Synchronous and asynchronous models of communication Description of simultaneous, independent activities A task is an independent thread of control, with own stack, program counter and local environment. Ada Tasks communicate through Rendez-vous protected objects Shared variables Java threads communicate through shared objects (preferably synchronized)

concurrency - 3 Task Declarations A task type is a limited type: state cannot be copied task type worker; -- declaration; public interface type Worker_Id is access worker; -- a conventional access type task body worker is -- actions performed in lifetime begin loop -- forever. Will be shutdown from compute; -- the outside. end loop; end worker;

concurrency - 4 Task Declarations A task type can be a component of a composite. The number of tasks in a program is not fixed at compile-time. W1, W2 : Worker; -- two individual tasks type Crew is array (Integer range <>) of worker; First_Shift : Crew (1.. 10); -- a group of tasks type Monitored is record Counter : Integer; Agent : Worker; end record;

concurrency - 5 Task Activation When does a task start running: If statically allocated, at the next begin If dynamically allocated, at the point of allocation. declare W1, W2 : Worker; Joe : Worker_Id := new Worker; -- Starts working at once Third_Shift : Crew (1..N); -- some number of them begin -- activate W1, W2, and the … -- third_shift end; -- wait for them (not Joe) -- to complete

concurrency - 6 Task Services A task can perform some actions on request from another task The interface (declaration) of the task specifies the available actions (entries) A task can also execute some actions on its own behalf, without external requests or communication. task type Device is entry Read (X : out Integer); entry Write (X : Integer); end Device;

concurrency - 7 Synchronization: Rendez-vous Caller makes explicit request: entry call Callee (server) states its availability: accept statement If server is not available, caller blocks and queues up on the entry for later service If both present and ready, parameters are transmitted to server. Server performs action Out parameters are transmitted to caller Caller and server continue execution independently

concurrency - 8 Example : semaphore Simple mechanism to create critical sections: section of code that must be executed by only one task at a time task type semaphore is entry P; -- Dijkstra’s terminology entry V; -- from the Dutch end semaphore; task body semaphore is begin loop accept P; -- won’t accept another P until a caller asks for V accept V; end loop; end semaphore;

concurrency - 9 Using a semaphore A task that needs exclusive access to the critical section executes: Sema.P; -- critical section code Sema.V; If in the meantime another task calls Sema.P, it blocks, because the semaphore does not accept a call to P until after the next call to V: the other task is blocked until the current one releases by makin an entry call to V. programming hazards: someone else may call V : race condition no one calls V: other callers are deadlocked.

concurrency - 10 Delays and Time A delay statement can be executed anywhere at any time, to make current task quiescent for a stated interval: delay 0.2; -- type is Duration, units of seconds We can also specify that the task stop until a certain specified time: delay until Noon; -- defined elsewhere

concurrency - 11 Time package Ada.Calendar is type Time is private; -- nanoseconds since epoch, maybe subtype Year_Number is Integer range ;.. function Clock return Time; -- some operating system service procedure Split (Year : out Year_Number; Month : out Month_Number; Day : out Day_Number; Seconds : out Day_Duration; function “+” (Left : Time; Right : Duration) return Time; … end;

concurrency - 12 Conditional Communication Need to protect against excessive delays, deadlock, starvation, caused by missing or malfunctioning tasks. Timed entry call: caller waits for rendez-vous a stated amount of time: select Disk.Write (value => 12, Track => 123); -- Disk is a task or delay 0.2; end select; If Disk does not accept within 0.2 Secs, go do something else.

concurrency - 13 Conditional Communication (ii) Conditional entry call: caller ready for rendez-vous only if no one else is queued, and rendez-vous can begin at once: select Disk.Write (value => 12, Track => 123); else Put_Line (“device busy”); end select; Print message if call cannot be accepted immediately.

concurrency - 14 Conditional communication (iii) The server may accept a call only if the internal state of the task is appropriate: select when not full => accept Write (Val : Integer); or when not empty => Accept Read (Var : out Integer); or delay 0.2; -- maybe something will happen end select; If several guards are open and callers are present, any one of the calls may be accepted : non-determinism.

concurrency - 15 Data-Driven synchronization Tasking operations are heavy-duty, involve several system calls and exchange of multiple messages: using a task to synchronize two other tasks is an example of abstraction inversion (the machine gun as fly swatter) Need lighter locking mechanisms without additional threads. Pascal monitors are a good model: object with operations that guarantee mutual exclusion on data Modern incarnation: protected types

concurrency - 16 Protected types Object encapsulates private data, public operations, and implicit locks (a quintessential object) protected type counter is function value return integer; procedure Increment (delta : Integer); private Val : integer := 0; end; Value can be executed by several threads concurrently. Increment is executed by a single thread

concurrency - 17 Protected bodies Function execution takes a read lock on object Procedure execution takes a read / write lock Entries like procedures, but have queues. protected body counter is function value return integer is begin return val; end; procedure increment (delta : integer) is begin val := val + delta; end increment; end counter;

concurrency - 18 Entries and queues Need conditional blocking: object may be free but in the wrong state. protected body buffer is entry put (x : in Item) when not full is begin -- insert into container entry get (x : out item) when not empty is begin -- remove from container end buffer ; Private data includes full / empty indications

concurrency - 19 Concurrency in Java Two primitive notions class thread interface runnable An object of class thread is mapped into an operating system primitive interface runnable { public void run ( ); }; Any class can become a thread of control, by supplying a run method

concurrency - 20 Threads at work class PingPong extends Thread { String word; int delay; PingPong (String whatToSay, int delayTime) { word = whatToSay; } public void run () { try { for (;;) { // infinite loop System.out.print (word + “ “); sleep (delayTime); // yield processor } } catch (InterruptedException e) { return; // terminate thread } } }

concurrency - 21 Activation and execution public static void man (String[] args) { new PingPong (“ping”, 33).start ( ); // activate new PingPong (“pong”, 100).start ( ); // activate } Call to start activates thread, which executes run method Threads can communicate through shared objects Classes can have synchronized methods to enforce critical sections.