CPSC 213 Introduction to Computer Systems
Unit 2b
Virtual Processors
1
Reading
‣Text
• 2ed: 12.3 • 1ed: 13.3
2
The Virtual Processor
‣ Originated with Edsger Dijkstra in the THE Operating System • in The Structure of the “THE” Multiprogramming System, 1968
“I had had extensive experience (dating back to 1958) in making basic software dealing with real-time interrupts, and I knew by bitter experience that as a result of the irreproducibility of the interrupt moments a program error could present itself misleadingly like an occasional machine malfunctioning. As a result I was terribly afraid. Having fears regarding the possibility of debugging, we decided to be as careful as possible and, prevention being better than cure, to try to prevent nasty bugs from entering the construction.
This decision, inspired by fear, is at the bottom of what I regard as the group’s main contribution to the art of system design.”
‣ The Process (what we now call a Thread)
• a single thread of synchronous execution of a program – the illusion of a single system such as the Simple Machine
• can be stopped and restarted
– stopped when waiting for an event (e.g., completion of an I/O operation) – restarted with the event fires
• can co-exist with other processes sharing a single hardware processor
– a scheduler multiplexes processes over processor
– synchronization primitives are used to ensure mutual exclusion and for waiting and signalling
3
Thread
‣ An abstraction for execution
• looks to programmer like a sequential flow of execution, a private CPU
• it can be stopped and started, it is sometimes running and sometimes not • the physical CPU thus now multiplexes multiple threads at different times
‣ Creating and starting a thread
• like an asynchronous procedure call
• starts a new thread of control to execute a procedure
‣ Stopping and re-starting a thread
• stopping a thread is called blocking
• a blocked thread can be re-started (i.e., unblocked)
‣ Joining with a thread
• blocks the calling thread until a target thread completes
foo
bar zot
join
• returns the return value of the target-thread’s starting procedure
bat
• turns thread create back into a synchronous procedure call
4
Revisiting the Disk read
‣ A program that reads a block from disk • want the disk read to be synchronous
read (buf, siz, blkNo); // read siz bytes at blkNo into buf nowHaveBlock (buf, siz); // now do something with the block
• but, it is asynchronous so we have this
asyncRead (buf, siz, blkNo, nowHaveBlock); doSomethingElse ();
‣ As a timeline
• two processors
• two separate computations
asyncRead
CPU
disk controller
do something else while waiting
nowHaveBlock
perform disk read
5
Synchronous Disk Read using Threads
x block √ unblock asyncRead do something else while waiting nowHaveBlock
CPU
‣ Create two threads that CPU runs, one at a time • one for disk read
• one for doSomethingElse
‣ Illusion of synchrony
• disk read blocks while waiting for disk to complete • CPU runs other thead(s) while first thread is blocked • disk interrupt restarts the blocked read
asyncRead (buf, siz, blkNo); waitForInterrupt ();
nowHaveBlock (buf, siz);
interruptHandler() { signalBlockedThread();
}
6
Threads in Java
‣ Create a procedure that can be executed by a thread • build a class that implements the Runnable interface
class ZotRunnable implements Runnable {
Integer result, arg;
ZotRunnable (Integer anArg) {
arg = arg; }
public void run() { result = zot (arg);
} }
‣ Create a thread to execute the procedure and start it
ZotRunnable zot = new ZotRunnable (0);
Thread t = new Thread (zot);
t.start();
7
‣ Later join with thread to get zot’s return value
Integer result;
try {
t.join();
result = zot.result;
} catch (InterruptedException ie) {
result = null;
}
‣ So that the entire calling sequence is
foo();
ZotRunnable zot = new ZotRunnable (0);
Thread t = new Thread (zot);
t.start();
bar();
Integer result = null;
try { join
t.join();
result = zot.result;
} catch (InterruptedException ie) { }
bat();
foo
bar zot
bat
8
Executor Services in Java
‣ Create an Executor Service once at beginning of program • to manage asynchronous call’s in a pool of threads (here limited to 2)
ExecutorService ex = new ScheduledThreadPoolExecutor (2);
‣ Create a procedure that can be submitted to this Service
• build a class that implements the Callable interface
class ZotCallable implements Callable
Integer arg;
ZotCallable (Integer anArg) {
arg = anArg; }
public Integer call () { return zot (arg);
} }
9
‣ Schedule execution of the procedure
• declare a Future variable to store the procedure’s result
• submit procedure’s callable object to the Executor Service
Future
Integer result = null;
try {
result = resultFuture.get(); // join } catch (InterruptedException ie) {
} catch (ExecutionException ee) {}
‣ Shutdown Executor Service before program terminates • program keeps running after main returns until executor is shutdown
ex.shutdown();
10
‣ So that the entire calling sequence is
ExecutorService ex = new ScheduledThreadPoolExecutor (2); foo();
Future
Integer result = null;
try {
result = resultFuture.get(); // join } catch (InterruptedException ie) {
} catch (ExecutionException ee) {} bat();
ex.shutdown ();
foo
bar zot
join
bat
11
Comparing Java’s Alternatives
‣ Focusing on asynchronous call
ZotRunnable zot = new ZotRunnable (0);
Thread t = new Thread (zot);
t.start();
Integer result = null;
try {
t.join();
result = zot.result;
} catch (InterruptedException ie) {
}
Future
Integer result = null;
try {
result = resultFuture.get(); // join
} catch (InterruptedException ie) {
} catch (ExecutionException ee) {} // if zot() threw an exception
‣ Advantages of Executor Service
• better management of result returned or exception thrown by asynchronous call
• precise thread management abstracted from application code
12
UThread
‣ The UThread Interface file (uthread.h) struct uthread_TCB;
typedef struct uthread_TCB uthread_t;
void uthread_init ();
uthread_t* uthread_create (void* (*star_proc)(void*), void* start_arg);
void uthread_yield ();
void* uthread_join (uthread_t* thread);
void uthread_detach (uthread_t* thread);
uthread_t* uthread_self ();
: A Simple Thread System for C
‣ Explained
• uthread_t
• uthread_init
• uthread_create • uthread_yield • uthread_join
• uthread_detach • uthread_self
is the datatype of a thread control block
is called once to initialize the thread system
create and start a thread to run specified procedure temporarily stop current thread if other threads waiting join calling thread with specified other thread
indicate thread no thread will join specified thread
a pointer to the TCB of the current thread
13
Example Program using UThreads
void ping () {
int i;
for (i=0; i<100; i++) {
printf ("ping %d\n",i); fflush (stdout);
uthread_yield ();
}
}
void pong () {
int i;
for (i=0; i<100; i++) {
printf ("pong %d\n",i); fflush (stdout);
uthread_yield ();
}
}
void ping_pong () {
uthread_init (1);
uthread_create (ping, 0);
uthread_create (pong, 0);
while (1)
uthread_yield ();
}
14
Implement Threads: Some Questions
‣ The key new thing is blocking and unblocking • what does it mean to stop a thread?
• what happens to the thread?
• what happens to the physical processor?
‣ What data structures do we need
‣ What basic operations are required
15
Implementing UThreads: Data Structures
‣ Thread State
• running: register file and runtime stack
• stopped: Thread Control Block and runtime stack
‣ Thread-Control Block (TCB)
• thread status: (NASCENT, RUNNING, RUNNABLE, BLOCKED, or DEAD)
• pointers to thread’s stack base and top of its stack
• scheduling parameters such as priority, quantum, pre-emptability etc.
‣ Ready Queue
• list of TCB’s of all RUNNABLE threads
‣ One or more Blocked Queues • list of TCB’s of BLOCKED threads
16
Thread Data Structure Diagram
Ready Queue
Thread Control Blocks
TCBa
RUNNING
TCBb
RUNNABLE
TCBc
RUNNABLE
Stacks
r5
17
Thread Status DFA
Schedule
Create
Yield
Nascent
Running
Blocked
Runnable
Schedule
Block
Complete
Unblock
Freed
Dead
Join or Detach
18
Implementing Threads: Thread Switch
‣ Goal
• implement a procedure switch (Ta, Tb) that stops Ta and starts Tb
• Ta calls switch, but it returns to Tb • example ...
‣ Requires
• saving Ta’s processor state and setting processor state to Tb’s saved state • state is just registers and registers can be saved and restored to/from stack • thread-control block has pointer to top of stack for each thread
‣ Implementation
• save all registers to stack
• save stack pointer to Ta’s TCB
• set stack pointer to stack pointer in Tb’s TCB • restore registers from stack
19
Thread Switch
Thread Control Blocks
TCBa
TCBb
Stacks
Register File
1. Save all registers to A’s stack
2. Save stack top in A’s TCB
3. Restore B’s stack top to stack-pointer register 4. Restore registers from B’s stack
20
Example Code for Thread Switch
asm volatile ("pushq %%rbx\n\t"
"pushq %%rcx\n\t"
"pushq %%rdx\n\t"
"pushq %%rsi\n\t"
"pushq %%rdi\n\t"
"pushq %%rbp\n\t"
"pushq %%r8\n\t"
"pushq %%r9\n\t"
"pushq %%r10\n\t"
"pushq %%r11\n\t"
"pushq %%r12\n\t"
"pushq %%r13\n\t"
"pushq %%r14\n\t"
"pushq %%r15\n\t"
"pushfq\n\t"
"movq %%rsp, %0\n\t"
"movq %1, %%rsp\n\t"
"popfq\n\t"
"popq %%r15\n\t"
"popq %%r14\n\t"
"popq %%r13\n\t"
"popq %%r12\n\t"
"popq %%r11\n\t"
"popq %%r10\n\t"
"popq %%r9\n\t"
"popq %%r8\n\t"
"popq %%rbp\n\t"
"popq %%rdi\n\t"
"popq %%rsi\n\t"
"popq %%rdx\n\t"
"popq %%rcx\n\t"
"popq %%rbx\n\t"
: "=m" (*from_sp_p)
: "ra" (to_sp));
from_tcp->saved_sp ← r[sp]
r[sp] ← to_tcp->saved_sp
21
Implementing Thread Yield
‣ Thread Yield
• gets next runnable thread from ready queue (if any)
• puts current thread on ready queue • switches to next thread
‣ Example Code
void uthread_yield () {
uthread_t* to_thread = dequeue (&ready_queue);
uthread_t* from_thread = uthread_cur_thread ();
if (to_thread) {
from_thread->state = TS_RUNABLE;
enqueue (&ready_queue, from_thread);
uthread_switch (to_thread);
} }
22
Question
‣ The uthread_switch procedure saves the from thread’s registers to the stack, switches to the to thread’s stack pointer and restores its registers from the stack, but what does it do with the program counter?
• (A) It saves the from thread’s program counter to the stack and restores the to-thread’s program counter from the stack.
• (B) It saves the from thread’s program counter to its thread control block.
• (C) It does not need to change the program counter because the from and to threads PCs are already saved on the stack before switch is called.
• (D) It jumps to the to thread’s PC value.
• (E) I am not sure.
23
Multiple Processors
‣ Processors are
• the physical / hardware resource that runs threads • a system can have more than one
‣ Uni-Processor System
• a single processor runs all threads
• no two threads run at the same time
‣ Multi-Processor System
• multiple processors run the threads
• two threads can be running at the same time
‣ More about this later, but we have a problem now …
• how do we compute the value of cur_thread, the current threads TCB?
• we need this to yield the thread, for example, to place it on ready queue • but, can’t use a global variable
24
Thread Private Data
‣ Threads introduce need for another type of variable • a thread-private variable is a global variable private to a thread • like a local variable is private to a procedure activation
‣ For example
• cur_thread, the address of the current thread’s activation frame • Its a global variable to thread, but every thread has its own copy
‣ Implementing Thread Private Data • store Thread-private data in TCB
• store pointer to TCB at top of every stack
• compute current stack top from stack pointer – require that stack top address is aligned to stack size
– stack top = r5 & ~(Stack Size – 1)
25
Thread Private Data
Ready Queue
Thread Control Blocks
TCBa
RUNNING
TCBb
RUNNABLE
TCBc
RUNNABLE
Stacks
r5
Top of stack points to TCB where Thread-private data is stored
26
Thread Scheduling
‣ Thread Scheduling is
• the process of deciding when threads should run
• when there are more runnable threads than processors • involves a policy and a mechanism
‣ Thread Scheduling Policy
• is the set of rules that determines which threads should be running
‣ Things you might consider when setting scheduling policy
• do some threads have higher priority than others? • should threads get fair access to the processor? • should threads be guaranteed to make progress? • do some operations have deadlines?
• should one thread be able to pre-empt another?
• if threads can be pre-empted, are there times when this shouldn’t happen?
27
Priority, Round Robin Scheduling Policy
‣ Priority
• is a number assigned to each thread • thread with highest priority goes first
‣ When choosing the next thread to run
• run the highest priority runnable thread
• when threads are same priority, run thread that has waited the longest
‣ Implementing Thread Mechanism
• organize Ready Queue as a priority queue
– highest priority first
– FIFO (first-in-first-out) among threads of equal priority
• priority queue: first-in-first out among equal-priority threads
‣ Benefits
‣ Drawbacks and mitigation
28
Preemption
‣ Preemption occurs when
• a “yield” is forced upon the current running thread
• current thread is stoped to allow another thread to run
‣ Priority-based preemption
• when a thread is made runnable (e.g., created or unblocked)
• if it is higher priority than current-running thread, it preempts that thread
‣ Quantum-based preemption
• each thread is assigned a runtime “quantum” • thread is preempted at the end of its quantum
‣ How long should quantum be? • disadvantage of too short?
• disadvantage of too long?
• typical value is around 100 ms
‣ How is quantum-based preemption implemented?
29
Implementing Quantum Preemption
‣ Timer Device
• an I/O controller connected to a clock • interrupts processor at regular intervals
‣ Timer Interrupt Handler
• compares the running time of current thread to its quantum • preempts it if quantum has expired
‣ How is running thread preempted
30
Real-Time Scheduling
‣ Problem with round-robin, preemptive, priority scheduling
• some applications require threads to run at a certain time or certain interval • but, what does round-robin guarantee and not guarantee?
‣ Real-time Scheduling
• hard realtime – e.g., for controlling or monitoring devices
– thread is guaranteed a regular timeslot and is given a time budget
– thread can not exceed its time budget
– thread will not be “admitted” to the run in the first place, unless its schedule can be guaranteed
• soft realtime – e.g., for media streaming
– option 1: over-provision and use round-robin
– option 2: thread expresses its scheduling needs, scheduler tries its best, but no guarantee
31
Summary
‣ Thread
• synchronous “thread” of control in a program
• virtual processor that can be stopped and started
• threads are executed by real processor one at a time
‣ Threads hide asynchrony
• by stopping to wait for interrupt/event, but freeing CPU to do other things
‣ Thread state
• when running: stack and machine registers (register file etc.)
• when stopped: Thread Control Block stores stack pointer, stack stores state
‣ Round-robin, preemptive, priority thread scheduling • lower priority thread preempted by higher
• thread preempted when its quantum expires
• equal-priority threads get fair share of processor, in round-robin fashion
32