Thread’s Concepts in Operating System
Today in this Operating System Study Material and Tutorial I will discuss about threading concepts such as threads basic , its advantages, threads implementation in operating system , user level and kernel level threads.
Lets see each one by one –
what is thread in os ?
A thread is nothing but a light weight process which can utilize the cpu independently. A thread meaning is just a flow of execution through its process code.
Like a process a thread also its thread control block. Which keeps the information about that thread. Thread control block also has program counter field that keeps track of which instruction to execute next. It also has system registers which tell about the information about its current working variables, and a stack which contains the execution history.
The most important point to be noted about the thread is that a thread shares with its peer threads various information like code segment, data segment, and open files. If one thread tries to alters a code segment memory item, then all other threads can see that.
Threads provide a method to improve application performance This method is nothing but the use of parallelism. Thread use parallelism or multi-threading concepts to improve performance of the application. Threads gives a software based approach to reduce the overhead.
More than one threads belongs to exactly one process, and no thread can exist outside a process. Each thread represents a separate flow of control.
In the present time threading concepts is also successfully used in implementing network servers and web servers. They also provide a best support for parallel execution of applications using shared memory multiprocessors.
Advantages of Threads
There are following advantages of threads in operating system
- They minimize the context switching time. Context switch overhead is less as compare to switching overhead among the processes.
- We can achieve concurrency or parallelism by using more than one threads in a single process.
- They provide efficient communication.
- It is more economical to create and context switch threads.
- It is easy to utilize the multiprocessor architectures using threads to a greater scale and efficiency.
Difference between user level thread and kernel level thread
Implementation of threads in operating system is handled in two manner which are as follows-
(1) User Level Threads
(2) Kernel Level Threads
User Level Thread
User level threads manage user threads it means thread related to user process. In this case, the thread management kernel does not have information about presence of threads. The thread library is used to manage the user level thread this thread library include the code for the following purpose.
- To create and destroy the threads.
- To pass messages and data among threads.
- To schedule an execution of thread.
- To save and restore the thread contexts.
Advantages of User Level Threads
- In user levels threads , Thread switching does not require any Kernel mode privileges.
- User level threads are independent from operating system they can run on any os.
- With the user level threads it is easy to make scheduling more application specific.
- · It is easy to create and manage fast user level thread.
Disadvantages of User Level Threads
- Most of the system calls are blocking call.
- Multi threaded application cannot take advantage of multiprocessing.
Kernel Level Threads
These are the threads managed by operating system which are acting on a kernel, an operating system core. In case of kernel level threads , thread management is done by the Kernel. Thread management code in the application area is not available here because Operating System directly supports to Kernel levels threads.
Here any application can be programmed to be multi threaded. All of the threads are supported within a single process for an application. These threads share the same code , data and file of the process to which they belong.
It is the responsibility of the kernel to 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.
Advantages of Kernel Level threads:
There are following advantages of Kernel level threads
- The Kernel can simultaneously schedule multiple threads from the same process on multiple processes.
- One major advantage of kernel level thread is that If any one thread in a process is blocked, then Kernel is capable of scheduling a new thread for that process.
- Kernel routines themselves can be multi threaded.
Disadvantages of Kernel Level threads:
- Kernel threads are created and managed slowly as compare to user level threads.