Escolar Documentos
Profissional Documentos
Cultura Documentos
[RTCS Ch. 3]
mechanical techniques
Implementation Alternatives
Context switches
Internal representation
Generalpurpose processors
Specialpurpose processors
Microcontrollers
Digital Signal Processors (DSP)
Performance,
Power
efficiency
Flexibility
Programmable Hardware
Small Microcontrollers
Large Microcontrollers
External memory
Processor:
Processor:
(Periodic) timers
Periodic controllers implemented in interrupt handlers
associated with periodic timers
Only a limited number of timers
Process
Concurrent Programming
A
B
C
Logical concurrency
Time
Process
Context
Switch
Approaches
Time
8
Memory models
Processes may have
Processes vs threads
10
Coroutines
A mechanism for expressing concurrent execution.
Process 2
Coroutine A;
Process 1
Threads
begin
.
1
.
Resume(B);
.
5
.
Resume(B);
.
9
.
end A;
Process 3
Scheduler
11
Coroutine B;
begin
.
3
.
4
Resume(A);
.
7
.
8 Resume(A);
.
.
end B;
12
Running
Context Switches
A context switch takes place when the system changes the
running process (thread).
Ready
waiting
event
Blocked
14
Priorities
preemptive scheduling
the context switch takes place immediately (the running
process is preempted)
Most RTOS and real-time languages (e.g. STORK and
Java)
nonpreemptive scheduling
Running
the running process continues until it voluntarily releases the CPU, then the context switch takes place
Process Process Process
Process
Higher Priority
15
Assigning priorities
the running points continues until it reaches an preemption point, then the context switch takes place
Linux (no preemptions in kernel mode)
16
Dynamic priorities
Non-trivial
Global knowledge
Two ways:
ad hoc rules
important time requirements high priority
time consuming computations low priority
conicts, no guarantees
scheduling theory
Often easier to assign deadline than to assign priority
18
Process Representation
VAR x, z: INTEGER;
y: POINTER TO INTEGER;
PROCEDURE Demo(a : INTEGER, b : INTEGER): INTEGER;
VAR Result: INTEGER;
BEGIN
Result := a * b - b*b;
RETURN Result;
END Demo;
a stack
local variables of the process
arguments and local variables of procedures called by the
process
when suspended: storage place for the values of programmable registers and program counter
BEGIN
x := 2;
NEW(y);
y^ := 7;
z := Demo(x,y^);
Write(z);
DISPOSE(y);
End Main.
19
Growing stack
direction
Stack
20
Heap end
Stack
b
7
x 2
Data area
Parameters
Heap start
y
z
freelist
...
New(y);
y^ := 7;
...
Return value
Return address
Local variables
Code for
Module Main
Program area
Result
Growing stack
direction
Heap
Stack
Pointer
Program counter
Result
21
22
Main Stack
.
Main stack end
MODULE
Main;
STORK
STORK
Heap end
ProcessB
(Suspended)
ProcessB Stack
Heap
Stack pointer
ProcessA
(Running)
ProcessA Stack
Heap start
Global
Data area
Code for
Module Main
Program counter
Program area
23
24
RealTime Kernel
Process Record
Process Record
ProcessRec
STORK
TYPE
PROCESS = ADDRESS;
ProcessRef = POINTER TO ProcessRec;
Queue = POINTER TO QueueRec;
QueueRec
STORK
head: succ
pred
...
Modula2
Process Record
...
stackpointer
...
procv
timer
stack
stackSize
QueueRec = RECORD
succ, pred
: ProcessRef;
priority
: CARDINAL;
nextTime
: Time;
END;
Running
QueueRec
ReadyQueue
succ
pred
ProcessRec = RECORD
head
: QueueRec;
procv
: PROCESS;
timer
: CARDINAL:
stack
: ADDRESS;
stackSize
: CARDINAL;
END;
Head: succ
pred
...
procv
timer
stack
stackSize
QueueRec
(List Head)
ProcessRec
QueueRec
Head: succ
pred
...
procv
timer
stack
stackSize
ProcessRec
25
26
STORK
STORK
head.succ
ProcessRec
for ProcessB
head.succ
ProcessRec
for ProcessB
head.pred
procv.stackpointer
stack
ProcessB
(Suspended)
Heap
ProcessRec
for ProcessA
stack
ProcessB
(Suspended)
ProcessB Stack
Heap
head.succ
head.pred
ProcessRec
for ProcessA
procv.stackpointer
List head
Global
Data area
head.pred
ProcessA Stack
ProcessA
(Running)
Stack
pointer
succ
List head
pred
ReadyQueue
Global
Data area
Running
...
Code for
Module Main
head.succ
stack
ProcessA Stack
Program area
ProcessB Stack
procv.stackpointer
stack
ProcessA
(Running)
head.pred
procv.stackpointer
Stack
pointer
succ
pred
ReadyQueue
Running
...
Program area
Code for
Module Main
Program
counter
Program
counter
27
28
STORK
STORK
head.succ
ProcessRec
for ProcessB
head.succ
ProcessRec
for ProcessB
head.pred
procv.stackpointer
stack
ProcessB
(Suspended)
Heap
ProcessRec
for ProcessA
stack
ProcessB
(Suspended)
ProcessB Stack
Heap
head.succ
head.pred
ProcessRec
for ProcessA
procv.stackpointer
List head
Global
Data area
Program area
head.succ
head.pred
stack
ProcessA Stack
ProcessA Stack
ProcessA
(Running)
Stack
pointer
succ
List head
pred
ReadyQueue
Global
Data area
Running
...
Code for
Module Main
ProcessB Stack
procv.stackpointer
stack
ProcessA
(Running)
head.pred
procv.stackpointer
Program area
Program
counter
29
Stack
pointer
succ
pred
ReadyQueue
Running
...
Code for
Module Main
Program
counter
30
STORK
STORK
head.succ
ProcessRec
for ProcessB
head.succ
ProcessRec
for ProcessB
head.pred
procv.stackpointer
head.pred
procv.stackpointer
stack
ProcessB
(Suspended)
Heap
stack
ProcessB
(Suspended)
ProcessB Stack
Heap
head.succ
ProcessRec
for ProcessA
head.pred
head.pred
procv.stackpointer
stack
stack
ProcessA
(Running)
succ
Stack
pointer
succ
List head
pred
ReadyQueue
pred
Global
Data area
Running
...
Program area
ProcessA Stack
ProcessA
(Running)
Stack
pointer
Global
Data area
head.succ
ProcessRec
for ProcessA
procv.stackpointer
ProcessA Stack
List head
ProcessB Stack
ReadyQueue
Running
...
Program area
Code for
Module Main
Code for
Module Main
Program
counter
Program
counter
31
32
Schedule
Context Switching
STORK
STORK Schedule;
PROCEDURE
VAR
oldRunning
oldDisable
: ProcessRef;
: InterruptMask;
BEGIN
oldDisable := Disable(); (* Disable interrupts *)
IF ReadyQueue^.succ <> Running THEN
oldRunning := Running;
Running := ReadyQueue^.succ;
TRANSFER(oldRunning^.procv,Running^.procv);
END;
Reenable(oldDisable);
END Schedule;
interrupts disabled
the actual context switch takes place in the Modula-2 primitive
TRANSFER
the Modula-2 process record pointer as argument
Process A
.
TRANSFER
is entered in the context of one process and left in
STORK
the context of another process.
STORK
Process B
Context switch
Process C
34
Schedule
Disable
enter TRANSFER;
exit TRANSFER;
Reenable;
END Schedule;
Process A
Process B
Context switch
exit TRANSFER;
Reenable;
END Schedule;
Schedule
Disable;
enter TRANSFER;
Schedule
Disable
enter TRANSFER;
Schedule
Disable;
enter TRANSFER;
Context switch
exit TRANSFER;
Reenable;
END Schedule;
Context switch
exit TRANSFER;
Reenable;
END Schedule;
Context switch
exit TRANSFER;
Reenable;
END Schedule;
Context switch
exit TRANSFER;
Reenable;
END Schedule;
35
Schedule
Disable;
enter TRANSFER;
36
Inside TRANSFER
STORK
STORK
Saving
Switching
Restoring
Old context
New Context
38
Java in Real-Time
Three possibilities:
1. Ahead-of-time (AOT) Compilation
Java or Java bytecode to native code
Java or Java bytecode to intermediate language (C)
2. Java Virtual Machine (JVM)
as a process in an existing OS
green thread model the threads are handled
internally by the JVM
native thread model the Java threads are
mapped onto the threads of the OS
executing on an empty machine
green thread model
3. direct hardware support, e.g., through micro-code
Compiled Java:
works essentially in the same way as presented for
STORK
JVM with native-thread model:
each native thread consists of a JVM that interprets the
Java code of the corresponding Java thread (conceptually,
but not the way it is implemented)
similar to what has been presented before
39
40
Thread creation
JVM with green-thread model:
threads are abstractions inside the JVM
the JVM holds within the thread objects all information
related to the threads
the threads stack
the current instruction of the thread
bookkeeping information
Two ways:
By dening a class that extends (inherits from) the Thread
class
By dening a class that implements the runnable interface
(denes a run method)
The run method contains the code that the thread executes.
The thread is started by a call to the start method.
A Java thread active object, as opposed to a passive object.
42
This way is used when the active object needs to extend some
other class than Thread.
public class MyThread extends Thread {
public void run() {
// Code to be executed
}
}
...
MyClass m = new MyClass();
Thread t = new Thread(m);
t.start();
...
...
MyThread m = new MyThread();
m.start();
...
Drawback: non-static Thread methods are not directly accessible inside the run method (example later on)
44
43
public MyThread(MyClass m) {
owner = m;
}
public void run() {
// Code to be executed
}
}
public class MyClass extends MySuperClass {
MyThread t;
public MyClass() {
t = new MyThread();
}
public void start() {
t.start();
}
public MyClass() {
t = new MyThread(this);
}
public void start() {
t.start();
}
45
46
Thread Priorities
A newly created thread inherits the priority of the creating
thread.
Thread termination
A thread terminates when its run method terminates.
48