Download 4061_18

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

4061 Session 18 (3/22)
• 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
• 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
• A process *without* isolation
Logical View of a Process
• 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
– Set of pending and blocked signals
Each Thread Has Its Own Stack
Thread States
• A Thread has the same set of states as a
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
Why Threads? Efficiency.
• Threads less costly than processes
(context switch, caching)
– E.g., handling 1000 clients: processes or
Why Not Threads?
• Programming complexity.
• Non-deterministic behavior.
• Context-switch overhead when switch
• Less efficient than select() or async I/O.
• Portability problems due to different
• 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
• Disadvantages
– How to handle blocking system calls? E.g. read from
– 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
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
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 to the UNIX
pipeline model)