0% found this document useful (0 votes)
60 views21 pages

Lect 5

The document discusses real-time programming languages like Ada and Java. It provides an overview of key features of Ada like its structured and statically typed design, support for concurrency through tasks, and mechanisms for communication between tasks. For Java, it describes how concurrency is implemented through threads, how threads are created and terminated, and synchronized methods for thread communication and coordination. It also discusses enhancements provided by the Real-Time Specification for Java (RTSJ) to make Java more suitable for real-time applications, including improvements to memory management, time handling, and scheduling of real-time threads.

Uploaded by

Rusla Sipayung
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views21 pages

Lect 5

The document discusses real-time programming languages like Ada and Java. It provides an overview of key features of Ada like its structured and statically typed design, support for concurrency through tasks, and mechanisms for communication between tasks. For Java, it describes how concurrency is implemented through threads, how threads are created and terminated, and synchronized methods for thread communication and coordination. It also discusses enhancements provided by the Real-Time Specification for Java (RTSJ) to make Java more suitable for real-time applications, including improvements to memory management, time handling, and scheduling of real-time threads.

Uploaded by

Rusla Sipayung
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Real-time Programming

RT Programming Language

Aim of this Lecture

brief overview, not a tutorial


to illustrate: how different programming languages realize general concepts that each programming languages focuses on different aspects

RT Languanges: Ada Java Other Languages

Real-time Programming

2/61

5. RT Programming languanges

Ada designed for United States Department of Defense during 1977-1983 targeted at embedded and real-time systems Ada 95 revision used in critical systems (avionics, weapon systems, spacecrafts) free compiler: gnat
Ada Lovelace (1815-1852)

Real-time Programming

3/61

5. RT Programming languanges

Page 1

Main Principles structured, statically typed imperative computer programming language strong typing modularity mechanisms (packages) run-time checking parallel processing (tasks) exception handling object-oriented programming (Ada95)

Real-time Programming

4/61

5. RT Programming languanges

Concurrency: Tasks task = the unit of concurrency explicitly declared (no fork/join statement, cobegin, ...) tasks may be declared at any program level created implicitly upon entry to the scope of their declaration or via the action of an allocator

Real-time Programming

5/61

5. RT Programming languanges

Tasks: interaction communication and synchronization via a variety of mechanisms: rendezvous (a form of synchronized message passing) protected units (a form of monitor) shared variables support for hierarchies, parent-child, guardian-dependent relations

Real-time Programming

6/61

5. RT Programming languanges

Page 2

Communication remote invocation with direct asymmetric naming one task denes an entry and then, within its body, accepts any incoming call (accept statement) a rendezvous occurs when one task calls an entry in another task selective waiting allows a process to wait for more than one message

Real-time Programming

7/61

5. RT Programming languanges

Task States

Real-time Programming

8/61

5. RT Programming languanges

Time access to clock: package Calendar abstract data type Time function Clock for reading time data type Duration predefined fixed point real for time calculations conversion utilities (to human readable units) waiting: delay, delay until statements

Real-time Programming

9/61

5. RT Programming languanges

Page 3

Ada Example task Ticket_Agent is entry Registration(...); end Ticket_Agent; task body Ticket_Agent is -- declarations Shop_Open : Boolean := True; begin while Shop_Open loop select accept Registration(...) do -- log details end Registration; or delay until Closing_Time; Shop_Open := False; end select; -- process registrations end loop; end Ticket_Agent;
Real-time Programming 10/61 5. RT Programming languanges

Java object-oriented programming language developed by Sun Microsystems in the early 1990s compiled to bytecode (for a virtual machine), which is compiled to native machine code at runtime syntax of Java is largely derived from C/C++

Real-time Programming

11/61

5. RT Programming languanges

Concurrency: Threads predefined class java.lang.Thread - provides the mechanism by which threads (processes) are created

to avoid all threads having to be child classes of Thread, it also uses a standard interface: public interface Runnable { public abstract void run(); } any class which wishes to express concurrent execution must implement this interface and provide the run() method

Real-time Programming

12/61

5. RT Programming languanges

Page 4

Threads: Creation dynamic thread creation, arbitrary data to be passed as parameters thread hierarchies and thread groups can be created no master or guardian concept

Real-time Programming

13/61

5. RT Programming languanges

Threads: Termination one thread can wait for another thread (the target) to terminate by issuing the join method call on the target's thread object the isAlive method allows a thread to determine if the target thread has terminated garbage collection cleans up objects which can no longer be accessed main program terminates when all its user threads have terminated

Real-time Programming

14/61

5. RT Programming languanges

Synchronized Methods monitors can be implemented in the context of classes and objects lock associated with each object; lock cannot be accessed directly by the application but is affected by the method modier synchronized block synchronization synchronized method - access to the method can only proceed once the lock associated with the object has been obtained non-synchronized methods do not require the lock, can be called at any time

Real-time Programming

15/61

5. RT Programming languanges

Page 5

Waiting and Notifying wait() always blocks the calling thread and releases the lock associated with the object notify() wakes up one waiting thread; the one woken is not defined by the Java language notifyAll() wakes up all waiting threads

if no thread is waiting, then notify() and notifyAll() have no effect

Real-time Programming

16/61

5. RT Programming languanges

Illustration

Real-time Programming

17/61

5. RT Programming languanges

Real Time Java Java is not directly suitable for real time systems: no support for priority based scheduling does not prevent priority inversion garbage collection introduces unpredictable delays Real-Time Specification for Java (RSTJ), enhanced areas: thread scheduling and dispatching memory management (garbage collection) synchronization and resource sharing asynchronous event handling, transfer of control, thread termination physical memory access

Real-time Programming

18/61

5. RT Programming languanges

Page 6

Clocks java.lang.System.currentTimeMilis returns the number of milliseconds since Jan 1 1970 Real Time Java adds real time clocks with high resolution time types

Real-time Programming

19/61

5. RT Programming languanges

The Real-Time Specification for Java To give the background of the RTSJ and the NIST requirements To provide an introduction to Memory management Time values and clocks Schedulable objects and scheduling

Real-time Programming

20/61

5. RT Programming languanges

Background and NIST Requirements In the late 1990s, the US National Institute of Standards and Technology (NIST) coordinated the derivation of several guiding principles and a set of requirements for real-time extensions to the Java platform

Among the guiding principles was that Real-Time Java (RTJ) should take into account current real-time practices and facilitate advances in the state of the art of real-time systems implementation technology

Real-time Programming

21/61

5. RT Programming languanges

Page 7

Support for Current State of Practice

Fixed priority and round robin scheduling Mutual exclusion locking (avoiding priority inversion) Inter-thread communication (e.g. semaphores) User-defined interrupt handlers and device drivers including the ability to manage interrupts (e.g., enabling and disabling) Timeouts and aborts on running threads

The NIST group recognized that profiles of RTJ were necessary in order to cope with the wide variety of potential applications, these included: safety critical, no dynamic loading, and distributed real-time profiles

Real-time Programming

22/61

5. RT Programming languanges

Implementations must provide A framework for finding available profiles Bounded pre-emption latency on any garbage collection A well-defined model for real-time Java threads Communication and synchronization between real-time and non real-time threads Mechanisms for handling internal and external asynchronous events Asynchronous thread termination Mutual exclusion without blocking The ability to determine whether the running thread is real-time or non real-time A well-defined relationship between real-time and non real-time threads

Real-time Programming

23/61

5. RT Programming languanges

RTSJ Guiding Principles Be backward compatible with non real-time Java programs Support the principle of Write Once, Run Anywhere but not at the expense of predictability Address the current real-time system practice and allow future implementations to include advanced features Give priority to predictable execution in all design trade-offs Require no syntactic extensions to the Java language Allow implementers flexibility

Real-time Programming

24/61

5. RT Programming languanges

Page 8

Overview of Enhancements

The RTSJ enhances Java in the following areas: memory management time values and clocks schedulable objects and scheduling real-time threads asynchronous event handling and timers asynchronous transfer of control synchronization and resource sharing physical memory access

Real-time Programming

25/61

5. RT Programming languanges

Warning

It should be stressed that the RTSJ only really addresses the execution of realtime Java programs on a single processor systems. It attempts not to preclude execution on a shared-memory multiprocessor systems but it has no facilities directly to control, say, allocation of threads to processors.

Real-time Programming

26/61

5. RT Programming languanges

Memory Management I Many real-time systems have only a limited amount of memory available because of the cost other constraints associated with the surrounding system (e.g., size, power or weight constraints) It is necessary to control how this memory is allocated so that it can be used effectively Where there is more than one type of memory (with different access characteristics), it may be necessary to instruct the compiler to place certain data types at certain locations By doing this, the program is able to increase performance and predictability as well as interact with the outside world

Real-time Programming

27/61

5. RT Programming languanges

Page 9

Heap Memory The JVM is responsible for managing the heap Key problems are deciding how much space is required (the sizeEstimator class in RTSJ helps here) and when allocated space can be released The latter can be handled in several ways, including: 1. require the programmer to return the memory explicitly this is error prone but is easy to implement 2. require the JVM to monitor the memory and determine when it can logically no longer be accessed 3. require the JVM to monitor the memory and release chunks which are no longer being used (garbage collection); this is, perhaps, the most general approach as it allows memory to be freed even though its associated access type is still in scope

Real-time Programming

28/61

5. RT Programming languanges

Real-Time Garbage Collection From a real-time perspective, the approaches have an increasing impact on the ability to analyse the timing properties of the program Garbage collection may be performed either when the heap is full or by an incremental activity In either case, running the garbage collector may have a significant impact on the response time of a time-critical thread All objects in standard Java are allocated on the heap and the language requires garbage collection for an effective implementation The garbage collector runs as part of the JVM Although there has been much work on real-time garbage collection and progress continues to be made, there is still a reluctance to rely on these techniques in timecritical systems

Real-time Programming

29/61

5. RT Programming languanges

Memory Areas The RTSJ provides memory management which is not affected by the vagaries of garbage collection It defines memory areas, some of which exist outside the traditional Java heap and never suffer garbage collection RTSJ requires that the garbage collector can be preempted by real-time threads and that there should be a bounded latency for preemption to take place The MemoryArea class is an abstract class from which for all RTSJ memory areas are derived When a particular memory area is entered, all object allocation is performed within that area

Real-time Programming

30/61

5. RT Programming languanges

Page 10

Subclasses of MemoryArea

HeapMemory allows objects to be allocated in the Java heap ImmortalMemory is shared among all threads; objects created here are never subject to garbage collection and are freed only when the program terminates ScopedMemory is a memory area for objects that have a well-defined lifetime; associated with each scoped memory is a reference count which keeps track of how many real-time entities are currently using the area
When the reference count reaches goes from 1 to 0, all objects resident in the scoped memory have their finalization method executed and the memory is reclaimed

The ScopedMemory class is an abstract class which has several subclasses


VTMemory: where allocations may take variable amounts of time LTMemory: where allocations occur in linear time (related to the size of the object)
Real-time Programming 31/61 5. RT Programming languanges

Memory Parameters Can be given when real-time threads and asynchronous event handlers are created; they specify the maximum amount of memory a thread/handler can consume in a memory area, the maximum amount of memory that can be consumed in immortal memory, a limit on the rate of allocation from the heap (in bytes per second), Can be used by the scheduler as part of an admission control policy and/or for the purpose of ensuring adequate garbage collection

Real-time Programming

32/61

5. RT Programming languanges

Memory Management Classes


GarbageCollector MemoryArea SizeEstimator MemoryParameters

HeapMemory

ScopedMemory

ImmortalMemory

LTMemory RTSJ class RTSJ abstract class

VTMemory

Real-time Programming

33/61

5. RT Programming languanges

Page 11

Time Values HighResolutionTime encapsulates time values with nanosecond granularity A value is represented by a 64 bits milliseconds and a 32 bits nanoseconds component The class is an abstract class which has three subclasses: AbsoluteTime: expressed as a time relative to some epoch. This epoch depends of the associated clock. It might be January 1, 1970, GMT for the wall clock or system start-up time for a monotonic clock RelativeTime RationalTime is a relative-time type which has an associated frequency. It is used to represent the rate at which certain events occur (for example, periodic thread execution) Time values are also relative to particular clocks

Real-time Programming

34/61

5. RT Programming languanges

Clocks The RTSJ Clock class defines the abstract class from which all clocks are derived The specification allows many different types of clocks; for example, there could be a CPU execution-time clock (although this is not required by the RTSJ) There is always one real-time clock which advances monotonically A static method getRealtimeClock allows this clock to be obtained

Real-time Programming

35/61

5. RT Programming languanges

Time Values and Clocks

HighResolutionTime

AbsoluteTime relativeTo relativeTo

RelativeTime

Clock

relativeTo

RationalTime

standard Java interface RTSJ class RTSJ abstract class


Real-time Programming 36/61 5. RT Programming languanges

Page 12

Summary RTSJ The RTSJ originates from the desire to use Java in real-time Javas main problems include unsuitable memory management because of garbage collection and poor support for clocks and time The RTSJ stems from the NIST requirements Memory management is augmented by imoortal and scoped memory area Clocks and time are augmented by a real-time clock and by high resolution absolute and relate time types

Real-time Programming

37/61

5. RT Programming languanges

More Exotic Languages Real Time Euclid Occam Pearl

Real-time Programming

38/61

5. RT Programming languanges

Real Time Euclid real-time language, restriction to time-bounded constructs programmer is forced to specify time bounds and timeouts in all loops, waits and device accessing statements restrictions: absence of dynamic data structures absence of recursion time bounded loops | maximum number of iterations must be specied only academic proposal, never widely used

Real-time Programming

39/61

5. RT Programming languanges

Page 13

Occam concurrent programming language that builds on the Communicating Sequential Processes (CSP) formalism concurrency: cobegin (PAR) mainly of pedegogical interest, not widely used
ALT count1 < 100 & c1 ? data SEQ count1 := count1 + 1 merged ! data count2 < 100 & c2 ? data SEQ count2 := count2 + 1 merged ! data status ? request SEQ out ! count1 out ! count2

Real-time Programming

40/61

5. RT Programming languanges

Pearl Process and Experiment Automation Realtime Language language designed for multitasking and real-time programming developed since 1977 used mainly in Germany

Real-time Programming

41/61

5. RT Programming languanges

Pearl: Scheduling support Scheduling on events and time instants, examples: ALL 0.00005 SEC ACTIVATE Highspeedcontroller; cyclical activation of a controller with a frequency of 20 kHz AT 12:00 ALL 4 SEC UNTIL 12:30 ACTIVATE lunchhour PRIO 1; cyclical scheduling, every 4 seconds between 12:00 and 13:00 hrs with high priority WHEN fire ACTIVATE extinguish; activation of the task 'extinguish', when interrupt fire occurs

Real-time Programming

42/61

5. RT Programming languanges

Page 14

About (Not Just) Programming ... choose the right language for a given problem lectures can help often it is not your decision master the tool practice, practice, practice, ...

Real-time Programming

43/61

5. RT Programming languanges

POSIX Portable Operating System Interface for uniX standardised operating system interface and environment, including: system calls standard C libraries a command shell based on various avors of Unix, but vendor-independent original release in 1988, formally designated as IEEE 1003

Real-time Programming

44/61

5. RT Programming languanges

POSIX Versions Modularized set of standards: POSIX.1, Core Services standard C process creation, control signals, segmentation violations, illegal instructions, bus errors floating point exceptions POSIX.1b, Real-time extensions priority scheduling real-time signals, clocks and timers semaphores, message passing, shared memory POSIX.1c, Threads extensions thread creation, thread scheduling thread synchronization, signal handling

Real-time Programming

45/61

5. RT Programming languanges

Page 15

Outline threads (pthread.h) time (time.h, sys/time.h) signals (signal.h)

Real-time Programming

46/61

5. RT Programming languanges

Concurrency in POSIX provides two mechanisms: fork and pthreads fork creates a new process pthreads are an extension to POSIX to allow threads flat structure

Real-time Programming

47/61

5. RT Programming languanges

Pthreads pthread = posix thread specied by the IEEE POSIX 1003.1c standard (1995) set of C language programming types and procedure calls, implemented with a pthread.h header/include le and a thread library; compilation: gcc -pthread Pthreads API: thread management (creation, termination, joining, ...) mutexes (lock, unlock, ...) condition variables (not covered in lecture)

Real-time Programming

48/61

5. RT Programming languanges

Page 16

Example I #include <pthread.h> pthread_t id; void *fun(void *arg) { // Some code sequence } main() { pthread_create(&id, NULL, fun, NULL); // Some other code sequence }

Real-time Programming

49/61

5. RT Programming languanges

Example II
#include <pthread.h> #include <stdio.h> #define NUM_THREADS 5 void *PrintHello(void *threadid) { printf("\n%d: Hello World!\n", threadid); pthread_exit(NULL); } int main (int argc, char *argv[]) { pthread_t threads[NUM_THREADS]; int rc, t; for(t=0; t<NUM_THREADS; t++){ printf("Creating thread %d\n", t); rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t); if (rc){ printf("ERROR; return code from pthread_create() is %d\n", rc); exit(-1); } } pthread_exit(NULL); } 5. RT Programming languanges Real-time Programming 50/61

Semaphores = Mutexes pthread mutex init (mutex,attr) pthread mutex lock (mutex) - attempt to lock a mutex, if the mutex is already locked, this call blocks the thread pthread mutex trylock (mutex) - if the mutex is locked, returns immediately with busy error code pthread mutex unlock (mutex)

Real-time Programming

51/61

5. RT Programming languanges

Page 17

Communication signals message passing

Real-time Programming

52/61

5. RT Programming languanges

Signals: Motivation classical interrupts: external interrupt (short-lived) execution of a pre-installed interrupt-handler

normal execution temporarily suspended during the run of an interrupt-handler even if a handler has been installed by a certain process, its execution will interrupt any process that happens to be active when the corresponding interrupt signal is received

Real-time Programming

53/61

5. RT Programming languanges

Virtual Interrupts process with threads ~ virtual computer can we use virtual interrupts within the process? signals

Real-time Programming

54/61

5. RT Programming languanges

Page 18

Signals signal is sent towards a particular process, and handlers can be installed that are guaranteed to interrupt that process only signal can be sent to a process by executing kill(pid, sig) where pid is process number(0 means self) signals are also generated by dividing by zero, addressing outside your address space, etc. each thread can block incoming signals on a per-signal basis, dene signal handlers for each signal it might receive, and queue signals no data transfer can be used also for exception handling

Real-time Programming

55/61

5. RT Programming languanges

List of Signals SIGABRT Abnormal termination signal caused by the abort()function. SIGALRM The timer has timed-out. SIGFPE Arithmetic exception, such as overflow or division by zero. SIGHUP Hangup detected on controlling terminal or death of a controlling process. SIGILL Illegal instruction indicating a program error. SIGINT Interrupt special character typed on controlling keyboard (Ctrl-C). SIGKILL Termination signal. This signal cannot be caught or ignored. SIGPIPE Write to a pipe with no readers

Real-time Programming

56/61

5. RT Programming languanges

List of Signals SIGQUIT Quit special character typed on controlling key- board. SIGSEGV Invalid memory reference. Like SIGILL, portable programs should not intentionally generate invalid memory references. SIGTERM Termination signal. SIGUSR1 Application-dened signal 1. SIGUSR2 Application-dened signal 2. SIGCHLD Child process terminated or stopped. SIGCONT Continue the process if it is currently stopped; otherwise, ignore the signal.

Real-time Programming

57/61

5. RT Programming languanges

Page 19

Signal Handling same basic idea as for real interrupt-handling; a handler for a signal gets called "spontaneously", just as if the interrupted code had made the call itself like an interrupt handler ignores what process is running, a signal handler ignores what thread is running difference: signals are not delivered until the receiving process is actually running internally generated signals { the receiving process is already running per denition

Real-time Programming

58/61

5. RT Programming languanges

Messages support for interprocess communication see sys/ipc.h, sys/msg.h, mqueue.h, ... also note Messsage Passing Interface (MPI) not directly related to POSIX used mainly for distributed computation

Real-time Programming

59/61

5. RT Programming languanges

Getting Time POSIX requires at least one clock of minimum resolution 50Hz (20ms) time() - seconds since Jan 1 1970 gettimeofday() - seconds + nanoseconds since Jan 1 1970 tm - structure for holding human readable time

Real-time Programming

60/61

5. RT Programming languanges

Page 20

Timers simple waiting: sleep, nanosleep timers: timer t, can be set: relative/absolute time single alarm time and an optional repetition period timer rings by sending a signal

Real-time Programming

61/61

5. RT Programming languanges

Page 21

You might also like