Operating Systems ECE 344 Threads API Ashvin Goel

  • Slides: 12
Download presentation
Operating Systems ECE 344 Threads API Ashvin Goel ECE University of Toronto

Operating Systems ECE 344 Threads API Ashvin Goel ECE University of Toronto

Overview q Thread scheduler o Threads API 2

Overview q Thread scheduler o Threads API 2

Overview of Threads q A thread is an independent stream of instructions q OS

Overview of Threads q A thread is an independent stream of instructions q OS runs one or more threads on one or more CPUs thread_create(T 2) CPU 1 main_loop thread_create(T 1) CPU 2 q T 1 t 1_loop t 2_loop T 2 t 1_loop T 1 thread_switch: suspend + resume main_loop T 2 t 2_loop Implementing threads requires two tasks: Thread scheduling chooses which thread to run & when o Thread switching suspends and resumes chosen thread o 3

Overview of Thread Scheduler q A thread scheduler chooses threads to run in some

Overview of Thread Scheduler q A thread scheduler chooses threads to run in some order based on a scheduling policy o q Example policies: round robin, priority (discussed later) Thread scheduler implements Threads API thread_create, thread_exit, thread_yield, thread_sleep, thread_wakeup, … o Programs call these kernel functions to run thread scheduler o § Use similarly named system calls 4

Thread Status Ready Running Exited Blocked q Scheduler tracks status of each thread q

Thread Status Ready Running Exited Blocked q Scheduler tracks status of each thread q A thread’s status has one value at any one time Ready - thread is ready to run o Running - thread is using CPU o Exited - thread will not run any longer o Blocked - thread is waiting for some event, e. g. , input o 5

Threads API thread_create run Ready current thread_exit Running thread_destroy Exited Blocked q Scheduler functions

Threads API thread_create run Ready current thread_exit Running thread_destroy Exited Blocked q Scheduler functions change thread status q thread_create: thread creates another thread q thread_exit: current thread will not run any longer o q Scheduler chooses another READY thread to run thread_destroy: deallocate any state for thread 6

Threads API thread_create run Ready current thread_exit Running thread_destroy Exited thread_yield thread_wakeup thread_sleep Blocked

Threads API thread_create run Ready current thread_exit Running thread_destroy Exited thread_yield thread_wakeup thread_sleep Blocked q thread_yield: current thread yields CPU o q thread_sleep: current thread blocks for an event o q Scheduler chooses another READY thread to run thread_wakeup: wakeup a thread blocked on event 7

When Does the Scheduler Run a Thread? thread_create run Ready current thread_exit Running Exited

When Does the Scheduler Run a Thread? thread_create run Ready current thread_exit Running Exited thread_yield thread_wakeup thread_sleep Blocked 8

Programming With Threads main() { thread_create(worker. A); thread_create(worker. B); 1 while (1) { 4

Programming With Threads main() { thread_create(worker. A); thread_create(worker. B); 1 while (1) { 4 thread_yield(); } } worker. A() { 2 do_A(); thread_yield(); 5 do_more_A(); thread_exit(); } worker. B() { 3 do_B(); thread_yield(); 6 do_more_B(); thread_exit(); } 9

Preemptive Scheduler q Until now, thread calls scheduler function voluntarily A thread may never

Preemptive Scheduler q Until now, thread calls scheduler function voluntarily A thread may never call thread API functions o How can the scheduler get control? o q Scheduler uses timer interrupt to regain control q Interrupt handler forces call to thread_yield This is called preemptive scheduler o Current thread can be preempted at an arbitrary instruction o current thread run Ready Running Exited preemptive thread_yield 10

Summary q A thread scheduler allows running one or more threads q Scheduler functions

Summary q A thread scheduler allows running one or more threads q Scheduler functions define threads API q Threads API includes functions for: Creating and terminating threads o Yielding CPU to other threads o Synchronizing threads on events o q Next lecture describes scheduler implementation 11

Think Time q q What does thread_yield do? What are the benefits of cooperative

Think Time q q What does thread_yield do? What are the benefits of cooperative versus preemptive scheduling? In the example program using threads, what happens after both worker. A and worker. B finish executing? In the example program using threads, what would happen if a thread does not call thread_exit at the end of the function? 12