1.25k likes | 1.5k Views
Introduction to real-time systems. CI346. What does ‘Real Time’ mean?. All computer systems model some aspect of the outside world the timescale does not necessarily match that of the real world Real time systems required to conform to timescales imposed by the outside world
E N D
What does ‘Real Time’ mean? • All computer systems model some aspect of the outside world • the timescale does not necessarily match that of the real world • Real time systems required to conform to timescales imposed by the outside world • must work as fast as the outside world • must work as slowly as the outside world
What does ‘Real Time’ mean? • Hard real time • very tight deadlines • failure to meet deadlines is catastrophic • e.g. aerospace applications (autopilot)
What does ‘Real Time’ mean? • Soft real time • deadlines are more flexible • failure to meet deadlines is not necessarily catastrophic • e.g. multimedia applications (video display) • e.g. financial systems (payroll)?
Embedded systems • Real time systems are often computer systems which are part of (embedded in) a larger system • e.g. process control, autopilot, manufacturing • ‘Embedded’ often used as a synonym for real time
Characteristics • Need to cope with a variety of external events • software frequently large and complex • Reliable and safe • able to detect and recover from failures • Need to interact with external hardware
Characteristics • Need to be able to specify timing requirements • when to perform actions • when to complete actions by • what to do when deadlines are missed • granularity is important (e.g. IBM PC clock granularity is 55ms = 100yds at 600mph)
Characteristics • Event-driven rather than process-driven • external events must be dealt with as they occur • event ordering is not generally predictable • Generally uses concurrent processes • each event source can be handled by a separate process
Characteristics • Predictable response times • guaranteed maximum processing times (rules out e.g. use of Ethernet) • How to measure response times? • caching, pipelining etc. affect program speed • worst case is an order of magnitude slower than the average • fast programs + external time references
Object-oriented real time systems • O-O systems are modelled as collections of interacting objects • Objects are generally passive, responding only when requested to (sent a message) • Real time systems are modelled as collections of interacting processes • Processes are active entities, operating autonomously and interacting with each other whenever necessary
Object-oriented real time systems • O-O real time systems combine the two approaches • an OORTS is a collection of objects, each of which is associated with an autonomous process
Why concurrency? • Example: system to service multiple telex & fax connections • each device sends headers (e.g. date, time, message number) followed by the message itself
Why concurrency? • Non-concurrent (sequential) implementation: for each device loop if character available then get character if waiting for header then ... elsif in message header then ... elsif in message body then ... end if else try next device end if end loop
Design issues • Body of program is a large finite state machine with many states • program hard to read; transitions between states is buried in body of code • program is hard to maintain; adding new states may affect existing states
Design issues • All data ends up globally accessible • variables must retain their value from one pass through loop to the next • leads to more maintenance problems • Bottom-up approach to coding: get a character and then decide what to do
Why concurrency? • Concurrent implementation: loop wait for start of header process message header process message body end loop process message header: read a line of text from device
Why concurrency? read a line of text: while not end of line loop read a character from device end loop read a character: if character available then get character else suspend and activate another process end if • Allows use of a top-down approach to coding
Processes and programs • A program is a sequence of instructions • a passive collection of binary data stored on e.g. a disk • A processor is a device for processing instructions • a hardware device
Processes and programs • A process is the act of processing a sequence of instructions • requires a program and a processor • more than one process can be executing the same program • one process can execute more than one program • processes are also called tasks
Processes and programs • Operating systems provide separated environments for processes • separate address space • Each process is like an emulation of a complete independent computer • has its own private processor, memory and I/O devices • communication between processes is relatively expensive
Processes and programs • In real-time systems, processes are usually lightweight processes (aka threads) • they share the same address space • communication is by shared memory areas • Threads behave like a single computer with multiple processors • each OS process contains one or more threads
Processes and programs • Real-time kernel represents processes and threads using context blocks (aka descriptors, or many other names) • volatile context (copies of processor registers) • system info (process priority, periodicity, current state...)
Types of concurrency • Co-operative scheduling (’coroutines’) • processes suspend voluntarily when they need to wait for an event • processes must also suspend at regular intervals during lengthy processing
Types of concurrency • Pre-emptive scheduling (‘timeslicing’) • processes suspended as a result of interrupts from external hardware • problems with access to shared resources: impossible to predict at what point a process will be suspended
Implementing concurrency • Most languages don't provide support for concurrency (e.g. C, C++) • operating system (or runtime system) may support concurrency via a function library • Languages which do support concurrency include occam, Ada, Java
Implementing concurrency • Languages with concurrency facililties are not automatically real-time languages • depends on definition of how queues are managed etc. • even languages which are suitable for real-time are dependent on the underlying runtime system and operating system (e.g. clock granularity, interrupt latency)
Implementing concurrency • Real-time behaviour requires predictability • Java is not a real-time language! • garbage collection occurs at unpredictable times • the order in which waiting threads are woken up is not defined • no timing guarantees • Work is in progress on a real-time version of Java
Concurrency in Java • Derive from class Thread • implement the thread body by overriding the ‘run’ method public class MyThread extends Thread { public void run () { for (int i = 0; i < 1000; i++) { System.out.println(’Hello world’); } } }
Concurrency in Java • Create objects of the thread class: MyThread t1 = new MyThread(); MyThread t2 = new MyThread(); • Start the threads by calling ‘start’: t1.start(); t2.start(); • Each thread executes in parallel with the rest of the program • threads halt on exit from ‘run’
Synchronization • Problem if two threads try to update the same object: int i = sharedObject.get(); i = i + 1; sharedObject.set(i); • If sharedObject holds the value 5 and two threads execute this code: • each thread copies 5 into its own local variable, updates the local variable and sets sharedObject to 6
Synchronization • ‘Synchronized’ blocks prevent this happening: synchronized (sharedObject) { int i = sharedObject.get(); i = i + 1; sharedObject.set(i); } • only one thread at a time can execute a ‘synchronized’ block for a particular object • other threads must until they are allowed to enter it
Synchronization • Each Java object has an internal ‘lock’ variable and a queue for waiting threads • if the lock is clear, lock the object and enter the block • if the lock is set, wait in the queue • on exit from the block, clear the lock and wake up one of the waiting threads (if there are any)
Synchronization • This is not very O-O • data is protected at the point where it is accessed, not at the point it is defined • Solution: synchronized methods • any method can be marked as ‘synchronized’ in a class declaration public synchronized void() { ... } • only one thread can execute a synchronized method of a particular object at any one time
Synchronization • Problem: • it’s up to you to decide what methods should be synchronized • if there’s a maximum time you’re prepared to wait, you can’t specify a timeout • one solution: use a second thread to interrupt the first one after a timeout period (messy!)
Thread methods • sleep(n) – sleep for at least n milliseconds • getPriority(), setPriority(n)– change a thread’s priority • interrupt() – interrupt a thread • join(), join(n)– wait for a thread to die (indefinitely, or for up to n milliseconds) • Thread.currentThread()– get a reference to the currently-executing thread
Object methods • wait(), wait(n) – wait inside a synchronized block, releasing any object locks • note: threads can’t continue after a wait() until they have reacquired the lock • notify()– wake up one waiting thread • notifyAll()– wake up all waiting threads • note: this can only be used inside a synchronized block for the relevant object
Other approaches • Semaphores (Dijkstra) • a semaphore has an initial integer value N • the wait operation decrements the counter and suspends the caller if the counter becomes negative • the signal operation increments the counter and resumes suspended task(s)
Other approaches • Problems: • non-OO: client code must use semaphores to protect critical sections where shared data is updated • very low-level and hence error-prone • requires shared memory
Other approaches • Channels (mailboxes) • like a pipe in Unix: write to one end, read from the other • suspend if nothing to read • doesn't provide updatable shared data • can encapsulate shared data in a process with separate reader and writer channels
Safety and liveness • Concurrency provides many new opportunities for errors • Concurrent code must be safe (nothing ‘bad’ will happen) and live (something ‘good’ will eventually happen) • In a real-time system, it must be possible to establish an upper bound on ‘eventually’
Exclusion issues • Mutual exclusion • protection against more than one process updating the same data at the same time • synchronized methods can do this • Conditional exclusion • e.g. making tasks wait for write access to shared buffer if buffer is full
Exclusion issues • Use wait() and notify(): while (bufferEmpty()) { wait(); } Item i = getItem(); notifyAll(); return i; ----------------------------------- while (bufferFull()) { wait(); } insertItem(i); notifyAll();
Deadlock • Example: gridlocked cars at a junction • all paths are blocked so all cars are unable to move • unless a car moves the paths will remain blocked
Deadlock • An even simpler example: two trains going in opposite directions on a single track: • neither train can proceed unless the other train gets out of the way • if trains can’t go backwards, they’re stuck
Deadlock • Processes prevent each other from proceeding • Possible solutions • prevent (make deadlock impossible) • avoid (see it coming and take steps to avoid it happening) • recover (let it happen, then fix it) • ignore (cross fingers and hope)
Deadlock • Four necessary & sufficient conditions for deadlock: • tasks require to use a non-shareable resource • tasks hold onto resources while waiting for extra ones to be assigned to them • resources cannot be taken away from tasks by an outside agency • there is a circular chain of tasks requesting a resource held by another task
Deadlock • Tasks require to use a non-shareable resource: • virtualise resources, e.g. print spooling on disk (printer is non-shareable, disk is shareable) • not possible in all cases (e.g. a railway track is not shareable between two trains and can't be virtualised away!)
Deadlock • Tasks hold onto resources while waiting for extra ones to be assigned to them: • insist that all resources are allocated at once (task cannot proceed until all requests have been granted) • inefficient (resources will be allocated when they're not needed)
Deadlock • Resources cannot be taken away from tasks by an outside agency: • similar to the previous case (if any request fails, release all resources and loop back to start again) • inefficient, as before