Today s topic: Real Time Programming with Ada

Similar documents
Real-Time Programming

Today s topic: Real Time Programming with Ada

Overall Structure of RT Systems

Ada 2005 An introduction for TTK4145

Ada and Real-Time. Prof. Lars Asplund. Mälardalen University, Computer Science

Resource management. Real-Time Systems. Resource management. Resource management

Real-Time Systems. Lecture #4. Professor Jan Jonsson. Department of Computer Science and Engineering Chalmers University of Technology

Green Hills Software, Inc.

Performance Throughput Utilization of system resources

A Predictable RTOS. Mantis Cheng Department of Computer Science University of Victoria

TDDD07 Real-time Systems Lecture 10: Wrapping up & Real-time operating systems

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

Implementing Scheduling Algorithms. Real-Time and Embedded Systems (M) Lecture 9

Embedded Systems. 6. Real-Time Operating Systems

Comp 310 Computer Systems and Organization

EECS 571 Principles of Real-Time Embedded Systems. Lecture Note #10: More on Scheduling and Introduction of Real-Time OS

Last Class: Synchronization

Real-Time and Concurrent Programming Lecture 4 (F4): Monitors: synchronized, wait and notify

Dept. of CSE, York Univ. 1

Lecture 3: Concurrency & Tasking

Real-Time Programming

Java Basics. Object Orientated Programming in Java. Benjamin Kenwright

SWE 760 Lecture 1: Introduction to Analysis & Design of Real-Time Embedded Systems

Interrupts on the 6812

Process Management And Synchronization

Last Class: CPU Scheduling! Adjusting Priorities in MLFQ!

Lecture notes Lectures 1 through 5 (up through lecture 5 slide 63) Book Chapters 1-4

Concurrent Programming

Concurrent Programming. Implementation Alternatives. Content. Real-Time Systems, Lecture 2. Historical Implementation Alternatives.

Real-Time Programming Languages (ADA and Esterel as Examples)

Programming Languages for Real-Time Systems. LS 12, TU Dortmund

CSC Operating Systems Spring Lecture - XII Midterm Review. Tevfik Ko!ar. Louisiana State University. March 4 th, 2008.

CSE 153 Design of Operating Systems

The Real Time Thing. What the hack is real time and what to do with it. 22C3 30. December Erwin Erkinger e.at

Last Class: Deadlocks. Today

Chapter 6: Process Synchronization. Operating System Concepts 9 th Edit9on

Introduction to Lab 2

Implementing Mutual Exclusion. Sarah Diesburg Operating Systems CS 3430

CHAPTER 6: PROCESS SYNCHRONIZATION

Chapter 13 Topics. Introduction. Introduction

Lecture 4: Memory Management & The Programming Interface

Chapter 13. Concurrency ISBN

CHAPTER 2: PROCESS MANAGEMENT

Informatica 3. Marcello Restelli. Laurea in Ingegneria Informatica Politecnico di Milano 9/15/07 10/29/07

Lecture 2 Process Management

Lecture #7: Implementing Mutual Exclusion

Implementation Alternatives. Lecture 2: Implementation Alternatives & Concurrent Programming. Current Implementation Alternatives

Real Time Operating System: Inter-Process Communication (IPC)

Operating Systems. Lecture 4 - Concurrency and Synchronization. Master of Computer Science PUF - Hồ Chí Minh 2016/2017

Today: Synchronization. Recap: Synchronization

Main Points of the Computer Organization and System Software Module

Lecture 17: Threads and Scheduling. Thursday, 05 Nov 2009

Midterm Exam. October 20th, Thursday NSC


CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring Lecture 8: Semaphores, Monitors, & Condition Variables

a data type is Types

Notes on the Exam. Question 1. Today. Comp 104:Operating Systems Concepts 11/05/2015. Revision Lectures (separate questions and answers)

Dr. D. M. Akbar Hussain DE5 Department of Electronic Systems

Embedded Systems. 5. Operating Systems. Lothar Thiele. Computer Engineering and Networks Laboratory

Module 20: Multi-core Computing Multi-processor Scheduling Lecture 39: Multi-processor Scheduling. The Lecture Contains: User Control.

Multiprogramming. Evolution of OS. Today. Comp 104: Operating Systems Concepts 28/01/2013. Processes Management Scheduling & Resource Allocation

Lecture 15: Object-Oriented Programming

Comp 204: Computer Systems and Their Implementation. Lecture 25a: Revision Lectures (separate questions and answers)

Concept of a process

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

Processes and Threads. Processes: Review

Need for synchronization: If threads comprise parts of our software systems, then they must communicate.

Synchronization I. Jo, Heeseung

Question 1. Notes on the Exam. Today. Comp 104: Operating Systems Concepts 11/05/2015. Revision Lectures

Lecture 13 Condition Variables

Embedded Systems: OS. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Concurrency, Mutual Exclusion and Synchronization C H A P T E R 5

Precedence Graphs Revisited (Again)

a. It will output It s NOT Rover b. Class Main should be changed to the following (bold characters show the changes)

CS 31: Introduction to Computer Systems : Threads & Synchronization April 16-18, 2019

Intertask Communication

Dealing with Issues for Interprocess Communication

Multimedia Systems 2011/2012

Concurrent Programming

What s An OS? Cyclic Executive. Interrupts. Advantages Simple implementation Low overhead Very predictable

Scheduling Algorithm and Analysis

Concurrency, Thread. Dongkun Shin, SKKU

COMP 346 WINTER Tutorial 2 SHARED DATA MANIPULATION AND SYNCHRONIZATION

Operating Systems, Fall

Embedded Systems: OS

CS 318 Principles of Operating Systems

CSE Traditional Operating Systems deal with typical system software designed to be:

Embedded Operating Systems

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

CSCI-1200 Data Structures Fall 2009 Lecture 25 Concurrency & Asynchronous Computing

Time Triggered and Event Triggered; Off-line Scheduling

CS 261 Fall Mike Lam, Professor. Threads

CSE 307: Principles of Programming Languages

2 Introduction to Processes

Concurrent Programming

Maximum CPU utilization obtained with multiprogramming. CPU I/O Burst Cycle Process execution consists of a cycle of CPU execution and I/O wait

Exam TI2720-C/TI2725-C Embedded Software

IT 540 Operating Systems ECE519 Advanced Operating Systems

Software Architecture

Deadlock and Monitors. CS439: Principles of Computer Systems September 24, 2018

Transcription:

Today s topic: Real Time Programming with Ada 1 Programming the car controller (3) 76 800 Soft RT tasks speed 4 ABS 14 64 FUEL-4 FUEL-1 20 speed A feasible Schedule! speed 24 60 FUEL-3 54 ABS 44 speed 40 Fuel-2 2 1

Programming the car controller Process Speed: Loop next := get-time + 0.02 read sensor,compute,display... sleep until next End loop Process ABS Loop next:=get-time + 0.04 Read sensor, compute, react sleep until next End loop Process Fuel Loop next:=get-time + 0.08 read data, compute, inject... sleep until next End loop Soft RT Processes Loop read temperature elevator, stereo... End loop Qustion: do we need 4 CPUs to run these concurrently? 3 Real time programming It is mostly about Concurrent programming We also need to handle Timing Constraints on concurrent executions of tasks (and maybe other constraints on e.g. energy consumption etc) However, remember: concurrency is a way of structuring computer programs e.g. three concurrent modules : task 1, task 2 task 3 concurrency is often implemented by fast sequential computation 4 2

The classic/simplest approach: cyclic execution Program your tasks in any sequential language loop do task 1 do task 2 do task 3 end loop Efficient code, deterministic, predictable, But difficult to make it right, difficult to reuse existing design In particular extremely difficult for constructing large systems 5 Cyclic Execution : example Task Required sample rate Processing time t1 3ms (333Hz) 0.5ms t2 6ms (166Hz) 0.75ms t3 14ms (71Hz) 1.25ms void main(void) { do_init(); while (1) { t1(); t2(); t3(); delay_until_cycle_start(); } } 0ms 3ms 6ms 9ms 12ms 6 3

Cyclic Execution : overheads Task Required sample rate Processing time t1 3ms (333Hz) 0.5ms t2 requires 12.5% CPU (0.75/6), uses 25% (4*0.75/12) t3 requires 9% CPU (1.25/14), uses 42% (4*1.25/12) t2 6ms (166Hz) 0.75ms t3 14ms (71Hz) 1.25ms add interrupt I with 0.5ms processing time 0ms 3ms 6ms 9ms 12ms 7 Major/minor cyclic scheduling 12ms major cycle containing 3ms minor cycles t1 every 3ms, t2 every 6ms, t3 every 12ms t3 still upsampled (10.4% where 9% needed) time is still allocated for I in every cycle will not always be used, but must be reserved 0ms 3ms 6ms 9ms 12ms 8 4

Fitting tasks to cycles add t4 with 14ms rate and 5ms processing time 12ms cycle has 5.25ms free time......but t4 has to be artificially partitioned 0ms 3ms 6ms 9ms 12ms 9 Effect of new task at code level void do_task_t4(void) { /* Task functionality */ } void do_task_t4_1(void) { /* first bit */ state_var_1 = x; state_var_2 = y;... } void do_task_t4_2(void) { x = state_var_1;... /* second bit */ state_var_3 = a; state_var_4 = b;... } void do_task_t4_3(void) { c = state_var_4;... /* third bit */ } int state_var_1; int state_var_2; int state_var_3; int state_var_4; void main(void) { do_init(); while(1) { } } do_task_t1(); do_task_t2(); do_task_t3(); busy_wait_minor(); do_task_t1(); /* 3ms */ do_task_t4_1(); busy_wait_minor(); do_task_t1(); /* 6ms */ do_task_t2(); do_task_t4_2(); busy_wait_minor(); do_task_t1(); /* 9ms */ do_task_t4_3(); busy_wait_minor(); 10 5

This is ad hoc, but it is often used in industry You just don t want to do this for large software systems, say a few hundreds of control tasks This was why Multitasking came into the picture 11 Concurrent programming with multitasking: Program your computation tasks, execute them concurrently with OS support e.g. in LegOS (or in Ada in slightly different syntax) execi(foo1,..., priority1,...); execi(foo2,..., priority2,...); execi(foo3,..., priority3,...); Will start three concurrent tasks running foo1, foo2, foo3 12 6

Cyclic Execution vs. Multitasking with RTOS 0ms 3ms 6ms 9ms 12ms 13 RTOS vs. Programming Languages Without RTOS support Program your tasks in any programming language (C, Assembly ) Cyclic Execution Execution according to a Time table (static schedule) With explicit RTOS support Program your tasks in any programming language Fix the scheduler in OS e.g. static schedule, priority assignment Using RT programming languages Program your tasks in a RT language e.g. RT Java, Ada RTOS is hidden, a Run-Time kernel for each program We will consider the RT prog. lang. Ada 14 7

Ada95 It is strongly typed OO language, looks like Pascal Originally designed by the US DoD as a language for large safety critical systems i.e. Military systems Ada83 Ada95 + RT annex + Distributed Systems Annex Ada 2005 (allows scheduling policies e.g. RR, EDF, dynamic priorities for protected types ) 15 The basic structures in Ada A large part in common with other languages Procedures Functions Basic types: integers, characters,... Control statements: if, for,..., case analysis Any thing new? Abstract data type: Packages (objects) Protected data type Tasking: concurrency Task communication/synchronization: rendezvous Real Time 16 8

Typical structure of programs Program Foo(...) Declaration 1 ----- to introduce identities/variables and define data structures Declaration 2 ----- to define operations : procedures, functions and/or tasks (concurrent operations) to manopulate the data structures Main program (Program body) ------ a sequence of statements or operations to compute the result (output) 17 Declarations and statements Before each block, you have to declare (define) the variables used, just like any sequential program procedure PM (A : in INTEGER; B: in out INTEGER; C : out INTEGER) is B := B+A; C := B + A; end PM; 18 9

If, case, for: contrl-statements if TEMP < 15 then some smart code; else do something else..; end if; case TAL is when <2 => PUT_LINE( one or two ); when >4 => PUT_LINE( greater than 4); end case; for I in 1..12 loop PUT( in the loop ); end loop; 19 Types (like in Pascal or any other fancy languages) type LINE_NUMBER is range 1.. 72 type WEEKDAY is (Monday, Tuesday, Wednesday); type serie is array (1..10) of FLOAT; type CAR is record REG_NUMBER : STRING(1.. 6); MODEL : STRING(1.. 20); end record; 20 10

Concurrent and Real-Time Programming with Ada Abstract data types package protected data type Concurrency Task creation Task execution Communication/synchronization Rendezvous Real time: Delay time period and Delay until next-time point Real-time scheduling/ Fixed-priority scheduling 21 Package --- Class/Object 22 11

Package : abstract data type in Ada package definition ---- specification Package body ---- implementation 23 Package definition -- Specificaiton Objects declared in specification is visible externally. package MY_PACKAGE is -- declare/define data structures Type myobject is record Name: string Personalnr: integer End myobject -- declare/define all public operations procedure myfirst_operation; procedure mysecond_operation; function mythird_operation (name: string) return myobject; end MY_PACKAGE; 24 12

Package body -- Implementation Implements package specification (you probably want to use some other packages here e.g.. ) with TEXT_IO; use TEXT_IO; package body MY_PACKAGE is procedure myfirst_operation is myfirst_operation code here; end; function MAX (X,Y :INTEGER) return INTEGER is end; procedure mysecond_operation is PUT_LINE( Hello Im Ada Who are U ); GET(); end; function mythird_operation (name: string) return myobject is end; end MY_PACKAGE; 25 Protected data type protected Buffer is procedure read(x: out integer) procedure write(x: in integer) private v: integer := 0 /* initial value */ protected body Buffer is procedure read(x: out integer) is x:=v end procedure write(x: in integer) is v:= x end (note that you can solve similar problems with semaphores) 26 13

Tasking 27 Ada tasking: concurrent programming Ada provides at the language level light-weight tasks. These often refered to as threads in some other languages. The basic form is: task T is -------- specification --- operations/entry (or simply: task T) end T; task body T is ---- processing---- end T; --------- implementation/body 28 14

Example: the sequential case procedure shopping is buy-meat; buy-salad; buy-wine; end Assume pre-defined procedures: buy-meat buy-salad buy-wine 29 The concurrent version procedure shopping is task get-salad; task body get-salad is buy-salad; end get-salad; task get-wine; task body get-wine is buy-wine; end get-wine; buy-meat; end buy-salad and buy-wine will be activated concurrently here And then run in parallel with buy-meat 30 15

Creating Tasks Tasks may be declared at any program level Created implicitly upon entry to the scope of their declaration. Possible to declare task types to start several task instances of the same task type 31 example procedure Example1 is task type A_Type; task B; A,C : A_Type; task body A_Type is --local declarations for task A and C --sequence of statements for task A and C end A_Type; task body B is --local declarations for task B --sequence of statements for task B end B; --task A,C and B start their executions before the first statement of this procedure. end Example1; 32 16

Task scheduling Allow priorities to be assigned to tasks in task definition Allow task dispatching policy to be set (Default: highest priority first) task Controller is pragma Priority(10) end Controller 33 Task termination: a task will terminate if: It completes execution of its body It executes a terminate alternative of a select statement It is aborted: abort_statement ::= abort task_name {, task_name}; 34 17

Communication/Synchronization 35 Task communication/synchronization Message passing using rendezvous entry and accept Shared variables protected objects/variables 36 18

Rendezvous procedure foo task T is entry E(...in/out parameter...); end; task body T is ------- accept E(......) do ------- sequence of statements end E; end T; T and user will be started concurrently task user; task body user is --- T.E(......) --- end... end 37 Rendezvous task body A is... B.ring;... end A task body B is... accept ring do... end Call... end A 38 19

This is implemented with Entry queues (the compiler takes care of this!) Each entry has a queue for tasks waiting to be accepted a call to the entry is inserted in the queue the first task in the queue will be accepted first (like the queue for a semaphore) By default, the queuing policy is FIFO it can be different queuing policies 39 An Example: Buffer task buffer is entry put(x: in integer) entry get(x: out integer) end; task body buffer is v: integer; loop accept put(x: in integer) do v:= x end put; accpet get(x: out integer) do x:= v end get; end loop; end buffer; --- buffer.put(...) Buffer.get(...) ---- --------------------------------------------- other tasks (users)!! 40 20

Potential deadlocks Task A:. B.b; accept a Task B:. A.a; accept b 41 An Example, the Semaphore The Idea of a (binary) semaphore Two operations, p and v p grabs semaphore or waits if not available v releases the semaphore 21

Program Semaphore using Task & RV The specification task type Semaphore is entry p; entry v; end Semaphore; The implementation task body Semaphore is loop accept p; accept v; end loop; end Semaphore; Program Semaphore using Task & RV The implementation: task body Semaphore is loop accept p; accept v; end loop; end Semaphore; 22

Using the Semaphore Declare an instance of a semaphore Lock : Semaphore; Now we can use this semaphore to create a monitor Lock.P;. code to be protected by the lock Lock.V; Choice: Select statement task Server is entry S1( ); entry S2( ); end Server; task body Server is loop --prepare for service select when <boolean expression> => accept S1( ) do --code for this service end S1; or accept S2( ) do --code for this service end S2; or terminate; end select; --do any house keeping end loop; end Server; 46 23

Real-Time Facilities (next lecture) 47 24