11
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

Threads

  • Upload
    shamus

  • View
    26

  • Download
    0

Embed Size (px)

DESCRIPTION

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 - PowerPoint PPT Presentation

Citation preview

Page 1: 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

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

Page 2: Threads

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

Page 3: Threads

Single and Multithreaded Processes

Page 4: Threads

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

Page 5: Threads

Java Thread States

Page 6: Threads

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

OtherStateTCB9

LinkRegisters

OtherStateTCB6

LinkRegisters

OtherStateTCB16

LinkRegisters

OtherStateTCB8

LinkRegisters

OtherStateTCB2

LinkRegisters

OtherStateTCB3

LinkRegisters

Head

Tail

Head

Tail

Head

Tail

Head

Tail

Head

Tail

ReadyQueue

TapeUnit 0

DiskUnit 0

DiskUnit 2

EtherNetwk 0

Page 7: Threads

Dispatch Loop Conceptually, the dispatching loop of

the operating system looks as follows:

Loop {

RunThread();

ChooseNextThread();

SaveStateOfCPU(curTCB);

LoadStateOfCPU(newTCB);

}

Page 8: Threads

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

Page 9: Threads

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();

}

}

Page 10: Threads

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

Page 11: Threads

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