Concurrency in Java and Real-Time Operating Systems: Languages For Embedded Systems
Concurrency in Java and Real-Time Operating Systems: Languages For Embedded Systems
Columbia University
March 2009
The Java Language
24 iload_1 // Push a
25 ireturn // Return a
Aside: The JVM
Advantages:
Disadvantages:
Embedded Systems?
Concurrency
Concurrency in Java
$ java Clock
Sat Sep 14 13:04:27 EDT 2002
Sat Sep 14 13:04:29 EDT 2002
Sat Sep 14 13:04:30 EDT 2002
Sat Sep 14 13:04:31 EDT 2002
Synchronization
Motivation for Synchronization
class ScoreKeeper {
int _score = 0;
void score(int v) {
int tmp = _score;
tmp += v;
_score = tmp;
}
}
int i;
double d;
Thread 1 Thread 2
i = 10; i = 20;
d = 10.0; d = 20.0;
synchronized(mycount) {
mycount.count();
}
class AtomicCounter {
synchronized(Foo) { synchronized(Bar) {
synchronized(Bar) { synchronized(Foo) {
// Asking for trouble // Asking for trouble
} }
} }
while (!condition) {}
class Mailbox {}
What happens if the book is delivered before the main thread starts
waiting?
Answer: the “notify” instruction does not wake up any threads.
Later, the main thread starts to wait and will never be awakened.
As if Alice came to my office to tell me when I was not there.
Harder Problem
while (!condition)
wait();
The multi-book problem
class Mailbox {
int book;
Mailbox() { book = -1; }
void receive_book(int b) { book = b; }
int which_book() { return book; }
}
The Delivery class also tells the mailbox which book it got.
$ java MultiOrder
Ordering book 1
Ordering book 2
Book 1 Arrived
Book 2 Arrived
A Better Solution
Last solution relied on threads to synchronize their own access to
the Mailbox. Mailbox should be doing this itself:
class Mailbox {
int book;
Mailbox() { book = -1; }
$ java MultiOrder2
Ordering book 1
Ordering book 2
Book 1 Arrived
Book 2 Arrived
Building a Blocking Buffer
interface OnePlace {
public void write(Object o);
public Object read();
}
Blocking Buffer: Write Method
try {
while (o != null)
wait(); // Block while buffer is full
} catch (InterruptedException e) {}
try {
while (o == null)
wait(); // Block while buffer is empty
} catch (InterruptedException e) {}
Thread Priorities
Priorities
Implementing Threads
Processes and Threads
Thread Miscellany
Thread-Related Methods
born
Not always
Simplest approach: cyclic executive
for (;;) {
/* do part of task 1 */
/* do part of task 2 */
/* do part of task 3 */
}
Cyclic Executive
Advantages
Simple implementation
Low overhead
Very predictable
Disadvantages
Can’t handle sporadic events
Everything must operate in lockstep
Code must be scheduled manually
Interrupts
Ï Communication channels
Ï Transient events
A cheap alternative
Non-preemptive
Processes responsible for relinquishing control
Examples: Original Windows, Macintosh
A process had to periodically call get_next_event() to let other
processes proceed
Drawbacks:
Programmer had to ensure this was called frequently
An errant program would lock up the whole system
Alternative: preemptive multitasking
Concurrency Provided by OS
Basic philosophy:
An Alternative:
Certain high-end chips (e.g., Intel’s Xeon) now contain two or three
contexts. Can switch among them “instantly.”
Idea: while one process blocks on memory, run another.
Part VII
Real-Time Concurrency
Real-Time Is Not Fair
| {z }
Period
0 1 2 3 4 5 6 7 8
p = (2, 8, 8)
Example: Fly-by-wire Avionics
B B B
C C
B A B C A B A B
Solutions to equal priorities
Rate-Monotonic Scheduling
Rate-Monotonic Scheduling
E.g.,
Period Priority
10 1 (high)
12 2
15 3
20 4 (low)
Key RMS Result
2 2 2
1 2 1 2
p 2 misses a deadline
X ci
U=
i pi
U < n(21/n − 1)
n Bound for U
1 100% Trivial: one process
2 83% Two process case
3 78%
4 76%
..
.
∞ 69% Asymptotic bound
When Is There an RMS Schedule?
Asymptotic result:
2 2 2
1 2 1 2
Priority Inversion
Priority Inversion
2 2
2 2
3 3
Priority Inversion