1 / 11

Threads

Threads. Many software packages are multi-threaded Web browser: one thread display images, another thread retrieves data from the network Word processor: threads for displaying graphics, reading keystrokes from the user, performing spelling and grammar checking in the background

shamus
Download Presentation

Threads

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Threads • Many software packages are multi-threaded • Web browser: one thread display images, another thread retrieves data from the network • Word processor: threads for displaying graphics, reading keystrokes from the user, performing spelling and grammar checking in the background • A thread is sometimes called a lightweight process • It is comprised over a thread ID, program counter, a register set and a stack • It shares with other threads belonging to the same process its code section, data section and other OS resources (e.g., open files) • A process that has multiples threads can do more than one task at a time

  2. Benefits • Responsiveness • One part of a program can continue running even if another part is blocked • Resource Sharing • Threads of the same process share the same memory space and resources • Economy • Much less time consuming to create and manage threads than processes

  3. Single and Multithreaded Processes

  4. Lifecycle of a Thread (or Process) • As a thread executes, it changes state: • new: The thread is being created • ready: The thread is waiting to run • running: Instructions are being executed • waiting: Thread waiting for some event to occur • terminated: The thread has finished execution • Active threads are represented by their TCBs • TCBs organized into queues based on their state

  5. Java Thread States

  6. Ready Queue Other State TCB9 Link Other State TCB6 Link Other State TCB16 Link Other State TCB8 Link Other State TCB2 Link Other State TCB3 Link Head Head Head Registers Registers Registers Registers Registers Registers Tail Tail Tail Tape Unit 0 Head Tail Disk Unit 0 Disk Unit 2 Head Tail Ether Netwk 0 Ready Queue And Various I/O Device Queues • Thread not running  TCB is in some scheduler queue • Separate queue for each device/signal/condition • Each queue can have a different scheduler policy

  7. Dispatch Loop • Conceptually, the dispatching loop of the operating system looks as follows: Loop { RunThread(); ChooseNextThread(); SaveStateOfCPU(curTCB); LoadStateOfCPU(newTCB); }

  8. Running a thread Consider first portion: RunThread() • How do I run a thread? • Load its state (registers, PC, stack pointer) into CPU • Load environment (virtual memory space, etc) • Jump to the PC • How does the dispatcher get control back? • Internal events: thread returns control voluntarily • External events: thread gets preempted

  9. Internal Events • Blocking on I/O • The act of requesting I/O implicitly yields the CPU • Waiting on a “signal” from other thread • Thread asks to wait and thus yields the CPU • Thread executes a yield() • Thread volunteers to give up CPU computePI() { while(TRUE) { ComputeNextDigit(); yield(); } }

  10. Choosing a Thread to Run • How does Dispatcher decide what to run? • Zero ready threads – dispatcher loops • Alternative is to create an “idle thread” • Can put machine into low-power mode • Exactly one ready thread – easy • More than one ready thread: use scheduling priorities • Possible priorities: • LIFO (last in, first out): • put ready threads on front of list, remove from front • Pick one at random • FIFO (first in, first out): • Put ready threads on back of list, pull them from front • This is fair and is what Nachos does • Priority queue: • keep ready list sorted by TCB priority field

  11. Summary • The state of a thread is contained in the TCB • Registers, PC, stack pointer • States: New, Ready, Running, Waiting, or Terminated • Multithreading provides simple illusion of multiple CPUs • Switch registers and stack to dispatch new thread • Provide mechanism to ensure dispatcher regains control • Many scheduling options • Decision of which thread to run

More Related