1 16-035 Ada Constructs Revisited 21 Oct. 2002. 2 16-035 Constructs to be Expanded Generics Tasking Elaboration.

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.
Copyright © 2000, Daniel W. Lewis. All Rights Reserved. CHAPTER 10 SHARED MEMORY.
Global Environment Model. MUTUAL EXCLUSION PROBLEM The operations used by processes to access to common resources (critical sections) must be mutually.
Ch 7 B.
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
8a-1 Programming with Shared Memory Threads Accessing shared data Critical sections ITCS4145/5145, Parallel Programming B. Wilkinson Jan 4, 2013 slides8a.ppt.
CY2003 Computer Systems Lecture 05 Semaphores - Theory.
From HRT-HOOD to ADA95 Real-Time Systems Lecture 5 Copyright, 2001 © Adam Czajka.
1 Chapter 8 Channels. 2 Concurrent Programming Constructs So far we have seen contructs based on shared memory concept (shared directly – buffer - or.
Generic programming Define software components with type parameters –A sorting algorithm has the same structure, regardless of the types being sorted –Stack.
1 Friday, June 16, 2006 "In order to maintain secrecy, this posting will self-destruct in five seconds. Memorize it, then eat your computer." - Anonymous.
CS444/CS544 Operating Systems Synchronization 2/16/2006 Prof. Searleman
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.
Inter Process Communication:  It is an essential aspect of process management. By allowing processes to communicate with each other: 1.We can synchronize.
Chapter 11: Distributed Processing Parallel programming Principles of parallel programming languages Concurrent execution –Programming constructs –Guarded.
Generic programming Define software components with type parameters –A sorting algorithm has the same structure, regardless of the types being sorted –Stack.
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Generic programming Define software components with type parameters –A sorting algorithm has the same structure, regardless of the types being sorted –Stack.
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.
Even More Ada Constructs 9 Oct Today’s Constructs Generics Private types Child library units Tasking Pragmas Elaboration.
Concurrency - 1 Tasking Concurrent Programming Declaration, creation, activation, termination Synchronization and communication Time and delays conditional.
Pthread (continue) General pthread program structure –Encapsulate parallel parts (can be almost the whole program) in functions. –Use function arguments.
Experience with Processes and Monitors in Mesa
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.
ICS 145B -- L. Bic1 Project: Process/Thread Synchronization Textbook: pages ICS 145B L. Bic.
CSCI-455/552 Introduction to High Performance Computing Lecture 19.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 12 Concurrency can occur at four levels: 1. Machine instruction level 2. High-level language.
1 Programming Languages and the Software Production Process Informal Cardelli’s metrics of programming languages fitness to real-time applications: Economy.
1 Concurrency Architecture Types Tasks Synchronization –Semaphores –Monitors –Message Passing Concurrency in Ada Java Threads.
Parallel execution Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Using a simple Rendez-Vous mechanism in Java
ICS 313: Programming Language Theory Chapter 13: Concurrency.
I Power Higher Computing Software Development High Level Language Constructs.
Multithreading Chapter Introduction Consider ability of human body to ___________ –Breathing, heartbeat, chew gum, walk … In many situations we.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
Chapter 12, Slide 1 Concurrency Motivation 1: Mutual Exclusion  John and Mary share a bank acc't  withdraw x = copy balance to local machine subtract.
ADA95 – part III Real-Time Systems Lecture 3 Copyright, 2002 © Adam Czajka.
Copyright © Curt Hill Concurrent Execution An Overview for Database.
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.
COSC 3407: Operating Systems Lecture 9: Readers-Writers and Language Support for Synchronization.
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.
CSCI1600: Embedded and Real Time Software Lecture 17: Concurrent Programming Steven Reiss, Fall 2015.
Tutorial 3: Homework 2 and Project 2
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
1 Programming with Shared Memory - 2 Issues with sharing data ITCS 4145 Parallel Programming B. Wilkinson Jan 22, _Prog_Shared_Memory_II.ppt.
Deadlock and Starvation
COT 4600 Operating Systems Fall 2009
Background on the need for Synchronization
“Language Mechanism for Synchronization”
Deadlock and Starvation
Specification and Validation of Real-Time Programs ©A. Mok 2009
HW1 and Synchronization & Queuing
Multithreading Chapter 23.
Programming with Shared Memory
COP 4600 Operating Systems Fall 2010
Threading And Parallel Programming Constructs
Semaphore Originally called P() and V() wait (S) { while S <= 0
Lecture 2 Part 2 Process Synchronization
Concurrency: Mutual Exclusion and Process Synchronization
Subject : T0152 – Programming Language Concept
CSCI1600: Embedded and Real Time Software
Programming with Shared Memory - 2 Issues with sharing data
CSCI1600: Embedded and Real Time Software
CSE 542: Operating Systems
Presentation transcript:

Ada Constructs Revisited 21 Oct. 2002

Constructs to be Expanded Generics Tasking Elaboration

Generics Generic units are used to make Ada’s strong types less painful —Possible to create general unit once, but use it for many different declared types —Has types, variables, subprograms, and packages as parameters —When each instance is made of a generic unit, it might be a separate copy, or it might share code

Non-Generic Version package Complex_Numbers is type Complex is private;... function Construct (Real : Float; Imaginary : Float) return Complex; function “+” (X, Y: Complex) return Complex;... private type Complex is record Real_Part : Float; Imaginary_Part : Float; end record; end;

Non-Generic Body package body Complex_Numbers is... function Construct (Real : Float; Imaginary : Float) return Complex is begin return Complex’(Real, Imaginary); end Construct; function “+” (X, Y: Complex) return Complex is begin return Complex’( (X.Real_Part + Y.Real_Part), (X.Imaginary_Part + Y.Imaginary_Part) ); end “+”;... end Complex_Numbers;

Generic Example generic type My_Float is digits <>; package Generic_Complex_Numbers is type Complex is private;... function Construct (Real : My_Float; Imaginary : My_Float) return Complex; function “+” (X, Y: Complex) return Complex;... private type Complex is record Real_Part : My_Float; Imaginary_Part : My_Float; end record; end;

Generic Body – Almost Identical to Non-Generic package body Generic_Complex_Numbers is... function Construct (Real : My_Float; Imaginary : My_Float) return Complex is begin return Complex’(Real, Imaginary); end Construct; function “+” (X, Y: Complex) return Complex is begin return Complex’( (X.Real_Part + Y.Real_Part), (X.Imaginary_Part + Y.Imaginary_Part) ); end “+”;... end Generic_Complex_Numbers;

Instantiating a Generic with Generic_Complex_Numbers; procedure Test is type Small_Float_type is digits 6; type Large_Float_type is digits 14; package Small_Complex is new Generic_Complex_Numbers (Small_Float_type); use Small_Complex; package Large_Complex is new Generic_Complex_Numbers (Large_Float_type); use Large_Complex; Small_Real_Part : Small_Float_type := 1.234_567; Small_Imaginary_Part : Small_Float_type := 2.345_678; Large_Real_Part : Large_Float_type := 3.456_789_101_112; Large_Imaginary_Part : Large_Float_type := 4.567_891_011_121_3; Small_Complex_Number : Small_Complex.Complex := Construct (Small_Real_Part, Small_Imaginary_Part); Large_complex_Number : Large_Complex.Complex := Construct (Large_Real_part, Large_Imaginary_Part); begin Small_Complex_Number := Small_Complex_Number + Small_Complex_Number; Large_Complex_Number := Large_Complex_Number + Large_Complex_Number; end Test;

Tasking Multitasking (what UNIX people call Threads) is built in to the Ada language —Model is parallel activities —Easily implemented to use multiple processors —Implementation can use any scheduling mechanism »Time slicing, where each task (of the same priority) gets a bit of time, then the next task runs »One task runs until it hits a blocking point, then the next task (of the same priority or lower) runs —Priority can be used (if supported) to force the system to run tasks in a certain order (if running on a single processor system)

The rendezvous “two people meet, perform a transaction and then go on independently” Between two tasks: one task calls an entry declared in another. Task Specification task T is entry E( … ); end; Some other Task T.E( … ); Task Body accept E( … ) do --sequence of statements end E;

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; -- Passeren (lock) end Semaphore; -- Vrimajken (unlock) task body Semaphore is begin loop accept P; accept V; -- will not accept another P until a caller asks for V end loop; end Semaphore; Example: semaphore

Using a semaphore A task that needs exclusive access to the critical section executes: Semaphore.P; … -- critical section code Semaphore.V; If in the meantime another task calls Semaphore.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 making an entry call to V. Programming hazards: –someone else may call V : race condition –no one calls V: other callers are deadlocked –Starvation is possible if the queue is not a FIFO queue

Timing and scheduling Time slicing Priorities Real-Time Systems annex (Annex D) Task can be held up of different reasons —Waiting for: »a partner in a rendezvous »dependent task to terminate —delay 3.0; Seconds: constant duration := 1.0; Minutes: constant Duration := 60.0; Hours: constant Duration := ; delay 2*Hours+40*Minutes;

Example loop delay 5*Minutes; Action; end loop; Want cyclic execution. Any problems with this solution?

Same, but different … declare use Calendar; Interval: constant Duration := 5*Minutes; Next_Time: Time := First_Time; begin loop delay until Next_Time; Action; Next_Time := Next_Time + Interval; end loop; end;

“Protected Variable” Protect a variable V from uncontrolled access. package Protected_Variable is procedure Read(X: out Item); procedure Write(X: in Item); end; package body Protected_Variable is V: Item := initial value; procedure Read(X: out Item) is begin X := V; end; procedure Write(X: in Item) is begin V := X; end; end Protected_Variable; type Item is record X_Coord: Float; Y_Coord: Float; end record; Any problems?

Protected Object Example package Variable is type My_Type is...; protected P_Variable is function Read return My_Type; -- Many callers procedure Write ( -- One caller X : in My_Type ); private V : My_Type := 0; end P_Variable; end Variable; Any number of tasks may call protected functions at the same time Protected functions can not write to protected objects Not possible for a task to call Write while another calls Read (the second will block)

Bounded buffer IJ N: constant := 8; type Index is mod N; type Item_Array is array (Index) of Item; protected type Buffering is entry Put(X: in Item); entry Get(X: out Item); private A: Item_Array; I,J: Index := 0; Count: Integer range 0..N := 0; end Buffering; protected body Buffering is entry Put(X: in Item) when Count < N is begin A(I) := X; I := I+1; Count := Count + 1; end Put; entry Get(X: out Item) when Count >0 is begin X := A(J); J := J + 1; Count := Count – 1; end Get; end Buffering; My_Buffer : Buffering; … My_Buffer.Put(X);

Elaboration Ada programs perform work before the first executable statement after the “begin” of the main procedure is run —“Elaboration” code is executed first »Gives data initial values (which might call functions) »Creates objects (if dynamic) »Runs package initialization code To be completely precise, the whole program is one big elaboration —Package specifications are elaborated before their bodies —Package bodies are elaborated before any of their subprograms can be called —Package bodies can have task bodies, which start running after the package body has been elaborated —When all packages have been elaborated, the main procedure runs

Elaboration Example with Ada.Text_IO; procedure Elab_Test is package Random_Pack is -- Elaboration of this is first type My_Type is digits 6 range ; function Random return My_Type; end Random_Pack; package body Random_Pack is -- Elaboration of this is second Seed : My_Type; function Random return My_Type is task My_Task; task body My_Task is X : My_Type := Random; begin Ada.Text_IO.Put ("Made it"); end My_Task; begin return Seed; -- For now, return a constant end Random; begin Seed := 0.5; -- Need this before Random can be called end Random_Pack; -- After last statement after "begin", My_Task can run use Random_Pack; Z : My_Type := Random; -- Executes the Random function begin null; end Elab_Test;

Watch Out for Circular Dependencies package My_Pack is type My_type is range 1_ _000_000; procedure Do_Something (Parameter : in out My_type); end My_Pack; package Other_Pack is type Other_type is range 0.. 1_000; function Do_Something_Else return Other_type; end Other_Pack; with Other_Pack; pragma Elaborate (Other_Pack); package body My_Pack is X : Other_Pack.Other_type := Do_Something_Else; -- Needs Other_Pack body elab... end My_Pack; with My_Pack; package body Other_Pack is X : My_Pack.My_type;... end Other_Pack;