Title: Programming languages for real-time systems (extra slides, chapters 3 and 4)
1Programming languages for real-time
systems(extra slides, chapters 3 and 4)
2intermezzo de taal Ada
- taal ontworpen op aanvraag van DoD (Department
of Defense, US) - naam Ada komt van Countess Augusta Ada Lovelace
- dochter van dichter Lord Byron (England)
- schreef programmas voor de Difference Engine
van Charles Babbage, de eerste computer uit de
geschiedenis - was hierdoor de eerste computerprogrammeur
- volgende slides uit presentatie van DOD
3Comments on Ada in Relation to C
Center for Computer Systems Engineering Joint
Interoperability Engineering Organization Defens
e Information Systems Agency
4Why Ada? (Technical Arguments)
- In 1976, nothing else existed, so...
- Ada was designed with
- Software engineering in mind
- Large, complex projects in mind
- Standardization/validation in mind
- Reliability/maintainability in mind
- In 1996, still nothing else exists!
Ada Was Designed With Long-term Support for the
Warfighter in Mind
5IBM Weighted Scores for 6 Criterion Categories
Source IBM study, 1985
6Do Defects Really Matter?
- is 99.9 an acceptable tolerance for defects?
Consider - Drinking water contaminated for 1 hour per month.
- 16,000 letters lost every day by the US Post
Office. - 2 accidents per month at OHare International
Airport. - 20,000 prescriptions in error each year.
- 50 babies dropped on the delivery room floor each
hour. - 22,000 checks drawn from the wrong account per
hour.
OOPS!
7Software Crisis in 1976
- Proliferation of languages (gt450)
- Many dialects of standard languages
- Low interoperability
- High maintenance costs
- Low reliability
Result Ada
8Current State of Ada Use in DoD
SIGNIFICANT DECREASE IN No. OF 3GLs
(450)
NO. OF 3GLs
(37)
Ada is No. 1 For Weapons
Ada is No. 2 For AISs
Ada (33.5)
COBOL (59.2)
C (22)
Other (6.1)
Ada (22)
FORTRAN (13.2)
C 3.4
Other (10)
Jovial (9.3)
CMS-2 (12.5)
C (9)
9Ada in Maintenance Arena
270,000 LOC
100 productivityand 50 qualityincrease
doesnot reduce Cto Ada levels
225,000 LOC
135,000 LOC
150,000 LOC
112,500 LOC
75,000 LOC
Source MITRE (Avionics domain)
10Common Reasons for NOT Using Ada
- If I ignore it it will go away!
- Its too complex.
- I dont have time to learn another language.
- We dont have a compiler.
- We need more tools to use Ada.
- Blah, Blah, Blah!
11Expected Benefits of Ada
- Overall Advantage Increased Quality per Dollar
Spent - Code Portability
- People Portability
- Maintainability
- Reliability
- Common Basis for Tools/Methodologies
- Modularity, Managing Complexity
- Management Visibility, System View
- Improved Productivity
- Increased Reusability
12Why Ada? (Business Arguments)
- Ada
- Has better support for ilities
- Yields greater productivity
- Costs less per SLOC
- Has fewer defects
- Supported by multiple vendors
- Ada represents
- A DoD core competency
- Increased economies of scale
- An ISO and ANSI standard
- Freedom from proprietary solutions
Ada Makes Open Systems and Interoperability
Possible
13Why C?
- Major industry marketing
- Nothing equivalent for Ada
- Object-oriented programming support
- Did not exist in Ada 83, but does in Ada 95
- Builds on popularity of C
- C was free with UNIX operating systems
- Students were taught C
- Easier to move up to C than Ada
- Tools, bindings, visual environments and support
abundantly available (based on origin) - Ada was isolated
14Why C?
- Lower startup costs (, personnel, tools)
- Ada delivers more and thus costs more when only
development costs are considered - C gives programmers more freedom
- Less discipline is attractive to programmers -
dangerous on large systems - No standard for C
- Ada vendors must meet standard - more difficult
and more expensive
15Why C for Industry?
Industry and DoD have different focus
DoD
Commercial Sector
- sell many
- quick reaction to market forces
- large market
- large profit potential
- failures cost money
- buy one
- long-term planning precludes quick reaction
- limited market
- fixed profits
- failures cost lives
Source Nyberg, Karl presentation quoting
Gensler, The Defense Industry and Aharonian,
Greg, Microeconomic Definitions for Ada Summit,
1994.
16Software Development vs. Software Maintenance
- DoD looks at systems from total lifecycle
perspective - 60-80 of the lifecycle costs of software occur
in maintenance - Arguments based on lower development costs total
only 20-40 of the story - On most projects, development and maintenance are
two separate contracts - Commercial Off-The-Shelf (COTS) software is not
always the correct choice - Decisions must be made on lifecycle costs not on
development costs
17When Not to Use Ada
- Ada may not be appropriate
- When some other language has lower lifecycle
costs - For some RD applications where the intent is for
concept development only and the system will not
be fielded - For some prototype applications, however the
prototype must not be carried into ED for
subsequent fielding - When a compiler does not exist for the hardware
platform - When timing and/or sizing constraints make Ada a
technical infeasibility
18YF-22 Prototype Development
- Software for the aircraft (35 of total
avionics development cost) - Developed by 8 geographically separated
subcontractors - Developed using
- different Ada compilers
- different hardware platforms
- 12 major avionics subsystems
- 650 Ada modules
- Millions of SLOC
- Integrated in 3 days!
Source Guidelines for Successful Acquisition and
Management of Software Intensive Systems, Vol. I,
USAF, Feb 1995
19Summary Ada vs C
2002
- Ada is
- Technically superior
- Demonstrably lower in cost through entire
lifecycle - Successfully used today and growing
- Supported by DISA
Ada was explicitly designed to support large
systems well into this century
20hoofdstuk 3kort overzicht van Ada, C en
Javaprogramming in the small
21belang van goede syntax
- (oud) voorbeeld van een slechte syntax Fortran
lus tot label 20, iteratie van I van 1 tot 100
DO 20 I 1, 100
een programmeur schreef voor de U.S. Viking
Venus Probe DO 20 I 1. 100 de compiler
interpreteerde dit als een toekenning en negeerde
de blancos DO20I 1.100 variabelen hoeven
niet gedeclareerd te worden in Fortran, en de
variabelen die met een D beginnen worden
verondersteld reals te zijn (met de D van double)
en 1.100 is een real !
22algemene stijl een blok in Ada
- declare
- ltdeclarative partgt
- begin
- ltsequence of statementsgt
- exception
- ltexception handlersgt -- voor het behandelen van
fouten, zie H6 - end
- declare
- Temp Integer A -- initiële waarde wordt
gegeven aan Temp - begin
- A B -- is de toekenningsoperator
- B Temp
- end -- in dit voorbeeld geen exception deel
23algemene stijl een blok in C
algemene stijl een blok in C en Java
-
- ltdeclarative partgt
- ltsequence of statementsgt
-
-
- int temp A / declaratie en initialisatie /
- / merk op in C staat de naam van het type
eerst / - / in Ada komt het na de naam van de
variabele / - A B / toekenningsoperator is /
- B temp
- / Java eventueel ook exception handlers,
zie H6 /
24gegevenstypen
gegevenstypen
- Ada is sterk getypeerd in toekenningen en
expressies moeten objecten van hetzelfde type
zijn (expliciete conversies zijn mogelijk) - Java is ook sterk getypeerd
- C biedt veel minder type-controle
- sterke typering bevordert betrouwbaarheid de
compiler kan consistentie van gebruik van
gegevens testen
25types in C
types in C
-
- typedef enum (xplane, yplane, zplane) dimension
- / typedef introduceert een naam voor een nieuw
type / - / de naam is hier dimension /
- / enum zegt dat het om een enumeratie gaat /
- dimension line, force
- line xplane
- force line 1
- / force is nu yplane /
- / 1 wordt hier in volledig andere betekenis
gebruikt dan normaal / -
- Java biedt geen enumeratie type
26types in Ada
types in Ada
- type Dimension is (Xplane,Yplane,Zplane)
- type Map is (Xplane,Yplane)
- Line, Force Dimension
- Grid Map
- begin
- Line Xplane
- Force DimensionSucc(Xplane)
- Grid Yplane
- Grid Line -- mag niet, verschillende types
- end
27types in Ada (vervolg)
types in Ada
- subtype Surface is Dimension range Xplane ..
Yplane - type New_int is new Integer
- type Projection is new Dimension range Xplane ..
Yplane - D Dimension
- S Surface
- P Projection
- begin
- D S -- OK
- S D -- legaal, maar zou kunnen run-time
fout genereren - P D -- illegaal
- P Projection(D) -- OK, met expliciete
conversie - end
28types in C (vervolg)
types in C en Java
- in C
- typedef int newint
- typedef dimension projection
- / biedt niet dezelfde protectie als in Ada /
- / typedef is synoniem voor een ander type, geen
afgeleid type / - in Java
- nieuwe types worden gecreëerd via
object-oriëntatie, zie H4
29gestructureerde gegevenstypes in Ada
- Max constant Integer 10
- type Reading_T is array (0 .. Max-1) of Float
- Size constant Integer Max-1
- type Switches_T is array(0 .. Size, 0 .. Size) of
Boolean - Reading Reading_T
- Switches Switches_T
30gestructureerde gegevenstypes in C
gestructureerde gegevenstypes in Java
- static final int max 10 // definitie van
constante -
- float reading new float MAX // index is 0
.. max -1 - boolean switches new booleanMAX MAX
-
- // merk op in Java zijn rijen objecten
-
31gestructureerde gegevenstypes in C
gestructureerde gegevenstypes in Java
- define MAX 10 / manier om een constante te
definiëren / - / zonder typevoordeel /
- typedef float reading_tMAX / index is 0 ..
MAX-1 / - typedef short int switches_tMAX MAX
- / er zijn geen booleans in C /
- reading_t reading
- switches_t switches
32gestructureerde gegevenstypes in Ada (vervolg)
gestructureerde gegevenstypes in Ada
- type Day_T is new Integer range 1 .. 31
- type Month_T is new Integer range 1 .. 12
- type Year_T is new Integer range 1900 .. 2050
- type Date_T is
- record
- Day Day_T 1 -- initiële waarde is 1
- Month Month_T 1 -- idem
- Year Year_T -- geen initiële waarde
- end record
33gestructureerde gegevenstypes in C (vervolg)
gestructureerde gegevenstypes in C
- typedef short int day_t
- typedef short int month_t
- typedef short int year_t
- struct date_t
- day_t day
- month_t month
- year_t year / naam hiervan is struct
date_t / - typedef struct
- day_t day
- month_t month
- year_t year date2_t / dit is een nieuw type
met naam date2_t /
34gestructureerde gegevenstypes in C (vervolg)
gestructureerde gegevenstypes in Java
- class Date
-
- int day, month, year
-
- Date birthday new Date()
- birthdate.day 31
- birthdate.day 1
- birthdate.day 2000
35dynamische gegevenstypes in C
dynamische gegevenstypes in C
-
- typedef struct node
- int value
- struct node next / pointer naar een
record hier gedefinieerd / - node_t
- int V
- node_t Ptr
- Ptr malloc (sizeof(node_t)) / dynamische
allocatie van geheugen / - Ptr-gtvalue V
- Ptr-gtnext 0 / de null-pointer bestaat niet
/ - ...
36dynamische gegevenstypes in Ada
dynamische gegevenstypes in Ada
- type Node -- definitie zal volgen
- type Ac is access Node
- type Node is
- record
- Value Integer
- Next Ac
- end record
- V Integer
- A1 Ac
- begin
- A1 new Node -- dynamische allocatie van
geheugen - A1.Value V
- A1.Next null
- . . .
- end
37dynamische gegevenstypes in C (vervolg)
dynamische gegevenstypes in C
- typedef date_t events_tMAX, next_event_t
- / events_t is een type voor arrays van Max el.
van type date_t - next_event_t is type voor pointers naar el.
van type date_t / - events_t history
- next_event_t next_event
- next_event history0 / adres van eerste
element van rij / - next_event / de pointer gaat nu wijzen
naar het / - / volgende element in de rij /
- / een pointer in C kan naar om het even wat
wijzen, probleem van - hangende pointers (dangling pointer) /
38dynamische gegevenstypes in C (vervolg)
dynamische gegevenstypes in Java
- elk object in Java is een referentie naar het
actuele object met zijn data, voor de rest zijn
er geen pointers voorzien in Java - class node
-
- int value
- node next
-
- Node Ref1 new Node()
- Node Ref 2 new Node()
- if (Ref1 Ref2) // vergelijkt adressen
en geen inhoud
39controle structuren leeg blok
controle structuren leeg blok
- in Ada
- begin
- null
- end
- in Java en C
- / gewoon niets /
-
40controle structuren if-then-else
controle structuren if-then-else
- in Ada
- if A / 0 then
- if B/A gt 10 then
- High True
- else
- High False
- end if
- end if
in C if (A ! 0) if (B/A gt 10) high 1 else
high 0 // in Java mag die ambiguïteit
niet if (A ! 0) if (B/A gt 10) high
1 else high 0
41controle structuren if-then-else in Ada (vervolg)
controle structuren if-then-else in Ada
- if Number lt 10 then
- Num_Digits 1
- elseif Number lt 100 then
- Num_Digits 2
- elseif Number lt 1000 then
- Num_Digits 3
- elseif Number lt 10000 then
- Num_Digits 4
- else
- Num_Digits 5
- end if
42controle structuren case
controle structuren case
in Java en C switch (command) case A
case a action1 break case t
action2 break case e action3
break case x case y case z
action4 break default break
- in Ada
- case Command is
- when A a gt Action1
- when t gt Action2
- when e gt Action3
- when x .. z gt Action4
- when others gt null
- end case
43controle structuren while-lus
controle structuren while-lus
in Java en C while (ltexpressiongt) /
expressie 0 eindigt lus / ltstatementgt whil
e (1) ... if (ltexpressiongt) break ...
- in Ada
- while ltBoolean Expressiongt loop
- ltStatementsgt
- end loop
- loop
- ...
- exit when ltBoolean Expressiongt
- ...
- end loop
44controle structuren for-lus en oneindige lus
in Java en C for (i 0 i lt 9 i) Ai
i while (1) ltstatementgt
- in Ada
- for I in 0 .. 9 loop
- A(I) I
- end loop
- loop
- ltStatementsgt
- end loop
45parameters bij subprogrammas in Ada
parameters bij subprogrammas in Ada
- drie modes
- in data wordt doorgegeven aan het subprogramma
- out data wordt doorgegeven aan het
oproepende programmaonderdeel - in out data wordt doorgegeven aan het
subprogramma, wordt daar eventueel gewijzigd en
wordt dan weer doorgegeven aan het oproepende
programma- onderdeel - procedure Quadratic ( A, B, C in Float
- R1, R2 out Float
- Ok out Boolean)
- bij functies mogen alleen in parameters
gebruikt worden
46parameters bij subprogrammas in C
parameters bij subprogrammas in C
- maar 1 parametermechanisme
- by value data wordt enkel doorgegeven aan het
subprogramma - om resultaten te doen terugkeren naar het
oproepende programmaonderdeel moeten pointers
gebruikt worden - void quadratic ( float A, float B, float C,
- float R1, float R2, int OK)
- merk op
- - C heeft geen sleutelwoord om een subprogramma
aan te geven - - een procedure in C is een functie die niets
(void) terug geeft
47parameters bij subprogrammas in C
parameters bij subprogrammas in Java
- primitieve argumenten worden gekopieerd
- variabelen van classe-type
- zijn reference variabelen, worden doorgegeven
als referentie - argument dat niet mag gewijzigd worden door
functie final - public class Roots
- float R1, R2
-
- boolean quadratic ( final float A, final float B,
final float C, Roots R) - merk op
- - de boolean vlag is de teruggeefwaarde van de
functie - - Roots is een klasse R1 en R2 kunnen gewijzigd
worden
48procedures in Ada
procedures in Ada
- procedure Quadratic (A, B, C in Float
- R1, R2 out Float
- Ok out Boolean) is
- Z Float
- begin
- Z BB - 4.0AC
- if Z lt 0.0 or A 0.0 then
- Ok False
- R1 0.0 R2 0.0
- return
- end if
- Ok True
- R1 (-B Sqrt(Z)) / (2.0A)
- R2 (-B - Sqrt(Z)) / (2.0A)
- end Quadratic
49procedures in C
procedures in C
- void quadratic ( float A, float B, float C,
float R1, float R2, int OK) -
- float Z
- Z BB - 4.0AC
- if (Z lt 0.0 A 0.0)
- OK 0
- R1 0.0 R2 0.0
- return
-
- OK 1
- R1 (-B SQRT(Z)) / (2.0A)
- R2 (-B - SQRT(Z)) / (2.0A)
-
- ...
- quadratic (F1, F2, F3, Q1, Q2, S)
50procedures in C
procedures/functies in Java
- public class Roots
- float R1, R2
-
- boolean quadratic ( final float A, final float B,
final float C, Roots R) - float Z
- Z (float) (BB - 4.0AC)
- if (Z lt 0.0 A 0.0)
- R.R1 0f
- R.R2 0f
- return false
-
- R.R1 (float) (-B Math.sqrt(Z)) / (2.0A)
- R.R2 (float) (-B - Math.sqrt(Z)) / (2.0A)
- return true
51functies in Ada
functies in Ada
- function Minimum (X, Y in Integer) return
Integer is - begin
- if X gt Y then
- return Y
- else
- return X
- end if
- end Minimum
52functies in C
functies in C
- int minimum (int X, int Y)
-
- if (X gt Y) return Y
- else return
-
- voorbeeld van een macro (en tegelijk ook van
mogelijk cryptische schrijfwijze van if-then-else
in C) - define MIN(X, Y) ((X gt Y) ? (Y) (X))
53intermezzoJava for (real-time and) embedded
systems
54developing embedded SW in Java
- overview technology and architecture
- why Java ?
- Java Virtual Machine Technology
- alternative technologies
- supporting technologies
55what is so great about Java ?
- programming language
- familiar C-like (but improved) syntax
- all test conditions must be Boolean
- e.g. while (x3) is not allowed
- primitive data types have fixed sizes
- easier to produce bug-free software
- array bounds checking
- automatic garbage collection
- built-in exception-handling
- built-in library for concurrency
- truly object-oriented
- simpler to learn than C
56compileren vs interpreteren
- compileren (vertalen)
- vertalen van programma in een hogere
programmeertaal - naar machine code
- met gepaste oproepen naar een besturingssysteem
- resultaat heet object code
- object code is niet draagbaar
- object code wordt verdeeld, bron code is
beschermd - programma wordt volledig vertaald voordat het
uitgevoerd wordt - één maal vertalen, ontelbare keren uitgevoeren
- linken (bij grote programmas)
- elk onderdeel van een groter programma wordt
apart vertaald - verschillende onderdelen worden samen gelinked
57compileren vs interpreteren
- interpreteren (vertolken)
- een programma wordt tijdens de uitvoering, regel
per regel vertaald en uitgevoerd - vertaling gebeurt bij elke uitvoering vertraging
- bij een lus worden de de instructies even vaak
vertaald als er iteraties zijn nog extra
vertraging - programma is draagbaar
- bron code wordt verdeeld (nadeel voor verkopers)
- tijdswinst bij ontwikkeling
- stukjes programma kunnen uitgevoerd worden
- geheel hoeft niet gecompileerd te worden
58what is so great about Java ?
- bytecodes
- Java bytecode portable binary format
- instruction set for a virtual machine
- platform portability, without source code
- source translation is done in advance
- faster than a fully interpreted solution
- can be translated into native opcodes
- at each encounter (bytecode interpreter)
- at first encounter (just-in-time compiler)
- prior to loading (ahead-of-time compiler)
59what is so great about Java ?
Java Source
Java bytecode
libraries
Java compiler
Java bytecode
60what is so great about Java ?
- class libraries
- high level of abstraction
- standard APIs
- increase application portability
- reduce programming effort
- support for multitasking (see CH 7-9)
- Thread class and Runnable interface
- synchronized keyword
- monitors via wait() and notify()
- support for networking
61why use anything else ?
- problems with Java
- no direct access to hardware (see CH 15)
- Java has no pointers
- nor bytecodes to access a physical address
- but Java can call C ! (Java Native Methods)
- size of code JVM can be huge
- inefficiencies
- bytecode interpretation is slower than C/C
- garbage collection requires processor time
- interpreter or JIT may not be optimal compiler
- unpredictability (see CH 13)
- garbage collector may preempt a running task
- Just-in-Time compilation slows first access
62JVM technology components
Java Threads
C/C Tasks
Class libs
Java Virtual Machine ( garbage collector)
Multitasking OS
Native methods
Processor and other hardware
63inside a virtual machine
64inside a virtual machine
- execution engine
- bytecode interpreter
- just-in-time compilation
- hybrid
- Suns HotSpot compiler
65inside a virtual machine
- garbage collector (biggest myth for Java)
- many algorithms, some even hard RT suitable
- naive
- incremental
- real-time (double heap, HW assisted)
- bounded worst-case allocation time (faster than
incremental GC) - bounded worst-case object access time
- slower average-case allocation time
- slower object access time
66inside a virtual machine
- dynamic Class Loader
- find and load referenced class
- read file ltpackagegt/ltclassgt.class in the
CLASSPATH - ZIP files are also traversable, can be placed in
ROM, if necessary - verify class file format
- link place contents into a JVM data structure
- prepare and initialize the class
67inside a virtual machine
- bytecode verifier
- detects and prevents illegal activity
- verification is optional!
- of little value in a closed system
68inside a virtual machine
- Java Class Libraries
- Write Once, Run Anywhere
- only if the same set of class libraries are
available! - multiple standard platforms (APIs)
- Standard Java - the full set, for desktops and
workstations - PersonalJava - a subset of the above, for smaller
devices - EmbeddedJava - like the previous, but more
memory-conscious
69Inside a virtual machine
- Java Class Libraries
- upward compatibility is desirable
- EmbeddedJava applications also run on any
PersonalJava platform - PersonalJava applications also run on any
Standard Java platform
70JVM requirements memory
- ROM JVM libraries application
- JVM 500K is typical maximum
- Class libraries 500K for PersonalJava
- the application itself
- RAM JVM heap thread stacks
- affected by engine type
- JIT compiler requires large storage area,
interpreter does not - heap size is application-dependent
- each thread requires its own stack
71JVM requirements processor
- capabilities
- arithmetic
- 32-bit integers assumed cheap and fast, 64-bit
available - 32-bit and 64-bit floating point (IEEE 754)
available - processing power
- need enough to negate interpretation slowdown
- address space
- JVM all class libraries requires a lot of
memory!
72JVM requirements OS
- JVM is not (usually) an OS replacement
- RTOS or kernel expected underneath
- only minimal functionality required
- thread creation and deletion (see CH 7)
- priority-based scheduling (see CH 13)
- at least 10 priority levels
- synchronization primitives (e.g., mutexes) (see
CH 8) - private thread stacks (see CH 7)
73JVM requirements other SW
- JVMs are written in C, therefore
- Standard C library
- Standard math library
- dynamic memory allocation
- malloc() is called internally
- Java class libraries
- java.net - assumes a TCP/IP stack
- java.awt - requires a graphics API
- others? - implementation-specific
74alternative technologies
- traditional JVM
- too big
- too slow
- too unpredictable
- alternatives
- Ahead-of-time compilers
- Java processors
- JavaCard technology
- Kjava Virtual Machine
75supporting technologies
- Java Native Interface (JNI)
- call legacy code
- work around Javas limitations
- implement higher-performance functionality
- native methods C functions (no good
integration with C)