- A traditional (or heavyweight) process has a single thread of control. The process model as we have discussed it thus far is based on two independent concepts: resource grouping and execution. Sometimes it is useful to separate them; this is where threads come in.
- A thread (also referred to as a light-weight process LWP) is a basic unit of CPU utilization; it comprises
- a thread ID,
- a program counter,
- a register set,
- and a stack.
- All threads in a process have exactly the same address space, which means that they also share the same global variables.
- It shares with other threads belonging to the same process its code section, data section, and other OS resources, such as open files and signals (see Fig. 4.1).
The first column lists some items shared by all threads in a process (process properties). The second one lists some items private to each thread.
- Processes are used to group resources together; threads are the entities scheduled for execution on the CPU.
- If a process has multiple threads of control in the same address space running in quasi-parallel, as though they were separate processes (except for the shared address space).
- Multithreading works the same way as the multiple processes does. The CPU switches rapidly back and forth among the threads providing the illusion that the threads are running in parallel, albeit on a slower CPU than the real one.
- With three compute-bound threads in a process, the threads would appear to be running in parallel, each one on a CPU with one-third the speed of the real CPU.
- Fig. 4.2 illustrates the difference between a traditional single-threaded process and a multithreaded process. It shows a traditional (or heavyweight) process, on the left, and 3 LWPs are drawn on the right.
Single-threaded and multithreaded processes.
- Although a thread must execute in some process, the thread and its process are different concepts and can be treated separately.
- The threads share an address space, open files, and other resources.
- The processes share physical memory, disks, printers, and other resources.
- Since every thread can access every memory address within the process' address space, there is no protection between threads because
- it is impossible,
- it should not be necessary. They are cooperating, not competing.
- Like a traditional process (i.e., a process with only one thread), a thread can be in any one of several states. The transitions between thread states are the same as the transitions between process states.