- Process: Owner of resources allocated for individual program execution, can encompass more than one thread of execution
- Thread: Unit of execution (unit of dispatching) and a collection of
resources, with which the unit of execution is associated, characterize the notion of a process. A thread is the abstraction of a unit of execution. It is also referred to as a light-weight process LWP that share the same text (program code) and global data, but possess their own CPU register values and their own dynamic (or stack based) variables
- First look at the advantages of threads;
- a program does not stall when one of its operations blocks.
- save contents of a page to disk while downloading other page (for web server example)
- Simplification of programming model
- Single process, single thread MS-DOS, old MacOS
- Single process, multiple threads OS/161
- Multiple processes, single thread traditional Unix
- Multiple processes, multiple threads modern Unices (Solaris, Linux), Windows2000
- As a basic unit of CPU utilization, a thread consists of an
instruction pointer (also referred to as the PC or instruction
counter), CPU register set and a stack. A thread shares its code
and data, as well as system resources and other OS related information, with its peer group (other threads of the same process)
Figure 2:
Single and Multithreaded Processes
|
- Threads versus processes;
- A thread operates in much the same way as a process:
- can be one of the several states.
- executes sequentially (within a process and shares the CPU).
- can issue system calls.
- Economy of Overheads - managing processes is considerably more expensive than managing threads so LWPs are better.
- Responsiveness - less setup work means faster response to requests, and multiple thread of execution mean there can be response from some threads even if other threads are busy or blocked.
- Resource Sharing - Threads within a process share resources (including the same memory address space) conveniently and efficiently compared to separate processes
- Threads within a process are NOT independent and are NOT protected against each other
- Multiprocessor Use - if multiple processors are available, a multithreaded application can have its threads run in parallel which means better utilization (especially if there are few other processes present so that, without a multithreaded application, some CPUs would be idle)
Figure 3:
Threads and Processes
|
- A process utilizing multithreading is a process with multiple points of execution-up to now, we have assumed that each process has only one point of execution
- similarity between an operating system supporting multiple processes and a process supporting multiple threads
- Figure 2 shows a traditional (or heavyweight) process, on the left, and 3 LWPs are drawn on the right in a way to emphasize their common text and global data (i.e. data and heap)
- In practice, an application such as a web server that can have considerable variations in the rate of requests can create additional threads in response to serving load, yet minimize process creation load on the host
- Less setup improves responsiveness, and shared text means more efficient memory use
Figure 4:
A word processor with three threads, a multithreaded web server
|
Subsections
2004-03-21