thread a new abstraction why threads for concurrency
play

Thread: a new abstraction Why threads? for concurrency A - PowerPoint PPT Presentation

Rethinking the process abstraction The Process, as we know it, serves two key purposes in the OS: Threads It defines the granularity at which the OS offers isolation App 1 An abstraction for concurrency each process defines an address space


  1. Rethinking the process abstraction The Process, as we know it, serves two key purposes in the OS: Threads It defines the granularity at which the OS offers isolation App 1 An abstraction for concurrency each process defines an address space that identifies what can be touched by the program OS It defines the granularity at which Hardware the OS offers scheduling and can express concurrency each process defines a stream of instructions executed sequentially � 1 � 2 Thread: a new abstraction Why threads? for concurrency A single-execution stream of instructions that represents To express a natural program structure a separately schedulable task updating the screen, fetching new data, receiving user OS can run, suspend, resume a thread at any time input — different tasks within the same address space bound to a process (lives in an address space) To exploit multiple processors Finite Progress Axiom: execution proceeds at some different threads may be mapped to distinct processors unspecified, non-zero speed To maintain responsiveness Virtualizes the processor splitting commands, spawn threads to do work in the programs run on machine with an infinite number of background processors (hint: not true) Masking long latency of I/O devices Allows to specify tasks that should be run concurrently... ...and lets us code each task sequentially do useful work while waiting � 3 � 4

  2. How can they help? How can they help? Consider the following code segment: Consider a Web server get network message from client for (k = 0; k < n; k++) get URL data from disk a[k] = b[k] × c[k] + d[k] × e[k] compose response Is there a missed opportunity here? send response thread_create(T1, fn, 0, n/2) thread_create(T2, fn, n/2, n) fn(l,m) { for (k = l; k < m; k++) a[k] = b[k] × c[k] + d[k] × e[k] } � 5 � 6 Overlapping I/O & How can they help? Computation Request 1 Request 2 Consider a Web server Thread 1 Thread 2 get network message Create a number of threads, and for each do (URL) from client get network message get network message from client get URL from disk (URL) from client get URL data from disk get URL from disk (disk access latency) compose response (disk access latency) send response send data over network send data over network Time What did we gain? Total time is less than Request 1 + Request 2 � 8 � 7

  3. All you need is Love All you need is Love (and a stack) (and a stack) Process address space Process address space All threads within a process share All threads within a process share Shared Libraries Shared Libraries Heap Heap heap heap global/static data global/static data libraries libraries Stack – Thread2 Each thread has separate Each thread has separate program counter program counter Stack Stack – Thread1 registers registers Initialized data Initialized data stack stack Code Code � 9 � 10 Implementing the thread Processes vs. Threads: abstraction: the state Parallel lives Per-Thread Shared Per-Thread State State State Thread Control Block Thread Control Block Heap (TCB) (TCB) Stack pointer Stack pointer Other Registers (PC, etc) Other Registers (PC, etc) Global Thread metadata (ID, priority, etc) Thread metadata (ID, priority, etc) Variables Stack Stack Stack frame Stack frame Code Stack frame Stack frame Note: No protection enforced at the thread level! � 11 � 12

  4. Processes vs. Threads: A simple API Parallel lives Processes Threads Syscall Description No data segment or heap Have data/code/heap and other void Creates a new thread in thread, which will segments Needs to live in a process thread_create execute function func with arguments arg. More than one can be in a (thread, func, arg) Include at least one thread process. First calls main. Calling thread gives up processor. Scheduler can If a process dies, its resources are void If a thread dies, its stack is thread_yield() resume running this thread at any time reclaimed and its threads die reclaimed Interprocess communication via OS Wait for thread to finish, then return the value Inter-thread communication via int and data copying memory thread passed to thread_exit. thread_join May be called only once for each thread. Have own stack and registers, but (thread) Have own address space, isolated no isolation from other threads in from other processes’ void the same process Finish caller; store ret in caller’ s TCB and wake thread_exit Each process can run on a up any thread that invoked thread_join(caller). Each thread can run on a (ret) different processor different processor Expensive creation and context Inexpensive creation and context switch switch � 14 � 13 Multithreaded Processing Multithreaded Processing Paradigms Paradigms User Space User Space Dispatcher Workers Web page cache Requests Kernel Kernel Dispatcher/Workers Specialists � 15 � 16

  5. Multithreaded Processing Threads Paradigms considered harmful Creating a thread or process for User Space each unit of work (e.g., user Well conditioned request) is dangerous Not well conditioned High overhead to create & delete thread/process Can exhaust CPU & memory Throughput resource Request Thread/process pool controls Kernel resource use Allows service to be well conditioned Pipelining output rate scales to input rate Load excessive demand does not degrade pipeline throughput � 17 � 18 Threads Life Cycle Threads Life Cycle Threads (just like processes) go through a sequence of Threads (just like processes) go through a sequence of Init , Ready , Running , Waiting , and Finished states Init , Ready , Running , Waiting , and Finished states Thread creation (e.g. thread_create() ) Ready Running Ready Running Init Finished Init Finished TCB: being created Waiting Waiting Registers: in TCB � 19 � 20

  6. Threads Life Cycle Threads Life Cycle Threads (just like processes) go through a sequence of Threads (just like processes) go through a sequence of Init , Ready , Running , Waiting , and Finished states Init , Ready , Running , Waiting , and Finished states Scheduler Scheduler Thread creation Thread creation resumes thread resumes thread (e.g. thread_create() ) (e.g. thread_create() ) Ready Running Ready Running Init Finished Init Finished TCB: Ready list TCB: Running list Waiting Waiting Registers: in TCB (or Registers: Processor pushed on thread’ s stack) � 21 � 22 Threads Life Cycle Threads Life Cycle Threads (just like processes) go through a sequence of Threads (just like processes) go through a sequence of Init , Ready , Running , Waiting , and Finished states Init , Ready , Running , Waiting , and Finished states Scheduler Scheduler Thread creation Thread creation resumes thread resumes thread (e.g. thread_create() ) (e.g. thread_create() ) Ready Running Ready Running Init Finished Init Finished Thread yields Thread yields Scheduler suspends thread Scheduler suspends thread (e.g. thread_yield() ) (e.g. thread_yield() ) TCB: Ready list TCB: Running list Waiting Waiting Registers: in TCB (or Registers: Processor pushed on thread’ s stack) � 23 � 24

  7. Threads Life Cycle Threads Life Cycle Threads (just like processes) go through a sequence of Threads (just like processes) go through a sequence of Init , Ready , Running , Waiting , and Finished states Init , Ready , Running , Waiting , and Finished states Scheduler Scheduler Thread creation Thread creation resumes thread resumes thread (e.g. thread_create() ) (e.g. thread_create() ) Ready Running Ready Running Init Finished Init Finished Thread yields Thread yields Scheduler suspends thread Scheduler suspends thread Event occurs Thread waits for event Thread waits for event (e.g. thread_yield() ) (e.g. thread_yield() ) (e.g. other thread (e.g. thread_join() ) (e.g. thread_join() ) calls thread_exit() ) TCB: Synchronization TCB: Ready list Waiting Waiting variable’ s waiting list Registers: in TCB (or Registers: TCB pushed on thread’ s stack) � 25 � 26 Threads Life Cycle Threads Life Cycle Threads (just like processes) go through a sequence of Threads (just like processes) go through a sequence of Init , Ready , Running , Waiting , and Finished states Init , Ready , Running , Waiting , and Finished states Scheduler Scheduler Thread exit Thread creation Thread creation resumes thread resumes thread (e.g. thread_exit() ) (e.g. thread_create() ) (e.g. thread_create() ) Ready Running Ready Running Init Finished Init Finished Thread yields Thread yields Scheduler suspends thread Scheduler suspends thread Event occurs Event occurs Thread waits for event Thread waits for event (e.g. thread_yield() ) (e.g. thread_yield() ) (e.g. other thread (e.g. other thread (e.g. thread_join() ) (e.g. thread_join() ) calls thread_exit() ) calls thread_exit() ) TCB: Finished list (to pass TCB: Running list Waiting exit value), then deleted Waiting Registers: Processor Registers: TCB � 27 � 28

Recommend


More recommend