Programming Languages for Embedded Systems
Download
Report
Transcript Programming Languages for Embedded Systems
Programming Languages
for
Embedded Systems
Embedded Systems
•
•
•
•
•
Communication Terminals
Communication Infra Structure
Control Systems
Surveillance Systems
Alarm Systems
Embedded Systems
•
•
•
•
•
Embedded into real physical world.
Often dependable (high risk).
Concurrency natural.
Real time demands.
Performance demands.
Real Time Programming
Languages
•
•
•
•
•
•
•
•
RT-PASCAL
Conc. PASCAL
Ada
Modula (2)
OCCAM
C / C++
Erlang
RT -JAVA
Language Support
•
•
•
•
•
•
Low level programming
High level programming
Concurrency
Synchronization (IPC)
Exception handling
Real time features
Low level issues
• Physical memory directly adressable
• Registers accessible
• (Direct) interrupt control
Low Level Programming
Memory Access
• C: linker directives and register variables
(not really C) char xdata RAM[SIZE];
• Ada:
for var’Adress use
System.Storage_Elements.ToAdress(offset)
• RT-JAVA:
RawMemory
Access( long base, long size) setByte(long
offset), getByte(long offset)
Low Level Programming
Interrupt Control
• C: void interrupt[nr] Handler(void){..}
• Ada:
pragma Attach_Handler(Handler,intr_nr);
• RT-JAVA:
publ.
Class AsyncEvent{},
addHandler(AsyncEventhandler, Handler),
bindTo(java.lang.String happening)
High level issues
• High level data structures (rec., list, queues,
trees)
• High level program structures (conditionals,
iterations, abstraction)
• Modularity (modules, components)
Concurrency Models
• Uniprocessor multiplexing
• Multiprocessor multiplexing (shared
memory)
• Distributed systems (networks)
Concurrency Models
•
•
•
•
•
Co-routines
Cobegin -end
Fork and join
Threads
Explicit processes
Co-routines
(domestic non preemptive
kernels)
Op. Sys
A
B
C
Co-routines
(Modula 2)
A
B
C
Cobegin/coend (par)
(OCCAM)
cobegin
st1;
st2;
st3;
.
.
.
stN;
coend
PAR
st1;
.
.
stN;
SEQ
st1;
.
.
stN;
PAR i=1 FOR N
proc(i);
Threads
(JAVA, POSIX)
Light weigth processes sharing data memory.
Handled by language run time system.
(JAVA:)
java.lang.Thread (implements java.lang.Runnable - has run method)
class MyThread extends Thread {
//constructor
public MyThread ( ) { //initialization}
public void run ( ) //executed upon calling inherited start-method
{// Thread statements}
Threads
(JAVA:)
public class ThreadExample {
public static void main ( ) {
MyThread thread1,thread2;
thread1 = new MyThread( );
thread2 = new MyThread ( );
thread1.start( );
thread2.start( );
}
}
Fork and Join
(POSIX,Erlang,Mesa )
(Erlang: in Pid1)
Pid2 = spawn(Module, Function, Arguments)
(Mesa: )
(POSIX:)
function F return …;
child_id = fork(proces_id);
.
.
C = wait(child_id);
procedure P;
… C:= fork F; (concur. Exec.)
… J:= join C; (synchronization)
end P;
Explicit process Declaration
(Ada: )
procedure OurProc is
task A;
task B;
-- declare A
-- declare B
-- startup point for A and B
-- statements for OurProc
-- declare A
task body A is
- - declarations for A
begin
- - statements for A
end
-- declare B
task body B is
- - declarations for B
begin
- - statements for B
end
Synchronization Models
•
•
•
•
Shared variables
Message based communication
Remote Procedure Calls
Asynchonous Control Transfer
Shared Memory
• Need for mutual exclusion.
• Either busy waiting or atomic primitives with
suspension (wait).
• Semaphores, conditional regions, monitors
or protected objects
Shared Memory
Conditional Critical Regions
( Edison:)
st;
----region <guard> do
st1;
--stN;
end region
st;
---
• guard is logical expression, i.e. N>3
• Suspended process must be awakened
to evaluate guard whenever CCR is left.
• Evaluation order.
• Can be spread all over the code.
Shared Memory
Monitors
(Modula 1: )
INTERFACE MODULE MyMon;
var Con: SIGNAL;
procedure A( )
begin
wait (Con);
----end
Procedures execute mutually exclusive
procedure B( )
begin
----send (Con);
end
Shared Memory
Protected objects
(Ada: )
protected body ProObj is
entry Out (OutVar: out Integer)
entry In (InVar: in Integer)
when <cond2> is
when <cond1> is
begin
begin
---
---
end Out
end In
Concurrent reads (in) - mutually exclusive writes (out)
process suspended until condition evaluates true
Shared Memory
Protected objects
(java: )
public synchronized void A ( ) public synchronized void B ( )
{
{
while (! condA)
while (! condB)
{
{
try{ wait( );}
try{ wait( );}
catch (. . .) {...}
catch (. . .) {...}
----notify ( ); // or notifyAll( );
notify ( ); // or notifyAll( );
}
}
}
}
Mutual exclusive execution of object methods
notify and notifyAll moves waiting process(es) to ready queue !
Message based
Communication
• Asynchronous: No blocking while waiting for reply (queueing)
- not language supported because of program complexity but
- can be implemented using synchronous message passing.
• Synchronous: Blocking while waiting for reply
Message based
Communication
(OCCAM2: )
ERLANG:
CHAN OF INT ch;
Pid2 = spawn(echo, loop, []),
ch ! X
ch ? X
Pid2 ! {self(), hello},
(in Pid2)
receive
{From, Msg} ->
//some action ,
end.
Message based Communication
Remote Invocation
(Ada (Rendezvous): )
(in calling task:)
task MyTask ( ) is
-entry Echo (Inp: in Integer; Outp: out Integer)
-end MyTask
MyTask.Call (2,X);
task body MyTask is
-begin
accept Echo (Inp: in Integer; Outp: out Integer) do
-Outp=Inp;
end Call
Exception handling
taxonomy of exceptions
• Synchronous: Exception timely linked to point in
control flow. (Divide by zero)
• Asynchronous: Exception related to other than
current point in control flow. (Interrupt)
• Internal: Unexpected result of computations (SW
error).
• External: Unexpected behaviour of environment.
(Broken network)
Exception handling
issues in exception handling
• Intermingled (older) : “if (! Myfunction ( ) ) exit (0) “
• Structured (modern) : control flow implicitely defined
• Domain: Region of program handled by
exceptionhandler.
• Propagation: Passing on exceptions on to callee.
Exception handling
domain:
(Ada: ) (blockstructured)
declare
-begin
-exception
end
(C++,JAVA: )
try { //statements within
scope }
catch { //exception spec.}
(CHILL: )
exceptions associated to all
statements
Exception handling
Propagation:
• Resumption: Execption handler allows procedure to resume action
when exception has been handled.
•Termination: Exeception handler only handles what is needed when
procedure cannot execute.
•Hybrid: Procedure is retried when exception has been handled.
Real Time Issues
•
•
•
•
•
Access to a physical (independent) clock
Clock granularity
Delay
Timeout
Scheduling: Periods, Comp. Times,
Deadlines e.t.c.
Real Time Issues
Access to a physical (independent) clock
(OCCAM2: )
(Ada: )
TIMER clock;
INT Time;
with Ada.Real_Time;
clock ? Time;
//Relative time is read
into “Time”
time : Time;
begin
time := Clock;
end
Real Time Issues
Access to a physical (independent) clock
(JAVA:)
(RT-JAVA:)
import java.util.Date;
import java.util.Date;
Date d;
d= new Date;
d.toString ( );
//returns time in 64
bits mSec.
public class {AbsoluteTime | RelativeTime |
RationalTime} extendsHighResolutionTime
AbsoluteTime time;
--time.getMilliseconds( ); //64 bits
time.getNanoseconds( ); //32 bits
Real Time Issues
Delay:
(Ada: )
(OCCAM2: )
delay 10.0; //sleeps 10
sec from now (relative)
TIMER clock;
SEQ
clock ? time;
delay until 100.0; //
sleeps until absolute time
reaches 100 sec.
(absolute)
Only lower bound
guarantteed
clock ? AFTER time PLUS (<offset<)
Real Time Issues
Delay:
(JAVA: )
(RT-JAVA: )
int time;
HighResolutionTime time;
//inside thread
//inside realtime thread
try { sleep (time) }
try { sleep (time) }
catch (<exception>)
catch (<exception>)
{ //handling action}
{ //handling action}
Relative delay
Relative or absolute delay in
nanosecs. granularity
Real Time Issues
Timeout (wait for messages):
(Ada: )
(OCCAM2: )
select
SEQ
accept Call( .. ) do
ALT
-- action
call ? Param
end Call
-- action for call
or
delay(until) <time>
end select
clock ? AFTER (<time>)
-- action for timeout
Real Time Issues
Timeout (wait for completion):
(Ada: )
( RT-JAVA:)
select
public abstract class Timer
delay(until) <time>;
then abort
-- action
(HighRes.Time t, Clock c,
AsyncEventHandler Handler);
AsyncEventHandler is a runnable
object
//in Handler.run ( )
MyThread.stop ( ); el.
MyThread.interrupt ( );
Real Time Issues
Task Scheduling:
(Ada: )
(OCCAM2: )
//Fixed priority assignment
comes with Real-Time
Systems Annex.
//Fixed priority assignment
included in language
PRI PAR
P1
task MyRTTask is
entry ..
P2
PAR
P3
pragma Priority(<prio>)
end MyRTTask
P4
P5
//order: P1,P2,(P3,P4),P5
Real Time Issues
Task Scheduling:
(Ada: )
//Immediate Ceiling Priority (ICPP)
//Ceiling: C(S) = max {Prio(T) | T locks S}
//When t locks S it executes under max {prio(t),C(S)}
pragma Locking_Policy(Ceiling_Locking)
Real Time Issues
Task Scheduling:
(JAVA: ) // Some prioritized scheduling policy - semantics
rather loose.
class MyThread extends Thread ..
public MyThread ( < prio > ) //10 distinct priority levels
{
setPriority (<prio>);
--}
Real Time Issues
Task Scheduling:
(RT - JAVA: ) //Supports at least 28 priorities in fixed prio.
preemptive scheduling.
class MyRTThread extends RealTimeThread ..
SchedulingParameters Scheduling;
ReleaseParameters Release;
MyRTT = new MyRTThread (Scheduling, Release);
Real Time Issues
Task Scheduling:
(RT - JAVA: )
//cost,deadline
deadline,overrunHandler,
missHandler
Release
Parameters
Scheduling
parameters
Priority
Parameters
1 - 28
Importance
Parameters
If missed
deadlines
Periodic
Parameters
//start, cost,
period,deadline
Aperiodic
Parameters
Sporadic
Parameters
//minInterarrival