Upload
timothy-hodge
View
218
Download
1
Embed Size (px)
Citation preview
Operating SystemsECE344
Ashvin GoelECE
University of Toronto
Thread Scheduling
2
Overview
Thread scheduling
Thread and context switch
Thread creation and termination
Kernel threads vs. user threads
3
Overview of Threads
A thread is an independent stream of instructionso Threads are multiplexed onto a CPU
Requires o Choosing which thread to run & wheno Switching threads
Next we will see how this is done
T1 T2
switc
h
switch
switc
h
suspendresume
suspendresume
4
Thread Scheduling
Thread scheduling means running threads in some order
A scheduler decides which thread to run based ono Thread stateo Various scheduling policies (discussed later)
A thread has several stateso Running - thread is using CPUo Blocked - thread is waiting for some event, e.g., inputo Ready - thread is ready to run (neither Running or Blocked)o Exited - thread has exited but not been destroyed
Threads invoke scheduler functions to change states
5
Thread Scheduling Functions
o Thread_yield - Current thread yields CPU State change: Running → Ready, e.g., ? Run another thread: Ready → Running
o Thread_sleep - Current thread blocks for an event State change: Running → Blocked, e.g., send to full buffer Run another thread : Ready → Running
o Thread_wakeup – Wakeup another thread blocked on event State change: Blocked → Ready, e.g., receive from non-empty
buffer
thread_wakeup
thread_sleep thread_yield
Running
ReadyBlocked
Exitedthread_exit
thread_destroy
thread_create
run
6
Preemptive Scheduling
A thread may never call thread_yield or thread_sleep A thread makes these calls voluntarily, when convenient
Scheduler uses timer interrupt to regain control
Interrupt handler calls yield on behalf of current thread
This is called preemptive schedulingo Current thread is preempted at an arbitrary instruction
thread_wakeup
thread_sleep preemptive thread_yield
Running
ReadyBlocked
Exitedthread_exit
thread_destroy
thread_create
run
7
Scheduler Implementation
Scheduler maintains thread structures in queueso Ready queue (also ready list) has threads in Ready state
Scheduler calls dequeue to run threado Wait queue has threads in Blocked state
Typically, a separate wait queue is used for each type of event– E.g., disk, console, timer, network, etc.
o Exited queue has threads in Exited state
Scheduling functions move threads between queues E.g., wakeup moves thread from wait queue to ready queue
thread_id = 5CPU_regsstate = ready…
thread_id = 8CPU_regsstate = ready…
thread_id = 3CPU_regsstate = ready…
Null
Ready queuehead
8
Thread Switching
Thread abstraction requires switching (suspending and resuming) threads
This implementation is tricky, often mysterious …o Let’s see how it works for thread_yield, thread_sleep is similar
// “current” is thread struct for currently running threadthread_yield(){ … // enqueue current in ready queue // choose next thread to run, remove it from ready queue next = choose_next_thread(); // switch to next thread thread_switch(current, next); …}
9
Thread Switching
Restore resumes running the next thread at the point where the next thread’s state was previously savedo Could this cause a problem?
Process switch requires changing address space as well, called a context switcho context switch = thread switch + address space switch
// call is invoked by one thread but returns in another!thread_switch(current, next) { // save current thread’s CPU state save_processor_state(current->cpu_regs); … // restore next thread’s CPU state restore_processor_state(next->cpu_regs);}
10
Thread Creation and Termination
API functionso thread_create: create a new threado thread_exit: terminate current threado thread_destroy: deallocate state for thread
thread_createo A thread creates a new thread by calling thread_createo Allocates thread struct, allocates stack memory, init PC, SPo Sets thread state to READY, and adds it to run queueo Issue 1: If a thread creates a new thread, then who creates
the first thread, and how?o Issue 2: We have seen how a thread resumes running, but
how does a new thread start running?
11
Thread Creation and Termination
thread_exito Current thread invokes this call to terminate itselfo On exit, thread is suspended but not resumedo Call does not destroy the thread structure and thread stack
Current code is running on that stack, so destroying it could cause serious corruption
o Switch to another thread
thread_destroyo Invoked by thread that starts running after the exited threado Destroys thread structure and stack of the exited thread
12
Kernel vs. User Threads
We have discussed how a thread scheduler works
The thread scheduler is implemented in the kernelo The corresponding threads are called kernel threadso Kernel threads virtualize CPU
It can also be implemented by a user programo These threads are called user threadso User threads virtualize a kernel thread
13
Kernel vs. User Threads
Kernel Threads
Program
User Threads
Program
ThreadUser-levelscheduler
Kernel-levelscheduler
14
Kernel vs. User Threads
Kernel Threads User Threads
Switching cost Kernel switches threads, requiring running kernel code, more expensive
Program switches threads, time closer to procedure call, less expensive
Scheduling policy System has fixed policies User can define custom policy
Blocking system calls
When system call blocks (in kernel), kernel switches to another thread
When system call blocks, all user threads (associated with the corresponding kernel thread) block, so overlap of I/O and computation is not possible
Multiprocessors Different kernel threads can use multiple CPUs concurrently
Different user threads (associated with a given kernel thread) cannot use multiple CPUs concurrently
15
Summary
The core of an operating system is a thread schedulero It implements the thread abstraction, and thread switchingo Creating and destroying threads (like people) introduces
some complications
Just like the OS, a user program can also implement a thread scheduler
16
Think Time
What steps are involved in switching the CPU from one process to another?
What is the difference between a context switch and a mode switch?
What is the difference between a thread switch and a context switch?