Download presentation
Presentation is loading. Please wait.
1
§ Real-time programming
IA § Real-time programming Problem definition Real-time programming methods Tasks Synchronization of tasks Communication between tasks
2
What is real-time programming? Non-real-time computing
IA What is real-time programming? Non-real-time computing – Correctness of the result Real-time computing Correctness of the result Timeliness of the result Not too soon, not too late
3
NON-REAL-TIME Computing:
IA NON-REAL-TIME Computing: Input data Output data Data Processing REAL-TIME Computing: Time Time Time Input data Output data Data Processing
4
Real-Time Programming
Creation of programs in such a way that the time requirements on the compilation of input data, on the processing and on the delivery of output data are fulfilled. time requirements Time requirements are dependent on the processes in the technical system coordination with the technical process Real-time: according to the real-time-sequences, no time expansion, no time compression Different kinds of requirements on the time behavior of the data processing Requirements for timeliness Requirements for concurrency –
5
Differences between information and real-time systems
IA Differences between information and real-time systems Information systems Real-time systems data-driven system event/time driven system complex data structure simple data structure great amount of input data small amount of input data I/O-intensive computation-intensive machine-independent machine-dependent
6
Hard real-time systems Strict deadlines must not be missed in any case
IA Important terms Hard real-time systems Strict deadlines must not be missed in any case Examples: DDC- control in airplanes, motor control in automobiles Soft real-time system A violation of deadlines can be tolerated Utility Deadline hard real-time system soft real-time system Time
7
Requirements for timeliness
IA Requirements for timeliness timely compilation of input data timely data processing – – timely delivery of output data Time requirements regarding timeliness Absolute time requirements e.g..: 11:45 signal for departure Relative time requirements Turn off signal 10 seconds after a measured value exceeds its threshold e.g..:
8
Requirements for concurrency
IA Requirements for concurrency Processes in the “real world” take place simultaneously -+ Real-time systems have to react to that simultaneously -+ Several computation tasks have to be executed simultaneously Examples: Reaction to simultaneous trips of several trains Processing of several simultaneously occurring measurements in heating systems Control motor and ABS system simultaneously
9
Realization of concurrency
IA Realization of concurrency – Each computation task is processed on a separate computer real parallelism – One computer for all data processing tasks quasi parallel Prerequisite: Processes in the environment are slow in comparison to the computation of the programs on the computer
10
Requirements for determinism
IA Requirements for determinism Determinism = predictability of the system behavior Parallel and quasi-parallel sequences are generally not predictable; time shifts can lead to different execution sequences. – the time behavior is not deterministic – no guaranty of the safety of automation systems
11
Deterministic real-time system
IA Deterministic real-time system A real-time system is called deterministic if for each possible state and for each set of input information there is a defined set of output information and a defined following state. Prerequisite: a finite set of system states! Timely deterministic system The response times for all output information are known In a deterministic system it is guaranteed that the system can react at every time. In a timely deterministic system it is additionally guaranteed at which time the reaction will have taken place.
12
§ Real-time programming
IA § Real-time programming Problem definition Real-time programming methods Tasks Synchronization of tasks Communication between tasks
13
Synchronous programming:
IA Different methods – Synchronous programming: Planning of the time sequence in advance of the execution of programs planned economy Asynchronous programming (parallel programming): Organization of the time sequence during the execution of the programs. market economy –
14
Organisator Dentist's assistant
IA e.g.: Dentist's office as real-time system (live writing) Patient Automation Program Execution place Processor, that executes the programs Dentist and dentist’s chair Organisator Dentist's assistant Operating system Planning of the timely sequence before the treatment with a appointment calendar Pre- planning Synchronous programming Planning of the timely sequence during the treatment (calls from the waiting room) Live- planning Asynchronous, parallel programming
15
Synchronous programming method Synchronous programming:
IA Synchronous programming method Synchronous programming: Planning of the time behavior of subprograms that have to be executed cyclically, before their actual execution – Synchronization of the cyclical subprograms with a time grid Time grid generated by a real-time clock, interrupting signal for the call of subprograms Time triggered – Strictly predefined sequence of the execution of subprograms –
16
Characteristics of the synchronous programming method (1)
– Requirement for timeliness is approximately fulfilled slight shifts Requirements for concurrency is fulfilled, if cycle time T is small compared to the time constants in the technical process – Synchronous programming is good for real-time systems with cyclic program execution – predictable behavior Synchronous programming is not suitable for the reaction on timely non-predictable (asynchronous) events – Increase of computation time due to constant polling Delay of the reaction •
17
Characteristics of the synchronous programming method (2)
Normally deterministic behavior No complex organization program – A bit more expensive in its planning (development) Disadvantage of synchronous programming: Modification of the requirements specification causes modification of the complete program structure! e.g.: PLC-programming
18
Asynchronous programming method (parallel programming)
IA Organization program (real-time operating system) controls the timely execution of subprograms at run-time Execution of subprograms, when time requirements are fulfilled Simultaneous execution is sequentialized according to a certain strategy – • Assignment of priority numbers The priority is higher, the smaller the priority number is
19
Characteristics of the asynchronous programming method
IA Characteristics of the asynchronous programming method – Requirements for timeliness only approximately fulfilled Bad for low priority subprograms Time requirements are fulfilled the better the higher the priority of the corresponding subprogram – – Actual time sequence can shift away from the desired time sequence Subprograms can over take each other mutually The sequence of the subprograms is not deterministic, but occurs dynamically – At the program development it cannot be indicated in advance which of the subprograms will run at which point of time – simple development complexity in the administration program program sequence is not transparent
20
Event-driven architectures Asynchronous programming
IA Event-driven architectures Asynchronous programming – all activities as sequences of events • activation of tasks transmission of messages – support through real-time operating systems non-deterministic behavior flexible regarding modifications Time-driven architectures Synchronous programming periodical execution of all tasks and communication actions sampling of external variable at determined times low flexibility in case of modifications easy to analyze –
21
§ Real-time programming
IA § Real-time programming Problem definition Real-time programming methods Tasks Synchronization of tasks Communication between tasks
22
Introduction of the term computation task
IA Task A task is the procedure of execution of a sequential program controlled by a real time operating system Introduction of the term computation task – Task starts with entry in a list of the real-time operating system and ends with the deletion from that list – Task does not exist only during the execution of the commands, but also during planned and forced waiting times
23
Differences between task and thread Task Thread
IA Differences between task and thread Task Thread Owner of resources Cannot own resources besides the processor’s, accesses all resources of the task, to which it belongs Own address space Address space of the task, to which it belongs Common address space Contains one or several threads Element of a task Communication beyond the task boundaries, preferred via messages Communication between the threads, preferred via shared data
24
State “runnable” or “ready”
IA 4 basic states State “running” the task is processed State “runnable” or “ready” all time conditions for the process are fulfilled what is missing is the start from the operating system i.e. the execution State “suspended” task is waiting for the occurrence of an event as soon as event occurs transition from state “suspended” to “ready” State “dormant” task is not ready because time conditions or other conditions are not fulfilled
25
– Invocation of a task cyclically or at certain times
IA State diagram of a task Planning of tasks ask Declaration Deletion of of the task – Invocation of a task cyclically or at certain times "dormant" – “Planning” is the transition from the state “dormant” into the state “runnable” planning "suspen- ded" "runnable" "running"
26
§ Real-time programming
IA § Real-time programming Problem definition Real-time programming methods Tasks Synchronization of tasks Communication between tasks
27
Classification of the actions of a task
IA Classification of the actions of a task – Two actions of tasks are called parallel if they can run simultaneously – Two actions are called sequential if they are arranged in a certain sequential order – Two actions of two different tasks are called concurrent if they can run simultaneously (outer parallelism) – Two actions of one task are called simultaneous, if they can be run at the same time Actions = Threads
28
Requirement on the execution of tasks
IA Requirement on the execution of tasks Synchronicity between tasks and the technical process Dependencies between tasks – Logical dependency because of the technical process e.g.: The desired values for the control have to be defined at least once before they can be used – Dependencies through the use of shared resources
29
the dependencies of tasks due to shared resources
IA Example the dependencies of tasks due to shared resources Task MEASUREMENTS Task PROCESS SUPERVISION Use of driver program for report printer Use of driver program for report printer Use analog- input Use analog- input Shared resources: report printer analog-input Input and conversion of measurements Input and conversion of measurements Output error message on report printer Output error message on report printer possibility of deadlock! Use of driver program for report printer Use of driver program for report printer Use analog- input Use analog- input Print- measurements and time Input and monitor process variables
30
Problems of dependency Deadlock:
IA Problems of dependency Deadlock: Two or more tasks block themselves mutually Permanent blocking (livelock, starvation) A conspiracy of tasks block a task -+ Time coordination of the tasks = synchronization of tasks limitation of the free parallel execution The synchronization of tasks is equivalent to the synchronization of their actions.
31
IA Example for a deadlock Road crossing without traffic lights. Traffic rule is “right before left“.
32
Example for a livelock: “ The Dining Philosophers”
IA Example for a livelock: “ The Dining Philosophers” every philosopher needs 2 chopsticks to eat no chopstick can be reserved
33
Synchronization methods
IA Synchronization methods – Semaphore Critical regions Rendezvous concept Basic idea of all synchronization methods – Task has to wait until a certain signal or event occurs – Use of waiting conditions at critical places
34
§ Real-time programming
IA § Real-time programming Problem definition Real-time programming methods Tasks Synchronization of tasks Communication between tasks
35
Exchange of data between parallel running tasks
IA Definition Synchronization = Fulfillment of time related and logical conditions in the parallel run of tasks Communication = Exchange of data between parallel running tasks
36
Possibilities of data communication
IA Possibilities of data communication – Shared memory (commonly used memory) common variable common complex data structure – Sending of messages • transmission of messages from a task to another (message passing) • especially in distributed systems
37
Different kinds of communication – Synchronous communication
IA Different kinds of communication – Synchronous communication • Sending and receiving processes communicate at a certain predefined position in the program flow waiting through blocking – Asynchronous communication Data are buffered no waiting times
38
Real Time Programming with Ada
Lady Ada Lovelace Ada Name of the 1. female programmer Named in the honor of the mathematician Augusta Ada Byron, Countess of Lovelace, daughter of Lord Byron. Ada Lovelace ( ) worked with Charles Babbage on his “Difference-and Analytic-Engine”. The idea of programming this machine with punched cards can be traced back to Ada Lovelace.
39
Characteristics of Ada
IA Characteristics of Ada Suitable for the development of extensive software systems (> 107 lines) Modularization concepts Separation between specification and implementation of units Support to reuse of software components Exception handling as part of the module’s code Real time specific features Fulfillment of absolute and relative time requirements Execution of parallel activities Prioritization and scheduling of parallel activities Extensive validation of each Ada95 compiler against compliance standards Several thousand test programs
40
Specific functionality encapsulated in executable pieces of code
IA Ada 95 program units Subprograms Specific functionality encapsulated in executable pieces of code -+ Functions and procedures Concurrently executing programs Different program sequences that are executed at the same time -+ Tasks Shared resources with synchronized access Data structures with access protected by monitors -+ Protected variables Reusable libraries Group of functionality related subprograms, tasks and protected units -+ Packages
41
Ada 95 basic program structure
IA Ada 95 basic program structure Ada program protected PT; Main routine Libraries package A is task T; procedure begin ... end main; main is begin ... function F; end A; procedure P; . . . No explicit superordinate program Definition of a subprogram as main routine Execution of program units from the main routine Programs units stored in libraries or directly declared
42
Example of unit structure
IA Example of unit structure Specification = external interface Implementation = realization of the program unit Specification (package declaration) package package_name is declaration (types, subprograms, tasks, protected units, ...) private private part (declaration) end package_name; externally visible part declaration Implementation (package body) package body package_name is further declaration subprograms’ bodies begin instructions end package_name; externally invisible part instructions
43
Declaration and implementation of units
IA Declaration and implementation of units – Separation of specification and implementation in different files Specification (File General.ads) package General is Implementation (File General.adb) package body General is protected body Variable is function read return Float is begin return protectedData; end read; Externally visible protected Variable is function read return Float; private protectedData: end Variable; Implementing instructions Float:= 0.0; end Variable; ... Externally invisible ... end General; end General;
44
Concurrent programming concepts
IA Concurrent programming concepts Requirement to enable the reaction to simultaneous events -+ Concept of parallel tasks to perform parallel activities Support of concurrent programming Task unit Task as a concurrent, parallel running sequences of commands Task as an encapsulated unit -+ Declarations made within a task are not visible outside it Communication between task units -+ Message passing and via shared variables
45
Planning of tasks done at the begin of the superordinate program unit
IA Realization of tasks Declaration and implementation of tasks in a package or directly in the program unit Planning of tasks done at the begin of the superordinate program unit End of execution and deletion after reaching the task’s end statement package package_name is task T1; task T2; end package_name; with package_name; use package_name; procedure main_procedure_name is task T3; package body package_name is task body begin ... end T1; end T2; T1 is task body T3 is ... end T3; Planning of all T3 T2 is begin ... end main_procedure_name ; end package_name;
46
Task type specification / implementation
IA Task types and objects Task types provide a template for the instantiation of task objects Tasks of the same type have similar properties and functionality Individual task objects can be parameterized during instantiation Inclusion of package General package General is task type monitorValue(X: task body monitorValue(X: begin With General; use procedure main is General; Integer); Integer) is M1 : monitorValue(1); M2 : monitorValue(2); M3 : monitorValue(3); -- Implementation ... end monitorValue; code begin -- Main code ... end main; Task objects declaration end General; Task type specification / implementation
47
Synchronization of tasks
IA Synchronization of tasks Synchronization concepts in Ada 95 Logical synchronization Establish a timely sequence of the tasks’ execution -+ Rendezvous concept Resource oriented synchronization Establish rules to access shared resources -+ Protected units
48
The rendezvous concept (1)
IA The rendezvous concept (1) Handshake method Definition of timely synchronization points between the tasks Mutual wait of the tasks Rendezvous has on the calling side the form of a procedure call Definition of calling declaration (entry) and calling point (accept) Caller Receiver If necessary, data transfer Receiver.entryName; accept entryName do Rendezvous end entryName; If necessary, data transfer
49
The rendezvous concept (2)
IA The rendezvous concept (2) Declaration of entry points declared in the task’s specification Calls and accept statements in the tasks’ implementation code Function principle: task calls the entry point another task task T1 is entry sync; end T1; task body T1 is begin Calling declaration task T2 is entry sync; end T2; task body T2 is begin task T3 is entry sync; end T3; task body T3 is begin Calling point ... accept sync; ... accept sync; ... T2.sync; accept sync; T3.sync; T1.sync; end T1; end T2; Call of another task’s calling point end T3; -+ Execution sequence is guaranteed in the code above T1, T2, T3, T1…
50
T1 T2 T3 Dynamic run of the multitask program example Waiting Waiting
IA Dynamic run of the multitask program example T1 T2 T3 accept sync; accept sync; Waiting Waiting T2.sync; accept sync; Waiting T3.sync; T1.sync;
51
Alternative sequence with the select statement
IA The select statement Alternative sequence with the select statement Conditional synchronization Timed waiting select accept or accept entryName1; select Receiver.entryName; Runs immediately, if there is no call entryName2; ... else -- Alternative code else -- Alternative code Runs immediately, if rendezvous is not possible end select; end select; select Timed waiting accept or accept ... entryName1; select Receiver.entryName; or Runs if no Runs if rendezvous is not possible within time_out entryName2; call is received within time_out delay time_out; -- Alternative code or delay time_out; end select; -- Alternative code end select;
52
Protected units and resource synchronization (1)
IA Protected units and resource synchronization (1) Problem of accessing shared variables Monitors for exclusive access Request from a task – Interface as protected operation Protected operations – Access to the shared variable controlled by a intern monitor … Protected operations are not executed in parallel Sequential ordering of simultaneous requests Monitor Shared variable -+ Tasks access the shared variable exclusively
53
Protected units and resource synchronization (2)
IA Protected units and resource synchronization (2) Protected operations protected S is entry P; procedure V; function G return Boolean; Protected operations are mutually exclusive All variables of a protected unit have to be declared private A procedure can manipulate the shared variable in any way -+ Functions have only read access The entries are protected operations private Sem : Boolean end S; := True; protected entry P body when S is Sem = True is Sem := False; end P; Entry condition procedure V is begin Sem := True; end V; ... end S;
54
Communication between tasks (1)
IA Communication between tasks (1) Synchronous communication Extension of the rendezvous synchronization with data exchange Mutual wait Data exchange during the rendezvous Data only valid within the rendezvous Example of synchronous communication task body T2 is task body T1 is begin T2.entryName(I:Integer); end T1; Entry syntax with data transfer begin accept entryName(I:Integer)do ... end entryName; end T2; -+ Message passing principle T1 is blocked during the rendezvous
55
Communication between tasks (2)
IA Communication between tasks (2) Asynchronous communication Data exchange via shared variables Protected operation for access No waiting/ blockage during communication Example of asynchronous communication protected body M is procedure write(I:Integer); task body T1 is begin M.write(I:Integer); end T1; task body T2 is temp: Integer; begin temp := M.read; ... end T2; function read private Val:Integer; end M; return Integer; -+ Shared variables and protected operations
56
Possible delay of the execution Until a defined point in time
IA Time operations Possible delay of the execution Until a defined point in time For a fixed time period Ada 95 statements delay and delay until Time units and operations defined in the core packages For standard applications, second For real time applications ms, μs, ns Operations Add and subtract time variables Read the current system time During a delay, the processor is occupied by another process ready to run After a delay, a task may have to wait to receive the processor -+ Preemption by a higher priority task
57
Example of time operations
IA Example of time operations task T1 is task T2 is ms: Duration := 0.001; next_call: Time; ms: Duration := begin loop read_sensor; delay 10*ms; end loop; end T1; 0.001; Execution time < 10ms, but not constant begin loop Current time next_call := Clock + 10*ms; read_sensor; delay until end loop; end T2; next_call; read_sensor execution Task T1 running blocked 32 t Task T1 activates read_sensor after a 10ms time delay c:> The subprogram activation time is relative to the previous execution Task T2 activates the read_sensor every 10ms c:>The subprogram is activated at fixed times
58
Modularized exception handling
IA Modularized exception handling Exceptions due to irregular operations possible -+ Treatment of exceptions (exception handling) Ada 95 exception handling concept: Exception handling blocks are implemented as part of the unit’s code If an exception occurs, control is given to the exception handling blocks Several exceptions can be handled in a single handling block Any exception not handled is propagated to the calling unit, if there is any Exceptions are raised by the runtime environment or by the program code -+ Runtime environment: predefined exceptions -+ Program code: custom defined exceptions
59
Exception handling example
IA Exception handling example procedure main is begin A:= readSensor(1); B:= readSensor(2); function readSensor (X:Integer) return Float is begin temp := read(X); if (temp < 0.0) then raise Sensor_Exception; -- Custom defined exception else return temp; end if; exception when Sensor_Exception => -- Handles sensor exceptions ... end readSensor; rate:= A/B; -- Could be exception a division by 0 when Constraint_Exception => -- Handles divisions by 0 ... when others => -- Handles ... end main; all other exceptions Handling blocks implement the exception handling Sensor exceptions handled within the function readSensor; others are propagated Activation of specific code by constraint exceptions Predictable system behavior also under exception conditions
60
Real time extensions to Ada 95 (1)
IA Real time extensions to Ada 95 (1) Tasks with dynamic priorities Base priority is established on its specification Actual priority can differ from the above -+ Priority inheritance Hint: In Ada, a lower number means a lower priority! Specification Implementation task T1 is pragma Priority(10); end T1; task body begin T2.sync; end T1; T1 is T1 calls T2’s entry point T2 executes here with T1’s priority Base priority task T2 is pragma Priority(1); entry sync; end T2; task body T2 is begin accept sync do ... end sync; Dynamic base priority change Set_Priority(20); end T2;
61
Real time extensions to Ada 95 (2)
IA Real time extensions to Ada 95 (2) Scheduling methods The method used to schedule tasks can be specified Pragma Task_Dispatching_Policy Default method: fixed priorities; FIFO within the priorities Further scheduling methods can be implemented and used Access to protected units and the Priority Ceiling Protocol Ceiling priority of protected units can be defined in its specification Default value: system’s highest priority Ceiling priority rules guarantee that there is no chance of deadlock
62
Further language extensions in Ada 95
IA Further language extensions in Ada 95 Dynamic character strings Interfaces to other PL I/O core language + base library System programming Real-time processing Distributed systems Information system Numerics Security Inline- machine- language Interrupts .... Flexible scheduling Flexible priorities Flexible queues Partitions Inter- partition communication Static and dynamic RPCs .... Decimal format "PIC" format .... Control over internal numeric formats Complex numbers .... Code revision and code inspection Reduction of available language constructs ... ....
63
Question referring to Chapter 6.4
IA Question referring to Chapter 6.4 State some of the differences between the real-time programming language Ada95 and the PLC language FBD concerning: Answer Ada FBD Notation text-based language graphical language Language level universal high level language language specific for control application Application areas suitable for large projects suitable for small projects only Real-time features extensive real-time features only restricted real-time capabilities
64
the ride and enters the church.
IA Question The following procedure should be implemented in Ada using two tasks: A pedestrian is waiting for a taxi and wants to get to the church. He pays the ride and enters the church. On the next slide there a three different code examples for this problem. Which is the correct one and what are the others doing?
65
code examples Alternative 1 Alternative 2 Alternative 3 IA
task pedestrian; task pedestrian is entry taxi; end pedestrian; task pedestrian; task body begin -- do pedestrian is task body pedestrian is begin -- do something taxi.drive; end pedestrian; something task body is begin -- do pedestrian taxi.drive; -- do something end pedestrian; something accept taxi; -- do something end pedestrian; task taxi is entry drive; end taxi; task taxi is entry drive; end taxi; task taxi is task body begin -- do taxi is entry end taxi; pedestrian; task body taxi is begin -- do something accept drive do drive.to(church); end drive; -- drive away end taxi; something accept drive; drive.to(church); -- drive away end taxi; task body begin -- do taxi is something accept pedestrian; drive.to(church); -- drive away end taxi; f - Implements only the timing synchronization without joint code execution ./- Implements the timing synchronization with joint code execution f - Does not implement the mutual synchronization
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.