Threads in Operating system
A thread is a flow of execution through the process code, with its own program counter that keeps track of which instruction to execute next, system registers which hold its current working variables, and a stack which contains the execution history.
A thread shares with its peer threads few information like code segment, data segment and open files. When one thread alters a code segment memory item, all other threads see that.
A thread is also called a lightweight process. Threads provide a way to improve application performance through parallelism. Threads represent a software approach to improving performance of operating system by reducing the overhead thread is equivalent to a classical process.
Each thread belongs to exactly one process and no thread can exist outside a process. Each thread represents a separate flow of control. Threads have been successfully used in implementing network servers and web server. They also provide a suitable foundation for parallel execution of applications on shared memory multiprocessors.
Difference between Thread and Process:
|Definition||Process means a program is in execution.||Thread means a segment of a process.|
|Lightweight||The process is not Lightweight.||Threads are Lightweight.|
|Termination time||The process takes more time to terminate.||The thread takes less time to terminate.|
|Creation time||It takes more time for creation.||It takes less time for creation.|
|Communication||Communication between processes needs more time compared to thread.||Communication between threads requires less time compared to processes.|
|Context switching time||It takes more time for context switching.||It takes less time for context switching.|
|Resource||Process consume more resources.||Thread consume fewer resources.|
|Treatment by OS||Different process are tread separately by OS.||All the level peer threads are treated as a single task by OS.|
|Memory||The process is mostly isolated.||Threads share memory.|
|Sharing||It does not share data||Threads share data with each other.|
Types of Thread
Threads are implemented in following two ways −
- User Level Threads − User managed threads.
- Kernel Level Threads − Operating System managed threads acting on kernel, an operating system core.
1.User Level Threads
In this case, the thread management kernel is not aware of the existence of threads. The thread library contains code for creating and destroying threads, for passing message and data between threads, for scheduling thread execution and for saving and restoring thread contexts. The application starts with a single thread.
- Thread switching does not require Kernel mode privileges.
- User level thread can run on any operating system.
- Scheduling can be application specific in the user level thread.
- User level threads are fast to create and manage.
- In a typical operating system, most system calls are blocking.
- Multithreaded application cannot take advantage of multiprocessing.
2.Kernel Level Threads
In this case, thread management is done by the Kernel. There is no thread management code in the application area. Kernel threads are supported directly by the operating system. Any application can be programmed to be multithreaded. All of the threads within an application are supported within a single process.
The Kernel maintains context information for the process as a whole and for individuals threads within the process. Scheduling by the Kernel is done on a thread basis. The Kernel performs thread creation, scheduling and management in Kernel space. Kernel threads are generally slower to create and manage than the user threads.
- Kernel can simultaneously schedule multiple threads from the same process on multiple processes.
- If one thread in a process is blocked, the Kernel can schedule another thread of the same process.
- Kernel routines themselves can be multithreaded.
- Kernel threads are generally slower to create and manage than the user threads.
- Transfer of control from one thread to another within the same process requires a mode switch to the Kernel.
Main Difference between User-Level & Kernel-Level Thread:
|S.N.||User-Level Threads||Kernel-Level Thread|
|1||User-level threads are faster to create and manage.||Kernel-level threads are slower to create and manage.|
|2||Implementation is by a thread library at the user level.||Operating system supports creation of Kernel threads.|
|3||User-level thread is generic and can run on any operating system.||Kernel-level thread is specific to the operating system.|
|4||Multi-threaded applications cannot take advantage of multiprocessing.
|Kernel routines themselves can be multithreaded.|
Next :Memory Management