Real Time Systems - 7th Sem - ECE - VTU - Unit 4 - Languages for Real Time Applications -...

35
Unit 4: Languages for Real-time Applications Introduction Real-time software in real-time systems: Producing places - heavy demands on programming languages Must be readable Failure can be expensive  in terms of production, loss of life (Ex.: aircraft control system) Frequently large and complex  makes, development and maintenance costly Have to respond to the external events   with a guaranteed response time Involve with  wide range of interface devices  including non-standard devices To obtain necessary speed  efficiency in use of the computer hardware is vital Early RTS  programmed with assembly level languages  to Make efficient use of CPU Access interface devices and support interrupts Assembly coding: still widely used for  small systems with high computing speed requirements for small systems which will be used in large numbers (here, high cost of development is offset by the reduction in unit c ost through having a small, efficient, program New languages like CORAL 66, RTL/2 and C, and others   developed  for programming embedded computers Limitations of these languages: designed  for producing sequential programs Rely on operating system support for concurrency RT language requirements  subsume  requirements of a general purpose language

Transcript of Real Time Systems - 7th Sem - ECE - VTU - Unit 4 - Languages for Real Time Applications -...

7/30/2019 Real Time Systems - 7th Sem - ECE - VTU - Unit 4 - Languages for Real Time Applications - Ramisuniverse

http://slidepdf.com/reader/full/real-time-systems-7th-sem-ece-vtu-unit-4-languages-for-real-time 1/35

Unit 4: Languages for Real-time Applications

Introduction

Real-time software in real-time systems:

Producing places - heavy demands on programming languages

Must be readable

Failure can be expensive – in terms of production, loss of life (Ex.: aircraft control system)

Frequently large and complex – makes, development and maintenance costly

Have to respond to the external events – with a guaranteed response time

Involve with – wide range of interface devices – including non-standard devices

To obtain necessary speed – efficiency in use of the computer hardware is vital

Early RTS – programmed with assembly level languages – to

Make efficient use of CPU

Access interface devices and support interrupts

Assembly coding:

still widely used for – small systems with high computing speed requirements

for small systems which will be used in large numbers (here, high cost of development is offset

by the reduction in unit cost through having a small, efficient, program

New languages like CORAL 66, RTL/2 and C, and others – developed – for programming

embedded computers

Limitations of these languages:

designed –  for producing sequential programs

Rely on operating system support for concurrency

RT language requirements – subsume – requirements of a general purpose language

7/30/2019 Real Time Systems - 7th Sem - ECE - VTU - Unit 4 - Languages for Real Time Applications - Ramisuniverse

http://slidepdf.com/reader/full/real-time-systems-7th-sem-ece-vtu-unit-4-languages-for-real-time 2/35

Barnes (1976) and Young (1982): divided the requirements – that a user looked for a in a

programming language into six general areas

1.  Security

2.  Readability

3.  Flexibility4.  Simplicity

5.  Portability

6.  Efficiency

Basic features of languages – how, meeting the requirements of the user – are

1.  Variables and constants: declarations, initialization

2.  Data types – including structured types and pointers

3.  Control structures and program layout and syntax

4. 

Scope and visibility rules5.  Modularity and compilation methods

6.  Exception handling

RTS languages – to be to support concurrency – for this the required things are:

1.  Construction of modules (software components)

2.  Creation and management of tasks

3.  Handlin of interrupts and devices

4.  Inter-task communication

5.  Mutual exclusion

6.  Exception handling

Providing of above facilities – by modern languages – are of 2 types:

1.  To provide – a minimum set of language mechanisms – with the ability to extend the set

It’s simple, clean language, but at the expense of standardization – as each user creates

additional facilities

2.  To provide – a more extensive set of mechanisms from which the programmer can

choose

It’s more complex, but standardized 

Ex.: Ada, CONIC, CUTLASS, and others

1 Security

Measured in terms of  – how effective the compiler and RTS – in detecting programming errors

automatically

7/30/2019 Real Time Systems - 7th Sem - ECE - VTU - Unit 4 - Languages for Real Time Applications - Ramisuniverse

http://slidepdf.com/reader/full/real-time-systems-7th-sem-ece-vtu-unit-4-languages-for-real-time 3/35

Chance of errors – which cannot be detected(Ex.: errors in logical design of the program): can be

reduced – if the language encourages the programmer to write clear, well-structured, code

Features which assist the detection of errors by the compiler – include:

1.  Good modularity support

2.  Enforced declaring of variables

3.  Good range of data types, including sub-range types

4.  Typing of variables, and

5.  Unambiguous syntax

Software to test exhaustively –  it’s not possible

Intrinsic security of the language is major importance for the production of reliable programs

Fundamental requirements of RTS – they operate reliably

In RTS:

compilation is often performed on – different compute han the one used in the actual system

Run-time testing:

expensive

can interfere with the hardware development program

run-time testing has to be done on the actual hardware and later on the hardware connected to

plant

Error detection:

1.  Economically, it is important to detect the errors – at the compilation stage – rather thanat run-time: because, the earlier – the error detected – the less it costs to correct it

2.  Checks done at compilation time have no run-time overheads – it is important as the

program will be run many times than it is compiled

Run-time checking:

7/30/2019 Real Time Systems - 7th Sem - ECE - VTU - Unit 4 - Languages for Real Time Applications - Ramisuniverse

http://slidepdf.com/reader/full/real-time-systems-7th-sem-ece-vtu-unit-4-languages-for-real-time 4/35

Reliance: frequently requires additional code to be inserted in the program (normally done by the

complier) – leads to increase in the program size – it decreases the execution speed

Strong typing – gives good security – at compilation time

Q

Readability

It’s the measure of the ease with which the operation of the program can be understood – without

resort to supplementary documentation – such as flow chart or natural language descriptions

Emphasis is on ease of reading code: because, particular segment of code – will be written only

once – but will be read many times

Benefits of good readability:

1.  Reduction in documentation costs:Code itself provides the bulk of documentation

Particularly valuable in projects with a long life expectancy (where series of 

modifications will be)

Difficult and costly – obtaining up-to-date documentation and keeping those up-to-date

2.  Easy error detection:

Clear readable code makes errors

Ex.: logical errors – easier to detect – hence, increases portability

3.  Easy maintenance:

It is required – when the modifications to a program are required – the person responsible

for making the modifications was not involved in the original design – changes can only

be made quickly and safely – if the operation of the program is clear

Factors which affect readability – are manifold and some extent depends on personal preference

1.  Co-operation of the programmer – is needed - if, in means – understandable programs

can be written

2.  Syntax of the language and the layout of the code statements

Readability – of the program – can be increased by – adoption of a clear layout – whichemphasizes the structure – by careful choice of variable names

Disadvantages of readability more of languages:

1.  Source code is longer

2.  Takes longer time to write

7/30/2019 Real Time Systems - 7th Sem - ECE - VTU - Unit 4 - Languages for Real Time Applications - Ramisuniverse

http://slidepdf.com/reader/full/real-time-systems-7th-sem-ece-vtu-unit-4-languages-for-real-time 5/35

Languages with more readability – provides security and maintainability easier

Flexibility

Languages must provide

All the features necessary for the expression of all the operations – required by the application

without requiring

the use of complicated constructions and tricks

resort to assembly level code inserts

RTS for – flexibility to above features by languages is important – since, frequently non-

standard I/O devices – will have to controlled

Security vs flexibility

More flexible – can cause problem to security

Compromise – is needed – in languages – to provide high flexibility and security

In modern languages – compromise is doen by – providing hgh flexibility and, through

the module or package concept – a means by which the low-level i.e., insecure operations can be

hidden in a limited number of self-contained sections of the program

Simplicity

Simple in design is preferred than complex

Contributes to security

Reduces the cost of training

Reduces the probability of programming errors – arising from misinterpretation of the language

features

Reduces compiler size

Leads to more efficient object code

Associated another factor – consistency – a good language should not impose arbitrary

restrictions or relaxations on the use of any feature of the language

7/30/2019 Real Time Systems - 7th Sem - ECE - VTU - Unit 4 - Languages for Real Time Applications - Ramisuniverse

http://slidepdf.com/reader/full/real-time-systems-7th-sem-ece-vtu-unit-4-languages-for-real-time 6/35

Portability

Desirable for:

speeding of development

reducing costs

increasing security

portability - difficult to achieve in practice

surface portability: has improved with the standardization agreements on many languages

it possible – to transfer often – a program in source code form from one computer to another

and to find that it will compile and run on the computer to which it has been transferred

Problems:

1.  word lengths of the two machines – differ

2.  precisions – with which numbers – are represented even on computers with the same

word lenght

3.  computer hardware and the operating system features - is used by RTS – since portability

going to difficult

solution: accept that a RTS – will not be directly portable, and to restrict the areas of non-

portability to specific modules by restricting the use of low-level features – to a restricted

range of modules

Enhancing of portability - by writing the application software – to run on a virtual machine –  

rather than for a specific operating system

Changes in computer and operating system – then requires – the provision of new support

software to create a virtual machine on the new system

Efficiency

RTS – must:

provide – a guaranteed performance

meet specific time constraints

with efficiency

7/30/2019 Real Time Systems - 7th Sem - ECE - VTU - Unit 4 - Languages for Real Time Applications - Ramisuniverse

http://slidepdf.com/reader/full/real-time-systems-7th-sem-ece-vtu-unit-4-languages-for-real-time 7/35

Early computer systems –  

great emphasis – was paced on the efficiency of the coding – for size of the object code and

speed of the operation – since, computers were expensive and slow

programming – with assembly languages was with and small and f ast code for ‘tricks’ uses were 

Emphasis – was on efficiency, rather than security and readability

Speeder and less cost of hardware – made emphasis shifted from efficiency to other aspects like

security and cost of writing and maintaining the program speed, and the compactness of the

object code

Speed and compactness needed in applications like: consumer market

Speed matters in applications like: control of electromechanical systems. Aircraft controls, and inthe general area of signal processing (Ex.: speech recognition)

Efficiency of the language – depends much more on – compiler and the run-time support – than

on the actual language design

SYNTAX LAYOUT AND READABILITY

Language syntax and readability – have the major impact on readability of code written in the

language

Program fragment: 1

1.  Problems in it: No explanation, No comments, Short identifiers like NST and ST are used

2.  Readability is less

Program fragment: 2

7/30/2019 Real Time Systems - 7th Sem - ECE - VTU - Unit 4 - Languages for Real Time Applications - Ramisuniverse

http://slidepdf.com/reader/full/real-time-systems-7th-sem-ece-vtu-unit-4-languages-for-real-time 8/35

1.  Problems – been cleared which were in Program fragment 1

2.  Poblems in program fragment 2

Code is not easy to read – since all upper case letters are used in it

Program fragment: 3

1.  Problems – been cleared which were in Program fragement 2

2.  Readability been improved by –  

Using uppercase letters – for keywordsUsing lowercase letters – for user-defined entitites

Program fragment: 4

1. 

Problems – been cleared which were in Program fragment 32.  No embedded spaces and tab characters – to improve the layout

Exact form of layout adopted: is a matter of house style or the personal preference

Modula- 2

Reserved words – written in uppercase letters

Case sensitive – Ex.: Time and time are different

7/30/2019 Real Time Systems - 7th Sem - ECE - VTU - Unit 4 - Languages for Real Time Applications - Ramisuniverse

http://slidepdf.com/reader/full/real-time-systems-7th-sem-ece-vtu-unit-4-languages-for-real-time 9/35

Ada

Case insensitive

Convention: reserved words are written in lower case, application entities are written in upper

case

Underscore characters – used as the separator for the compound names

Cooling ( 1991, p. 269): reports that some Ada programmers – consider the adoption of 

convention above – to have been a mistake in that it makes it more difficult to read the code

Program fragment: 5

It uses Ada programming language

DECLARATION AND INITIALISATION OF VARIABLES AND CONSTANTS

Declarations

Declaration purposes:

1.  Provide the compiler with information on the storage requirements

2.  To inform the system explicitly of the names being used

Languages such as Pascal, Modula-2, and Ada:

1.  Require all objects to be specifically declared

2.  A type should be associated with the entity when it is declared – this allows the compiler

to check that the entity is used only in operations associated with that type

Ex.: an entity declared as o type REAL, if as an operand used – in logical operation –  

compiler should detect the type incompatibility and flag the statement as being incorrect

Languages such as FORTRAN and BASIC:

1.  Do not require explicit declarations

2.  First use of the name is deemed to be its declaration

In FORTRAN:

1.  explicit declaration is optional

7/30/2019 Real Time Systems - 7th Sem - ECE - VTU - Unit 4 - Languages for Real Time Applications - Ramisuniverse

http://slidepdf.com/reader/full/real-time-systems-7th-sem-ece-vtu-unit-4-languages-for-real-time 10/35

2.  entities can be associated with a type if declared

3.  implicit typing takes place – if entities are not declared explicitly – names beginning with

letters I-N – are assumed to be integer numbers and names beginning with any other

letter are assumed to be real numbers

Optional declarations are dangerous: they can lead to the costruction of syntactically coorect butfunctionally erroneous programs – Ex.: ERROR and EROR – will be considered as two different

variables- FORTRAN – compilers – cannot detect these kind of errors – this can be avoided by –  

insisting that all entitites are declared and the code is processed by a preprocessor which checks

that all names used are mentioned in declaration statements

Implicit typing in FORTRAN – all also, can lead to confusion and misinterpretation

REAL KP, KD, KI

INTEGER DACV, ADCV

… 

100 DAVC=KP*(KD/KI)

… 

END

Progammer reading the statement with the label 100 – without reference to the declaration

statement – might assume that the variables on the RHS of the statement  – are all integers, and

that the resultant value will – then be floated – and assigning to the real variable – where as thestatement is doing just opposite – variables KP, KD, and KI – are real – and the resultant is

truncated – and assigned to an integer variable –  

Solution:

Insist that all variable names – conform to the implicit typing rules

Meaningful names that do not conform are prefixed with an appropriate letter

Normally – implicit integer name used for an integer is prefixed I - and a normally implicit

integer name used for a real number is prefixed X

REAL XKP, XKD, XKI

INTEGER IDACV, IADCV

100 IDACV = XKP*(XKD/XKI)

7/30/2019 Real Time Systems - 7th Sem - ECE - VTU - Unit 4 - Languages for Real Time Applications - Ramisuniverse

http://slidepdf.com/reader/full/real-time-systems-7th-sem-ece-vtu-unit-4-languages-for-real-time 11/35

Initialisation

Useful – if a variable – can be given an initial value – when it is declared

It’s bad practice to rely on the complier – to initialize variables to zero or some other value –  

strictly, it is not necessary – as a value can always be assigned to a variable

For security languages – compiler checks that a variable – is not used before it has had a value

assigned to it

Ex.: In Modula – 2: security is enhanced by – the compiler checking that – all variables have

been given an initial value

Weakness of Modula – 2: variables cannot be given an initial value when they are declared but

have to be initialized explicitly – using an assignment statement

Constants

Entities – with constant values – in the program – are constants, for ex.: physical entities ( speed

of light in vacuum) and mathematical entities (pi value) or because they are a parameter which is

fixed for that particular implementation of the program, for ex.: number of control loops being

used or the bus address of an input or output device

Always it is possible to provide constants by initializing a variable to the appropriate quantity –  

but this has the disadavantage – that the compiler – cannot detect if a further assignment – is

made with changes the value of the constants

It is also confusing to the reader – there is no indication which entities are constants – and, which

are variables – unless, the initial assignment is carefully documented

Pascal

Provides a mechanism for declaring constants

Constant declarations must precede the type declarations

Only constants of the predefined types can be declared  –  it’s the severe restriction on the

constant mechanism

Ex.: this cannot be possible to do

TYPE AMotorState =(OFF, LOW, MEDIUM, HIGH);

CONST M motorStop=AMotorState(OFF);

7/30/2019 Real Time Systems - 7th Sem - ECE - VTU - Unit 4 - Languages for Real Time Applications - Ramisuniverse

http://slidepdf.com/reader/full/real-time-systems-7th-sem-ece-vtu-unit-4-languages-for-real-time 12/35

Value of the constant – must be known at the compilation time

Expressions are not permitted in constant declarations

Modula – 2 language:

Modula – 2 in restricition on the use of expressions in constant declarations is removed

Ex.: in Modula-2

CONST message –  ‘a string of characters’; 

Length = 1.6;

Breadth = 0.5;

Area = length * breadth;

Ada language:

Value of the constant can be assigned at run-time

Costants – can be considered as – special variables, which become read only following the first

assignment to them – in Ada

MODULARITY AND VARIABLES

Scope of avariabe: Region of the program in which the variable is potentially accessible or

modificable

‘Visible’ of the variables: regions in which it may actually be accessed or modified

Most languages provide – mechanism – for controlling scope and visibility – there are 2 general

approaches for this

1.  languages such as FORTRAN provide a single level of locality

2.  languages such as Modula – 2 provide multi-level locality

In block structured languages: entities which are declared within a block may only be referenced

inside that block 

Blocks – can be nested and scopes extends throughout any nested blocks

7/30/2019 Real Time Systems - 7th Sem - ECE - VTU - Unit 4 - Languages for Real Time Applications - Ramisuniverse

http://slidepdf.com/reader/full/real-time-systems-7th-sem-ece-vtu-unit-4-languages-for-real-time 13/35

Nested procedure in Modula – 2

In nested procedures – entities declared in an inner block may have the same name as an entity

delcred in an outer bock  – this doesnot cause any confusion to the compliler – which simply

provides the new storage for the entity in the inner block and the entity in the outer block  –  

temporarily disappears – to reappear when the inner block is left

In Modula-2:

Variables declared inside a procedure have storage allocated to them only while that procedure is

being exceuted – when the procedure is entered storage in RAM is allocated from available

memory often referred to as heap – if no memory is available, a run-time error is generated –  

when an exit is made from the procedure – the memory is released and can be reused

This dynamica allocation and release of memory leads to two problems:

1.  variabes declared within a procedure cannot be used to hold values for reuse on the next

entry to the procedure

2.  if a procedure is called recursively it is possible that the program may fail because there

is no more memory available

Modula-2: overcomes first problem – by its use of a program unit MODULE – which has some

specific properties

Second problem – to overcome – recursively called procedures should not be used in RTS,

particlualry if failure will compromise the safety of people or equipment – even if the procedures

do not declare any variables each cll makes demands on the stack space for storage of its volatile

environment

Maximum memory requirement to be predicting possible – for a system to have - is safer

Global and Local Variables

7/30/2019 Real Time Systems - 7th Sem - ECE - VTU - Unit 4 - Languages for Real Time Applications - Ramisuniverse

http://slidepdf.com/reader/full/real-time-systems-7th-sem-ece-vtu-unit-4-languages-for-real-time 14/35

Compiler can easily handle the reuse of names –  it’s not easy for the programmer to use - the

deeply nested – PROCEDURE blocks with the reuse of names – this can compromise the

security of a Pascal or Modula-2 program

Program illustrates the reuse of names – can cause confusion as to which entitiy is being

referenced

Loss of visibility in nested procedures

Global and local variables – argumetns – is as fierce as that of GOTO uses

Local variables:

Good practice – is to introduce and name entities close to where they are to e used – thus, to liit

the socope of the entity and its visibility

Local declarations leads to duplication of names and difficulties in subsequent maintenance of 

programs

Global variables:

It’s the only way in which the consistency and control of the naming of entities can be achieved

 – for large systems – being developed by a team of progrmmers

It’s sensible – to declare globally the names of all entities which directly realte to the outside

world – that is to the system being modeled or controlled – and to use local declaration for the

names of all internal entities – many of the difficulties, disappear, if the language permits explicit

control over the scope and visibility of entities and does not rely on default rules

Control of Visibility and Scope

Modern approaches to software design place  – a lot of emphasis on modularity and informationhiding – both contributing to security

For effective use of modularization in program: designing of the language must provide

mechanisms that enable the programmer to control explicitly the socope and visibility of all

entities

7/30/2019 Real Time Systems - 7th Sem - ECE - VTU - Unit 4 - Languages for Real Time Applications - Ramisuniverse

http://slidepdf.com/reader/full/real-time-systems-7th-sem-ece-vtu-unit-4-languages-for-real-time 15/35

It includes – the ability to extend the scope of entities declared within a program unit to areas

outside that unit

Solution: had from a large number of language designers

Program units – which provide the necessary facilities have been devised – known by differentnames

class – in SIMULA

module – in Modula – 2

segment – in ALGOL

package – in Ada

Modularity

In Modula -2:

main program unit is MODULE

local modules can be nested within the main module

Nesting of modules – is illustrated in program

Scope and visibility control in Modula – 2

For allowing the entitites – which are declared within the body of a module to be visible outside

the module they must be listed in an EXPORT list

Entities which are declared outside the module must be specifically imported into the module by

naming them in an IMPORT list

All entities – can be imported and exported – that is, variabeles, constants, types and procedures

7/30/2019 Real Time Systems - 7th Sem - ECE - VTU - Unit 4 - Languages for Real Time Applications - Ramisuniverse

http://slidepdf.com/reader/full/real-time-systems-7th-sem-ece-vtu-unit-4-languages-for-real-time 16/35

Entities – which are declared ina module are created at the initialization of the program and

remain in existence throughout the existence of the program  – that is they are not like entities

crated inside a procedure which cease to exist when the procedure body is left

More general concept of module – allows a program to be split into many separate units – each

of which can be compiled separately

Facilities for separation – also allow – for the consruction of program libraries in which the

library segments ae held in compliled form rathe than as source code – for this – module is

splitted into 2 parts:

1.  DEFINITION MODULE: this contains information aboutentities which are exported rom

the module

2.  IMPLEMENTATION MODULE: this is the body of the module which contains the code

which carries out the functions of the module

DEFINITION part: made available t the client program in source form

IMPLEMENTATION part: is only provided in object form and its source code remaings private

to the module designer

Separation provides an excellent method of hiding implementation details form a user and the

actual implementation can be changed without informing the user, providing that the

DEFINITION part does not change

Program Fragment: DEFINITION MODULE in Modula – 2

COMPILATION OF MODULAR PROGRAMS

Compiling the module to obtain executable the source code level to form a single object code:

there are 2 approaches

1.  Combine at the source code level to form a single unit which is then compiled

2.  Compile the individual modules separately and then in some way link the compiled

version of each module to form the executable program code – for this – a special piece

of software called a linker – has to be provided as part of the compilation support to do

the linking of the modules

7/30/2019 Real Time Systems - 7th Sem - ECE - VTU - Unit 4 - Languages for Real Time Applications - Ramisuniverse

http://slidepdf.com/reader/full/real-time-systems-7th-sem-ece-vtu-unit-4-languages-for-real-time 17/35

FORTRAN language: popular and widespread form of use in engineering and scientific works

Subroutines can be compiled independently – from the main program, and from each other

Ability to carry out compilation independently – arises from the single-level scope rules

Compiler makes the assumption that any entity which is referenced in a subroutine – but not

declared within that subroutine – will be declared externally – hence, it simply inserts the

necessary external linkage to enabe the linker to attach the appropriate code

Compilation must be independent – that is when a main program is compiled- the compiler has

no information available which will enable it to check that the reference to the subroutine is

cocrrect

Ex.: subroutine may expect – 3 real variables as parameters – but if the user supplies four iteger

variales in the call statement the error will not be detected by the compiler

Independent compilation of most block-structured languages: is even more difficult and prone to

errors in that arbitrary resrtirncitons on the use of variables have to be imposed

Most of the errors – can be detected – at the linkage stage – because, linking comes later in the

implemntatin process errors discovered at this stge are more costly to correct

Preferable – to design the language and compilation sytem in such a way as to be able to detect

as many errors as possible during compilation instead of when linking

Separate compilation unit idea – Modula – 2 and Ada – have introduced it

It implies that – the compiler is provided with some iforaton about the priviosly or separately

comiled untis shich are to be incorporated into a program

Modula -2:

Source code of the DEFINITION PART of a separately compiled module must be made

available to the user – and hence, the compiler – this enables the compiler to carry out the normal

type checking and procedure parameter matching checks

Type mismatches and procedure parameter errors are detectable by the compiler

Makes available the scope control for Modula – 2 language

DATA TYPES

7/30/2019 Real Time Systems - 7th Sem - ECE - VTU - Unit 4 - Languages for Real Time Applications - Ramisuniverse

http://slidepdf.com/reader/full/real-time-systems-7th-sem-ece-vtu-unit-4-languages-for-real-time 18/35

Allocation of types is closely associated with the declaration of entities

Allocation of type defines the set of values that can be take by an entitiy of that type and the set

of opertions that an be performed on the entity

Richness of types supported by a language and the degree of rigour with which typeecompatibility is enforced by the language are important influences on the security of programs

written in the language

Strongly typed: languages which rigouously enforce type compatibility

Ex.: Modula -2

Enforce the type checking on standard types

Modula-2 supports: enumerated types – allows programmers to define their own types in

addition to using the predefined types

Ex.: Simple motor speed control system

Has 4 settings: OFF, LOW, MEDIUM, HIGH, and are controlled by a computer system

Programmer in Modula – 2: can make the declarations

TYPE AMotorState = (OFF, LOW, MEDIUM, HIGH);

VAR motorSpeed : AMotorState;

Variable motorSpeed can be assigned – only one of the values – enumerated in the TYPEdefinition statement – attempts to assign any other values will be trapped by the compiler

Ex.: motorSpeed : = 150; will be flagged as an error

Wekly typed: languagees wich do not enforce etype compatibility

Ex.: FORTRAN and BASIC

Enforce some type checking

Statements A$=25 or a=X$+Y are not allowed in BASIC

Allow mixed integer and real arithmetic and provide implicit type changing in arithmetic

sstateents

7/30/2019 Real Time Systems - 7th Sem - ECE - VTU - Unit 4 - Languages for Real Time Applications - Ramisuniverse

http://slidepdf.com/reader/full/real-time-systems-7th-sem-ece-vtu-unit-4-languages-for-real-time 19/35

Both languages support only a limited number of types

ANSI FORTRAN of one ex.:

In this, integers – must be used to represent the 4 states of the motor contro:

INTEGER OFF, LOW, MEDIUM, HIGH

DATA OFF/0/, LOW/1/, MEDIUM/2/, HIGH/3/ 

MSPEED: a variable can have the values: MSPEED = 24 or MSPEED = 150 – are valid, and not

flagged as errors – either by the compiler or by the run-time system

MSPEED values – can be detected – by programmer-inserted check  – since, the output of a value

 – outside the range 0 to 3 – may have an unpredictable effect on the motor speed

Types of data types

1.  Sub-range Types

It enhances the security (i.e., here there is the ability to declare a sub-range of a type)

Modula – 2: sub-ranges of ordinal types (i.e., INTEGER, CHAR, and ENUMERATED types

can be defined)

Ex.: sub-range types – defined by statements

TYPE ADACValue = 0..25;

ALowerCaseChar = ‘a’..’z’; 

If the variables – are defined as :

VAR output : ADACValue;

Character : ALowerCaseChar;

Then, output := -25; character : = ‘A’; will be flagged as errors by the compiler

Compiler : insert run-time checks – on all assignment statements – involving sub-range types and

dany assignment which violates the permitted values will generate a run-time error

Use of sub-range types: increase the security of the program

In RTS, full use of sub-range types may not be appropriate  – they can be used, if the run-time

system permits the transfer of control to a user-supplied error analysis segment on detection of a

7/30/2019 Real Time Systems - 7th Sem - ECE - VTU - Unit 4 - Languages for Real Time Applications - Ramisuniverse

http://slidepdf.com/reader/full/real-time-systems-7th-sem-ece-vtu-unit-4-languages-for-real-time 20/35

run-time error – if, it doesnot, it terminates the execution of the program – then the security of 

the system can be jeopardized by their use

Sub-range types – useful during the developmental stages of the sysem – as violations of the

correctly set sub-ranges – can indicate logical errors in the code – to avoid this, compilers

provide an option switch to control the inclusion of sub-range checking

Ada:

Sub-range types: have been extended in Ada – to include sub-ranges of REAL

Usefulness is limited – because, of the extra code – introduced in order to check for violations

Applications – involving a large amount of computation – the use of sub-ranges of REAL – can

significantly slow down the computation – and so, in many applications – efficiency

requirements will necessitate the use of explicit range checks at appropriate points – rather than

the use of compiler-supplied checks through the use of sub-range types

2.  Derived Types

New types – in many languages can be created by – implicit types: known as derived types

Inherit all the characteristics of the parent type

Use of these, can make the meaning of the code clearer to the reader

Ex.:

TYPE AVoltage = REAL; AResistance = REAL; ACurrent = REAL;

VAR V1 = AVoltage; R3 = AResistance; I2 = ACurrent;

BEGIN I2 : = V1/R3;

END;

Above – code in user can easily see what is implied by the calculation –  Ohm’s law – is being

used to calculate the current flowing thorough a resistance

If I2 : = V1 * R3; I2: is declared as of type ACurrent – reader would be suspicious that there –  

was an error in the line

In Ada:

7/30/2019 Real Time Systems - 7th Sem - ECE - VTU - Unit 4 - Languages for Real Time Applications - Ramisuniverse

http://slidepdf.com/reader/full/real-time-systems-7th-sem-ece-vtu-unit-4-languages-for-real-time 21/35

Neither of the above statements: would be accepted by the compiler – for although, erived types

inherit the properties of th pparent type – they re treated as distinct types – and hence, are not

compatible

Advantages: compiler can detect errors such as assigning V1, a variable representing voltage to

the variable I2, which represents a current (as shown by the first statement in program below) –  second statement below – will flag as an eroor

I2 : = V1;

I2 : = V1/R3;

Above problem: can be overcome by – operator overloading – operators here, can be redefined to

be compatible wit a different set of types

Ex.: / operator: can be defined to perform division: (AVoltage/AResistance) = ACurrent

Benefits of the strong typing – whether overweigh – the disadvantage of the complexity of 

overloading – is arguable

3.  Structured Types

Array: only one structured type – provided by many programming languages

Arrary: are powerful, contain elements of the same type

RECORD : it’s the structured type – in which, variables of different types – are present

Programmer does not continually, have to consider – the details of the way in which the

information relevant – to the variable is stored – i.e., abstraction for

Program Fragment:

Figure: Standard structured program constructs

FILE and SET : other 2 types of structured data types

4.  Pointers

Provide the mechanism – for indirect reference to variables

Widely used in systems programming and in some data processing applications

Can be used: to create linked lists and tree structures

7/30/2019 Real Time Systems - 7th Sem - ECE - VTU - Unit 4 - Languages for Real Time Applications - Ramisuniverse

http://slidepdf.com/reader/full/real-time-systems-7th-sem-ece-vtu-unit-4-languages-for-real-time 22/35

Unrestricted availability of pointers:

Ex.: permitting pointers to entities of different types to be interchanged – can give rise to

insecurity

Pointers, use in implicit: dynamic allocation and consequent deallocation of storage – theoverheads involved in he required mechanisms can be considerable

In RTS, the care in the use of the pointers – is necessary

CONTROL STRUCTUES

GOTO:

Use of GOTO: conditional GOTO and unconditional GOTO – in high-level languages – are

with extensisve arguments

Disadvantes:

Use will make program difficult to read

It can be shown that: any program can be expressed without the use of GOTOs – since,

langauges support –  WHILE statement, IF…THEN…ELSE conditional, and BOOLEAN

variables – by using them, while in of GOTOs places

Theoretical point of view: avoidance of GOTOs is attractive

Advantages:

Judicious use – of GOTOs – can avoid complicated and confusing coding

Ex.: when it is required – to exit from a loop – in order to avoid further processing when a

particular condition occurs

PROGRAM FRAGMET

EXIT:

Makes control passing to the statement immediately following the end of the loop

Lead to clearer and simpler coding

RETURN:

7/30/2019 Real Time Systems - 7th Sem - ECE - VTU - Unit 4 - Languages for Real Time Applications - Ramisuniverse

http://slidepdf.com/reader/full/real-time-systems-7th-sem-ece-vtu-unit-4-languages-for-real-time 23/35

Going to use – PROCEDURES in

Results in immediate – exit from the PROCEDURE

Lead to clearer and simpler coding

PROGRAM FRAGMENT

EXCEPTION HANDLING

Most difficult – area of programming and design – is handling of errors, unexpected events, and

exceptions – which make the processing of data by the subsequent segments superfluous, orpossibly dangerous

Designer has to make decisions on: such questions as –  

What errors are to be detected?

What sort of mechanisms is to be used to do the detection? And

What should be done when an error is detected?

Automatic error detection mechanisms:

as part of run-time support systems – in languages supported

trap errors such as tempt to divide by zero, arithmetic overflow, array bound violations, and sub-

range violations – can also include traps for input/output errors

for checks – compiler can add cod o the program – checks: increase the size of the code, and

reduce the speed at which it executes

when a program detected the error: halting and displaying an error message - is the normal

response – in RTS, halting the program is not acceptable – as it may compromise the safety of 

the system – every attempt must be made to keep the system running

checking for all data errors – can become very complex, can obscure the general flow of code,

and can slow down execution

7/30/2019 Real Time Systems - 7th Sem - ECE - VTU - Unit 4 - Languages for Real Time Applications - Ramisuniverse

http://slidepdf.com/reader/full/real-time-systems-7th-sem-ece-vtu-unit-4-languages-for-real-time 24/35

In RTS: better way of error handling: language to be designed to allow – the appicatio software

to deal with the errors – when they are detected – and to pass the error on to the built-in error

handling mechanism – only, if the application software, fails to deal with the error

Language run-time support software:

1.  detect as many types of errors as possible

2.  inform the application software – that an error exists

3.  contain error handling routines that will deal wih thee error – if the application software

does not do so

BASIC : one of the first language – to support the error handling – by using

ON ERROR GOTO statement

ON ERROR GOSUB statement

Above statements:

enables error trap routines to be inserted in the application program

can simplify – the flow of the code as it permits grouping of the error handling and analysis into

one place

Run-time error can be checked – and action taken to keep the system running – if it cannot be

kept running, at least – there may be an opportunity to close it down safely and warn the operator

Advantages:

designer can consider – the normal function and the error actions as two separate problems

execution speed is improved

in more complex cases – the readability of the code is improved

Disadvanteges:

Encourages reliance on the run-time system – to catch errors rather than careful consideration of 

the possible errors

Use of error trapping/exception statements requires – careful consideration of the action – which

follows – the execution of the error handling routine

Execution – will resume to the next statement – after the statement – which generated the error

7/30/2019 Real Time Systems - 7th Sem - ECE - VTU - Unit 4 - Languages for Real Time Applications - Ramisuniverse

http://slidepdf.com/reader/full/real-time-systems-7th-sem-ece-vtu-unit-4-languages-for-real-time 25/35

If the return is made from the error-correcting routine to the line that caused the error to be raised

- is made to the statement – that caused the error – if the correction is incorrect – the same error

will be raised again – software will cycle endlessly round the same loop

There can be abnormal or error conditions – which the underlying language support system –  

will not detect – can be – at that time – alternative – error-trapping things have to kept

Further, error analysis, and error handling: from designer – will be benefitted

It’s possible to write – error handling mechanisms – explicitly

Procedures: if nested, problem will be in exception handling – and a report of error – has to be

passed from one procedure to another

CUTLASS:

Developed for RT use

Offers an interesting approach – to error handing

Marks data – as bad - by using one bit in the storage used – for data values – as a tag bit

When an error is detected – data values marked as bad –  then allowed to propagate normally

though the system

Any action – to deal with the bad data – is then taken – at the point at which it is used

Rules used in the propagation of the bad data produce: a result which is marked as bad  – if any

of the operands in an arithmetic operation are bad 

LOW-LEVEL FACILITIES

In RTS: frequently, it need to manipulate directly data in specific registers in the computer

system – ex.: in memory registers, CPU registers, and registers in an I/O device

In older high-level languages: assembly languages are used to do this

Some languages – provide extensions to avoid the use of assembly routines  – and these, typically

found in BASIC

Form will be as follows:

1.  PEEK(address) – returns as INTEGER variable contents of the location address

2.  POKE(address, value) – puts the INTEGER values I the location address

7/30/2019 Real Time Systems - 7th Sem - ECE - VTU - Unit 4 - Languages for Real Time Applications - Ramisuniverse

http://slidepdf.com/reader/full/real-time-systems-7th-sem-ece-vtu-unit-4-languages-for-real-time 26/35

 

Integer values in 8-bit computers: values in the range of 0 to 255

Integer values in 16-bit computers: values in the range of 0 to 65 535

Computer systems in which the I/O systems: are not memory-mapped (Ex.: Z80) – additional

functions are usually provided such as:

1.  INP(address) and

2.  OUT(address, value)

BBC BASIC:

Uses an indirection operator – which indicates that the variable which follows – it is to be treated

as a pointer – which, contains the address of the operand – rather than the operand itself 

(Term indirection: derived from the indirection addressing mode – in assembly languages)

Indirection operator: ?

The following code:

100 DACAddress = &FE60

120 ?DACAddreess = &34 results in hex. 34 – loaded into locatiton FE60H

FORTRAN(in processes),CORAL, and RTL/2: additional features – which allow the

manipulation of the bits in an integer variable are provided

Ex.: SET BIT J(I),

IF BIT J(I) n1, n2 (where I refers to the bit in variable J).

Operations such as AND, OR, SLA, SRA –  mimic the operations available at assembly level

Weakness: all type checking is lost – and it is very easy to make mistakes

Safer method:

Allow the programmer –  

1.  to declare the address of the register or memory location

2.  to be able to associate a type with the declaration

7/30/2019 Real Time Systems - 7th Sem - ECE - VTU - Unit 4 - Languages for Real Time Applications - Ramisuniverse

http://slidepdf.com/reader/full/real-time-systems-7th-sem-ece-vtu-unit-4-languages-for-real-time 27/35

Ex.: VAR charout AT 0FE60H : CHAR; declares a variable of type CHAR located at memory

location 0FE60H

Characters can be written – to this location by simple assignment –  charout : = ‘a’; 

If charout : = 28; compiler would detect and flag – as an error

Both Modula-2 and Ada – permit the use of declarations of the above type

Modula – 2

Provides – low-level support mechanism through – a simple set of primitives – which have to be

encapsulated I a small nucleus coded in the assembly language of the computer on which the

system is to run

Access to primitives: is through – a module SYSTEM – which is known to the compiler

SYSTEM: can be thought of as the software bus linking the nucleus  – to the rest of the softwaremodules

SYSTEM – makes available 3 data types: WORD, ADDRESS, PROCESS

SYSTEM – makes available 6 PROCEDURES: ADR, SIZE, TSIZE, NEWPROCESS,

TRANSFER, IOTRANSFER

WORD:

Data type which specifies – a variable which maps onto one unit of the specific computer storage

Number of bits in WORD: vary from implementation to implementation

Ex.: on a PDP-11 implementation: a WORD is 16 bits

On a 68000 implementation: a WORD is 32 bits

ADDRESS:

Corresponds to the definition: TYPE ADDRESS = POINTERS TO WORD i.e., objects of the

type ADDRESS are pointers to memory units – and can be used to compute the addrsses of the

memory words

PROCESS:

Have associated with them storage for the volatile environment of the particular computer – on

which Modula – 2 is implemented

Make it possible – to create easily process(task) descriptors

7/30/2019 Real Time Systems - 7th Sem - ECE - VTU - Unit 4 - Languages for Real Time Applications - Ramisuniverse

http://slidepdf.com/reader/full/real-time-systems-7th-sem-ece-vtu-unit-4-languages-for-real-time 28/35

3 of the PROCEDURES provided – by SYSTEM – for address manipulation:

1.  ADR(v): returns the ADDRESS of variables

2.  SIZE(v): returns the SIZE of variable v in WORDs

3.  TSIZE(v): returns the SIZE of any variable of type t in WORDs

Variables – can be mapped onto the specific memory locations – this can be used to write the

device drivers – in Modula – 2

Combination of low-level access facilities and the module concept allows details of the hardware

device to be hidden within a module with only the procedures for accessing the module being are

available to the end user

COROUTINES

Modula – 2: 

Basic form of providing concurrency: is by coroutines

Two procedures – exported from SYSTEM – are defined as:

1.  NEWPROCESS:

Associates with the procedure storage parameters, and some storage to act as workspace for the

process

It’s the programmer’s responsibility – to allow the sufficient workspace

Amount of workspace to be allocated – depends on number and size of the variables – local to

the procedure forming the coroutine, and to the procedures which it calls

Failure to allocate – sufficient space will usually result in a stack overflow error at run-time

PROCEDURE NEWPROCESS( ParameterlessProcedue : PROC;

workspaceAddress : ADDRESS;

workspaceSize : CARDINAL;

VAR coroutine : ADDRESS (* PROCESS *) );

2. Variable coroutine is initialized to the address – which identifies the newly created coroutine,

and is used as a parameter in calls to TRANSFER

7/30/2019 Real Time Systems - 7th Sem - ECE - VTU - Unit 4 - Languages for Real Time Applications - Ramisuniverse

http://slidepdf.com/reader/full/real-time-systems-7th-sem-ece-vtu-unit-4-languages-for-real-time 29/35

Transfer of control – between coroutines is made using a standard procedure TRANSFER –  

which has two arguments – of type ADDRESS (PROCESS) – first, is the calling coroutine and

the second is the coroutine to which control is to be transferred

Modula -2 in, it cannot be used for RTS  – for concurrent programming implementation, from

coroutines – from as few commentators – but, it can be possible to use the primitive coroutineoperatitons – to create RT executive

PROCEDURE TRANSFER( VAR source, destination : ADDRESS (* PROCESS *) );

PROCESS: any parameterless procedure

INTERRUPTS AND DEVICE HANDLING

Language must support the interrupt handling: if full I/O device support is to be provided

It needs – support for minimum form of concurrent operation – since, an interrupt causes – the

suspension of the running program (task) and the execution of some other code

Two basic approaches in languages to do this:

1.  To provide a set of low-level primitive operations which can be sed either directly or to

build higher-level constructsEx.: Modula - 2

2.  To provide a high-level set of primitive operations which must be used

Ex.: Ada

Hardware interrupts: can be handled from within a Modula – 2

Device handling process: can enable – the external device and, then suspend itself  – by a call to a

procedure IOTRANSFER

This procedure – is similar to TRANSFER – but, with one additional parameter – which, allows

the hardware interrupt – belonging – to the device to be identified

When an interrupt occurs – control is passed back to the device routine by a return from

IOTRANSFER

7/30/2019 Real Time Systems - 7th Sem - ECE - VTU - Unit 4 - Languages for Real Time Applications - Ramisuniverse

http://slidepdf.com/reader/full/real-time-systems-7th-sem-ece-vtu-unit-4-languages-for-real-time 30/35

IOTRANSFER format:

IOTRANSFER ( VAR interruptHandler : PROCESS;

interruptedProcess: PROCESS;

interruptVector: CARDINAL )

Action:

1.  To save the current status of interruptHandler

2.  To resume execution of interruptedProess – i.e., to wait for an interrupt

When an interrupt occurs: the equivalent of 

TRANSFER ( interruptedProcess, interruptHandler ) occurs

Skeleton interrupt handler - will be of the form:

BEGIN

LOOP

… 

IOTRANSFER ( interruptHandler, interruptedProcess, interruptVector);

( * interrupt handler waits at this point for interrupt * )

… 

END LOOP

END;

Interrupt handler code:

Placed inside the LOOP…END construct 

Initiated by an explicit TRANSFER operation

It then, waits – for an interrupt at the IOTRANSFER statement

When, first time – it executes – control is returned to the initiating task 

Subsequent executions – will be after an interrupt – has occurred

And then, a return will be made to whichever task was interrupted

7/30/2019 Real Time Systems - 7th Sem - ECE - VTU - Unit 4 - Languages for Real Time Applications - Ramisuniverse

http://slidepdf.com/reader/full/real-time-systems-7th-sem-ece-vtu-unit-4-languages-for-real-time 31/35

CONCURRENCY

Wirth (1982): defined the standard – module Processes – which provides – a higher-level

mechanism – than coroutines for concurrent programming

Coroutines vs Concurrency

In concurrency: no assumption as to how the processes (tasks) – will be implemented – in

particular, it does not assume that – processes will be implemented on a single processor

If above are present: concurrency will – be coroutine

RUN-TIME SUPPORT

Run-time support mechanisms – provided by the particular implementation of the languages: is

the – important contributor to efficiency of implementation of a language and dto the run-timesecurity

Problem of run-time support mechanism – security and efficiency – frequently come into conflict

Solution: providing optional run-time error checking and trapping mechanisms

For tests: error traps are switched in – and, when the software – has been tested – the error traps

are switched out – and, the software runs faster

Ex.: checking of array bounds

Many compilers: allow the insertion of check code – as an option – which, is selected when the

program is compiled

Acceptable approach for standard programming:

During the initial testing – array bound check is selected, but when the program appears to

function correctly – it is omitted, and hence the program runs faster – this mode of facility usage

is often used for RTS, but it is not a reliable solution

2 ways to do array checking: Let an array of 20 numbers – denoted by arrary[i], where i – is an

integer variable – for security, array[i] – must be used – in the range 1<=i<=20

1.  Insert the check  – before every array access – to ensure that the – condition is not

violated

2.  Used by more modern compilers – check the condition, whenever an assignment is made

to i – require the cooperation of the programmer – who must specify the permitted range

of variable i – when it is declared

7/30/2019 Real Time Systems - 7th Sem - ECE - VTU - Unit 4 - Languages for Real Time Applications - Ramisuniverse

http://slidepdf.com/reader/full/real-time-systems-7th-sem-ece-vtu-unit-4-languages-for-real-time 32/35

Provided that the – checking of the assignment of values to i is done then there is no need

to check access to array[i]

A large number of assignments to i – can be tested during compilation

It relies – on language being strongly typed

For the secure RTS – this method is preferable – as it allows the checking to remain in

the final version of the software without imposing large – run-time overheads on the

execution time

For RTS – other important feature – of run-time software – is whether it allows the

application softwares – to intercept the error traps

OVERVIEW OF REA-TIME LANGUAGES

Which is the best language to use for writing software? – is one complex decisison

Since 1970s: high-level languages for the programming and construction of RTs have

become widely available

Early languages:

CORAL (Woodward et al., 1970) and RTL/2 (Barnes, 1976)

Modifications to the FORTRAN and BASIC

Interest in concurrency and multi-processing in languages – from they become –  

languages with the potential for use with RTS

Ex.:

Ada (Young, 1982; Burns and Wellings, 1990)

ARGUS (Liskov and Scheifler, 1983)CONIC (Kramer et al., 1983)

CSP (Hoare, 1978)

CUTLASS (CEGB, Bennet and Linkens, 1984)

FORTH (Brodie, 1986)

Modula -2 (Wurth, 1982)

Occam (Burns, 1988)

PEARL and SR (Andrews, 1981, 1982)

Most widely – used programming language in embedded programming – C

Languages – ranging from

1.  Comprehensive, general purpose languages such as Ada and Modula -2

Advantages: provide flexibility – that can support the building of virtual machines

Ex.: Budgen (1985) – shown how Modula – 2 can be used to create a MASCOT

virtual machine

7/30/2019 Real Time Systems - 7th Sem - ECE - VTU - Unit 4 - Languages for Real Time Applications - Ramisuniverse

http://slidepdf.com/reader/full/real-time-systems-7th-sem-ece-vtu-unit-4-languages-for-real-time 33/35

Disadvantages: can be too complex and permit operations – that can compromise the

security of the RTS

2.  More restrictive, special purpose languages such as CONIC and CUTLASS

Language suitable for programming RT distributed system: must have all the characteristic of the

good, modern, non-RT languages – that is, it should have

1.  Clean syntax

2.  Rational procedure for declarations

3.  Initialization and typing of variables

4.  Simple and consistent control structures

5.  Clear scope and visibility rules and

6.  Should provide support for modular construction

Additions required for RT use are

7.  Support for concurrency or multi-tasking

8.  Mechanism to access to the basic computer functions (low-level construts)

Languages design:

Modula -2:

Based on the small set of mandatory facilities – which can be extended using library modules

Core is standardized – even – there are many different sets of libraries supplied – by different

compiler implementers

Ex.: I/O routines are not provided – as part of the languages – but, have to be supplied as the

library module and consequently – the range and type of routines differ according to the supplier

Advantages: languages is kept simple – user can choose to add the features necessary, and

appropriate to the type of application

Disadvantages: loss of standardization and hence, portability

Ada:

All facilities – considered necessary are mandatory and extensions are prohibited

7/30/2019 Real Time Systems - 7th Sem - ECE - VTU - Unit 4 - Languages for Real Time Applications - Ramisuniverse

http://slidepdf.com/reader/full/real-time-systems-7th-sem-ece-vtu-unit-4-languages-for-real-time 34/35

Advantages: it is standardized, and hence – application software should be highly portable

Disadvantages: size and complexity

7/30/2019 Real Time Systems - 7th Sem - ECE - VTU - Unit 4 - Languages for Real Time Applications - Ramisuniverse

http://slidepdf.com/reader/full/real-time-systems-7th-sem-ece-vtu-unit-4-languages-for-real-time 35/35