Introduction to Threads
Objectives
We would like to :
- Define threads and their relationship to processes
- Take a look at the development of threads.
Notes
- A process is a program that is being executed.
- A thread is a lightweight process.
- The book states that a thread is the basic unit of CPU utilization.
- A thread has
- registers, general purpose and special purpose (ir pc, ...)
- A stack
- A process has
- Memory, code, files
- And a thread.
- This implies that threads share resources like
- Take a look at the picture on 160
- If a process has a single thread it is single-threaded
- If a process has multiple threads is called multithreaded
- What?
- They give three examples
- An application that needs to process many files could spawn/create a thread to handle each file.
- A web browser might have one thread to display the page and another to interact with the network
- A word processor could a thread to manage display, one to manage interaction and a third to manage spell/grammar checking
- Note in all three applications they are probably working on shared data, but distinct tasks.
- They mention that a web server might run one thread per connected client.A
- But why not spawn a new server for each connection?
- Process creation is expensive
- Thread creation is cheap
- And for a server we don't need copies of the open files, data and memory, one will do.
- They point out that the most kernels are heavily threaded.
- These are called kernel threads or kthreads
- They are managed by kthreadd
- The book lists the following advantages of threads
- Responsiveness: If part of a process is blocked (say for i/o) but there is more work to do, another thread can continue.
- Resource Sharing: By default, no need for shared memory or other IPC mechanisms (but we need synchronization mechanisms)
- Economy: Because of shared memory, thread context switches are easier/cheaper than process context switches.
- Scalability: A multi-threaded process can run on multiple cores.