Download 4061_18

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Spinning mule wikipedia , lookup

Screw thread wikipedia , lookup

Transcript
4061 Session 18 (3/22)
Today
• Finish up time
• Intro to threads
Today’s Objectives
• Compare a thread to a process
• Understand when to use threads in your
code (and why)
– Give an example of an application that
benefits from multi-threading
• Explain the difference between kernel
space and user space thread
implementations
Admin
• Homework 3 – questions?
• Movie time
Thread: Meaning
• An independent stream of instructions that
can be scheduled to run as such by the
operating system
• An execution context
– there are one or more execution contexts per
process
• A process *without* isolation
Logical View of a Process
Forking
• Create a copy of the current process
– text
– data
– stack
– heap
– kernel structures
– registers
• Threading saves us some of this effort
Logical View of Two Threads
Multi-Process vs. Multi-Thread
Thread Independence
• Threads are scheduled by the operating
system and run as independent entities
• They duplicate only some structures, e.g.:
– Stack
– Registers and program counter
– Scheduling properties (such as policy or
priority)
– Set of pending and blocked signals
Each Thread Has Its Own Stack
Thread States
• A Thread has the same set of states as a
process
Why Threads? Example
• Word processor: reformatting, saving, and
user input
Why Threads? Example
• Web server: fetch from disk and handle
incoming requests
Why Threads? Concurrency.
• Natural way to increase concurrency in the
face of blocking I/O – better overlap
processing with I/O operations
– E.g., multi-client servers
• Blocking system calls only block the
calling thread
• Increase responsiveness to interactive
user input
– E.g., windows gui
Why Threads? Resource Sharing.
• Can make access to shared data
convenient for interacting tasks
• We know that other methods for IPC can
be inelegant or slow
– E.g., pipes
Why Threads? Parallelism.
• Depending on implementation, can use
multiple processors efficiently
– E.g., computation-intensive apps like matrix
processing
Why Threads? Efficiency.
• Threads less costly than processes
(context switch, caching)
– E.g., handling 1000 clients: processes or
threads?
Why Not Threads?
• Programming complexity.
• Non-deterministic behavior.
• Context-switch overhead when switch
threads.
• Less efficient than select() or async I/O.
• Portability problems due to different
implementations
Implementation
• Does the kernel know about threads? If
not, threads have been implemented in
user space
• Somewhat controversial choice
User Space Threads
User Space Threads
• Advantages
– Can use threads even if OS doesn’t support them
– Very efficient to switch between threads (load and
store state) – no system call needed
– Each process can develop its own scheduling
algorithm
• Disadvantages
– How to handle blocking system calls? E.g. read from
keyboard
– Page faults cause all threads to block
– Threads must manually yield, or you must implement
your own timer.
Kernel Space Threads
Kernel Space Threads
• Advantages
– Programming simplicity – scheduling,
handling blocking calls is done automatically
• Disadvantages
– OS must support them
– Creating, destroying, and swapping between
threads is more expensive than user space
threads
Thread Pattern: Boss/Worker
• One thread dispatches other threads to do
useful work
• The “thread pool” is usually pre-allocated
at startup
– Creating threads expensive, increases
responsiveness
Thread Pattern: Peer
• Once the thread pool has been created,
the “boss” also becomes a worker thread
Thread Pattern: Pipeline
• Each thread part of a chain in a process
• Each thread works on data processed by
the previous thread, and hands it off to the
next thread in the pipeline
• (distributing work to optimize throughput
can be hard...compare to the UNIX
pipeline model)