G53SRP - System and Realtime Programming 2009/10
Notices
General feedback on coursework and exam performance for 2009/10: g53srp-0910-feedback.pdf
Module-related pages
Topics
- Concurrent
(multi-threaded) programming in Java
- Concurrency control in Java
- Real-time threads in RTSJ
- Scheduling, schedulability and resource
sharing
- Event-driven and asynchronous
programming in Java and RTSJ
- Hardware interfacing in
RTSJ
- (Interrupting threads, and asynchronous
transfer of control in RTSJ - not covered)
Course Notes
- Introduction: intro.ppt
/ g53srp-intro-6up.pdf
See also Burns & Wellings chapter 1, and Wellings
chapter 1. F22
Ariane
5
- Concurrent Programming with Java Threads: javathreads.ppt / g53srp-javathreads-6up.pdf
Code samples: Print10Test.java
Exam questions: 2004/5 Qn. 3, 2003/4 Qn 3
- Java Concurrency 1: Synchronisation: javaconcurrency1.ppt /
g53srp-javaconcurrency1-6up.pdf
Code samples: MethodNoSyncTest,
MethodSyncTest,
StaticMethodNoSyncTest,
StaticMethodSyncTest,
BlockSyncTest,
BlockSyncTest2
- Java concurrency 2: wait & notify: javaconcurrency2.ppt
/ g53srp-javaconcurrency2-6up.pdf
Code Samples: InfiniteBuffer,
InfiniteBufferTest,
ReaderWriterLock,
ReaderWriterLockTest
Exam questions: 2008/9 Qn. 1, 2005/6 Qn. 3 [partial solution - local access only; ignore incorrect capitalisation due to Word], 2004/5 Qn 4 (a)-(c), 2003/4
Qn 1 (b), (c), 2002/3 Qn 5
- Java clocks and time: clocksandtime.ppt
/ g53srp-clocksandtime-6up.pdf
Code samples: BusyDelay,
SleepDelay,
ClockGranularity,
DriftingClock, SleepAbsoluteDelay, SleepTimeUnitsDelay,
TimedWait,
TimerClock,
DriftingTimerClock
Exam questions: 2005/6 Qn. 2, 2003/4 Qn 1 (a), 2002/3 Qn 4
- Introduction to Real Time Scheduling: realtimescheduling.ppt
/ g53srp-realtimescheduling-6up.pdf
- Introduction to the Real Time
Specification for Java: rtsjintro.ppt
/ g53srp-rtsjintro-6up.pdf
RTSJ
Javadocs (local access only)
- RTSJ Clocks and Time: rtsjclocksandtime.ppt
/ g53srp-rtsjclocksandtime-6up.pdf
Code samples: RTClock,
RTSleep
- Realtime Threads (part I): realtimethreads.ppt
/ g53srp-realtimethreads-6up.pdf
Code samples: HelloScheduler,
HelloRealtimeThread,
NRTPriority
(/MyTimedTask),
RTPriority,
HelloPeriodicThread
Exam questions: 2008/9 Qn. 2
- RTSJ Asynchronous Events: asyncevents.ppt
/ g53srp-asyncevents-6up.pdf
Code samples: HelloAsyncEventHandler,
HelloAsyncEventHandler2,
SlowAsyncEventHandler,
AperiodicAsyncEventHandler,
SporadicAsyncEventHandler,
HelloOneShotTimer,
HelloOneShotTimer2,
HelloPeriodicTimer,
HelloPeriodicTimer2,
DisablePeriodicTimer,
HelloPOSIXSignalHandler,
HelloBoundAsyncEvent
Exam questions: 2008/9 Qn. 3, 2008/9 Qn. 5 (c)
- RTSJ Deadlines and cost: rtsjdeadlineandcost.ppt
/ g53srp-rtsjdeadlineandcost-6up.pdf
Code samples: HelloDeadlineMissHandler, HelloSchedulePeriodic, DeadlineMissAsyncEventHandler,
HelloCostOverrunHandler [Note: doesn't detect over-run on archer]
Exam questions: 2008/9 Qn. 2 (b)
- Feasibility Analysis: feasibility.ppt
/ g53srp-feasibility-6up.pdf
Exam questions: 2008/9 Qn. 4, 2005/6 Qn. 1, 2004/5 Qn. 2, 2003/4 Qn.2,
2002/3 Qn. 3
- Resource sharing: resourcesharing.ppt
/ g53srp-resourcesharing-6up.pdf
Exam questions: see 12 (Feasibility analysis)
- RTSJ Memory Areas and Parameters: memoryareas.ppt / g53srp-memoryareas-6up.pdf
Exam questions: 2008/9 Qn. 2 (c)
- Hardware interfacing: hardware.ppt / g53srp-hardware-6up.pdf
Appendix A: RS-232 serial / UART - an example of I/O interfacing.
Exam questions: 2008/9 Qn. 5, 2004/5 Qn. 1 (but would be done in terms of RTSJ
RawMemoryAccess and AsyncEvent/Handler); see also 2003/4 Qn. 4, 2003/3
Qn. 1 (we have not considered OS device drivers, but might interface to
the same device(s) using RTSJ).
- Please check before lecture(s)
Coursework
See
G53SRP_coursework.html due Friday 11th December 2009. Submit electronically using cw.
Exercises
Please check before lecture(s)
Java (on the command line)
- Write a "HelloWorld" class with a public static void main(String args[]) method which prints "Hello World".
- Compile it at the command line (Windows or UNIX) using javac: javac HelloWorld.java
- Run it at the comment line using java: java HelloWorld
- Check that it prints "Hello World"
- Move the .java file to its own directory, move to that directory and try running the application; it shouldn't work - why?
- Without changing directory of copying files use the
"-classpath" option to java to sucessfully run the application; why
does this work? (see for example the java command documentation)
- Move the .class
file to its own directory, move to that directory and try running the
application; it should work - why? Try re-compiling the application in
that directory using "javac HelloWorld.java"; it shouldn't work - why?
- Try disassembling the class: javap HelloWorld
- What is printed and why? Try some other disassembly options (see javap command documentation); is there any information missing completely from the class compared to the .java file?
Threads
- Write
a class which extends Thread
and prints 'Hello World'. Write an application which creates and starts
three such threads.
- Write
a Runnable
class which executes a busy loop enough times to take a few seconds and
then prints a message. Write an application that (a) runs the method
twice in succession (b) starts two threads that each run it once. Time
the two versions on a multi-core or multi-processor machine. Repeat for
four calls/threads
- What would happen if the threads had 'setDaemon(true)'
called on them before they were started?
- What would happen if one of the threads in (b) has 'setPriority(Thread.MAX_PRIORITY)'
called on it before starting it?
- [optional] Find out about Thread Local Data
- Write a method with the signature 'void
PAR(Runnable tasks [])'
which runs the given tasks in parallel, returning when they have all
completed. Try it out with a task such as that in Print10Test.
- What is the most tasks that it will execute in parallel?
Why?
Concurrency (1)
- Write a simple account class to keep track of a
current
balance and allow operations on it. In another class write a method
with the signature void
'transfer(Account from, Account to, int amount)'
which transfers the given amount from one account to another. Ensure
that the transfer is atomic as far as any other thread is concerned
(e.g. a thread which tries to sum the total money in all accounts
should never see an increase or decrease in the total).
- [optional] Write a simple Java program which exhibits deadlock. Work
out how/why it exhibits all four requirements for deadlock.
- Could the transfer example exhibit deadlock? Why/why not?
If it
does then uuder what circumstances? Could it be changed so that it did
not? How?
Concurrency (2)
- Write a finite buffer class. Test it using a version of the
InfiniteBufferTest in which the producer is very fast, but the consumer
takes some period of time to 'handle' each value.
- Under
what circumstances can the ReaderWriterLock give rise to starvation?
Modify the ReaderWriterLockTest program to demonstrate this.
- [optional] Modify the ReaderWriterLock so that it avoids starvation in
this
situation. What (if any) disadvantages does your new version have?
Java clocks and time
- Write an "alarm clock" which will print a message at (the
next) 3:30pm (or whatever time you prefer). Hint: see java.util.Calendar
for help for translating times.
- Using three java.util.TimerTasks
write a "drummer" application which prints "highhat" every 250ms,
"bassdrum" every 1000ms and "snare" every 1000ms, but starting 500ms
later than the bass drum. Justify the choice of the specific java.util.Timer
schedule
method(s) that you have used.
- Write
a variant of the finite buffer class from the Concurrency (2) exercises
which is "impatient", i.e. it should print "I'm waiting to put"/"I'm
waiting to get" (as appropriate) after every one second of waiting.
Test it with (a) a slow consumer and a fast producer and (b)
a
fast consumer and a slow producer.
RTSJ
-
See Running_RTSJ.html
for details of compiling/running RTSJ applications on the CS (Solaris)
computers.
- Use SSH (or equivalent) to log into archer. Copy RTClock.java to your own directory. Compile it ("javac RTClock.java") on archer. Run in on archer ("java RTClock" in that directory).
RTSJ clocks and time
- Write a program which performs an absolute sleep of 100ms duration 100 times, and prints the average delay actually observed.
- Do the same for a relative sleep of 100ms. Is it the same? Why/why not?
RTSJ realtime threads
- Write a RTSJ application with three realtime threads to implement
the "drummer" application (above), with a different thread for the
"highhat", "bassdrum" and "snare". Try changing the priorities of the
three threads and see if it affects the print order.
RTSJ asynchronous events
- Write a RTSJ application with an AsyncEvent which is fired by one
thread which repeatedly waits for user input and then fires the event.
A separate AsyncEventHandler should print "Ouch!" each time the event
fires.
- Write a version of the above application which considers the user
input to be sporadic, with minimum interarrival time 1000ms, arrival
time queue length 2, queue overflow behaviour ignore and MIT violation
behaviour save. Check what it does if user input is too fast.
- Change the MIT violation behaviour to ignore. Check what it does when user input is too fast.
- Write a RTSJ version of the "drummer" application (above) using a
PeriodicTimer each for the "highhat", "bassdrum" and "snare". Try
changing the priorities of the three event handlers and see if it
affects the print order.
- Write a RTSJ application which registers a handler for the POSIX
signal USR1 which prints "You cannot kill me so easily". Use the UNIX
"kill" command to send the USR1 signal to it.
RTSJ deadline and cost
- Write a RTSJ application with a periodic realtime thread which
runs every 3000ms, with a deadline of 200ms. Each time it should print
"hit enter now!" and wait for user input (e.g. System.in.read()). If
the user is too slow (as indicated by missing the deadline) then it
should print "too slow".
Resource sharing
- [optional] Write a RTSJ application with three real-time threads which would
exhibit priority inversion. Have it print diagnostic output as threads
acquire and release the lock on the shared resource and check that
priority inheritance is occurring (think carefully about whether you
print before/after acquiring/releasing lock).
- [optional] Write a version of the above application which uses
PriorityCeilingEmulation for the shared resource. Check that setting
the shared monitor's ceiling too low causes a run-time exception. Note:
PriorityCeilingEmulation is an optional part of RTSJ and not supported
by the Sun RTSJ JVM on archer - this program will throw an
UnsupportedOperationException.
- [optional] Try writing an equivalent application in regular Java. Does it
appear to exhibit priority inversion? (Note that the non-strict use of
priorities in regular Java will confuse the results)
Memory areas and parameters
- [optional] Write a realtime threads which has a maximum memory usage of
10000 bytes and a maximum allocation rate of 1000 bytes/second. By
adjusting the rate at which it allocates instances of class
java.lang.Object check if the usage limit and/or allocation rate
are being enforced. How big does a single Java object appear to be?
Partial solution: HelloMemoryParameters.java
- [optional] Write a RTSJ application which uses RawMemoryAccess to access (an
arbitrary) 1000 bytes of shared memory. Create two periodic realtime
threads, one of which repeatedly writes a random byte into the fourth
byte of this memory area, and the other which repeatedly reads the byte
from this location.
- [optional] Write a RTSJ application which uses the same RawMemoryAccess as
the previous exercise and which writes a range of Java integer values
into offset 10 and then reads back and prints the bytes in offsets
10-13 inclusive. Check that the bytes are consistent with the
platform's byte order as indicated by RealtimeSystem.BYTE_ORDER.
Hardware:
- see past exam questions (listed above).
Chris Greenhalgh
Last updated: 2009-11-19