A particular program may have multiple processes and each process has its separate address space as well as a single thread of control. Sometimes a process may have multiple threads in its address space. Now, a thread in the operating system can be determined as a fundamental unit of CPU utilization.
In our today content, we will discuss what is a thread in the operating system as well as we will also discuss the kind of threads along with the advantages of thread. So let’s begin.
Thread in Operating System
First, let us discuss the structure of a thread. A single thread has its particular thread id, which is its unique identification, it has a program counter, a set of registers, and a stack. Now, we know that a process may accommodate multiple threads and such processes perform multiple tasks at a time.
Threads associated with the same process uses the same data section, code section, and system resources such as files and signals. In the image below you can see a process with a single thread and a process with multiple threads.
Let us discuss an example where you would easily understand a process with a single thread or a process with multiple threads. Consider you open a web browser and search for today’s news. It is a process with a single thread of execution.
Now, in the same web browser, you launch a new tab where you search for images. So, it will now be a process with two threads of execution. You can open multiple tabs in the same browser window. It will be an example of a process with multiple threads of execution.
This is how multithreading supports multi-tasking.
Why are threads used?
Creating a new process for each task was in use before the concept of thread arrived. Creating a new process for the same task would indulge overhead such as it would consume more time, it will engage resources. So, it would be worthwhile to create multiple threads instead of creating multiple processes.
One of the reasons for creating a thread is that as the threads are lighter in weight they can be created and destroyed very rapidly which in turn saves time. In case if all the threads are not CPU bounded and the system has substantial computing and I/O then activities can be overlapped and thereby increases the speed of an application.
Types of Threads
Threads can be the User-level threads and Kernel level threads. We will discuss both the threads in brief.
- User Level Threads
User lever threads are managed at the application level that means the kernel does not even know about the existence of such user level of threads. The context switching of user-level threads does not require the interference of the operating system. The user-level threads execute faster when compared to kernel-level thread.
- Kernel Level Threads
The kernel-level threads are managed by the kernel of the operating system. You will not find any thread management code present at the application level. The context switch timing for the kernel-level thread is more as they are managed by the operating system. The kernel-level threads execute slowers as compared to the user-level thread.
Advantage of Thread in Operating System
- As threads belonging to the same process share the system resources, they reduce the time of context switching.
- Using multiple threads allows a process to perform multiple tasks in parallel.
- Threads belonging to the same process share the common address so the communication between threads becomes easier.
So, this is all about the thread in the operating system. It is economical to use threads instead of creating multiple processes for the same task. Thread raised the concept of multithreading.