Presentation is loading. Please wait.

Presentation is loading. Please wait.

Threads Many software packages are multi-threaded Web browser: one thread display images, another thread retrieves data from the network Word processor:

Similar presentations


Presentation on theme: "Threads Many software packages are multi-threaded Web browser: one thread display images, another thread retrieves data from the network Word processor:"— 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 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 Other State TCB 9 Link Registers Other State TCB 6 Link Registers Other State TCB 16 Link Registers Other State TCB 8 Link Registers Other State TCB 2 Link Registers Other State TCB 3 Link Registers Head Tail Head Tail Head Tail Head Tail Head Tail Ready Queue Tape Unit 0 Disk Unit 0 Disk Unit 2 Ether Netwk 0

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


Download ppt "Threads Many software packages are multi-threaded Web browser: one thread display images, another thread retrieves data from the network Word processor:"

Similar presentations


Ads by Google