Introduction to threads/Lightweight Process:
A thread is a basic unit of CPU utilization. It is also called a lightweight
process. A thread is a sequence of instructions within a process. A thread
behaves like "a process within a process" but it differs from a process in that
a thread does not have its own PCB. Usually multiple threads are created within
a process Threads are different kind of program executing within process.
Processes are programs in execution within kernel.
Thread consists of the following:
- Program Counter
- Register Set
A thread shares some things with its peer threads (all the other threads in this
particular task). The things that it shares are:
- Code Section
- Data Section
- Any operating system resources, which are available to the task
A traditional heavyweight process is the same as a task with one thread.
The term thread really refers to thread of execution. A multi-threaded process
means that more, than one part of the same process may be running at one time.
In a multi-threaded task, one server thread may be blocked and waiting for
something and another thread in the same task may be running. If a heavyweight
process blocks then the whole process stops. Multithreading is useful in
programs such as web browsers where you may wish to download a file, view an
animation and print something at the same time.
When multiple threads cooperate in a single job, there is a higher throughput. If one thread must wait, the whole process does not stop. Another thread may still run. Applications that require sharing a common buffer (such as the producer consumer problem) may benefit from using threads.
Types of Threads:
Threads may be handled at different levels.
1. Kernel-Level Threads:
Kernel-supported threads are seen by the operating system and must be scheduled
by the operating system. One multithreaded process may have multiple kernel
2. User-Level Threads:
User-level threads are not seen by the operating system and are also very fast.
Switching from one thread to another in .a single process does not require a
context switch since the same process is still executing. However, if the thread
that is currently executing blocks, the rest of the process may also be blocked
if the operating system sees only one kernel thread for this process. The thread
that the kernel sees is the same as the blocked thread, hence the kernel assumes
that the whole process is blocked.