Operating System Threads
A Thread is a line of execution.
Process can have a single thread/single line of execution as shown above. However, in most cases, there can be multiple lines of execution in parallel. In such scenarios, OS splits its work into multiple smaller pieces and executes those in different lines of execution.
In this page, we are going to learn about:
- Advantages and benefits of OS threads
- Purpose of OS threads
- What a kernel thread is
- What an user thread is
- How the threads use address space
- How OS threads work on Multi-CPU processors.
What an OS Thread is? What is the purpose of OS threads?
OS Thread is a task executor inside a OS process. Purpose of multiple OS threads inside the same process is to improve CPU utilization. OS Threads came into existence because an OS process can have several activities and there is no need to execute activities in a particular order. A process can split its activities into multiple threads of execution and start those in parallel.
OS Processes use Threads
An OS Thread is uniquely identified by a Thread ID. Each Thread includes:
- a program counter
- a stack
- register set
Each process comprises one or more thread to complete their tasks. These threads can share their code section, data section and other operating system resources like open files and registers with one another.
- Threads are supposed to be lightweight.
- It improves the performance of the system by reducing the process overhead.
An OS Process has either a single thread of control or multi-thread control.
- In case of multithreaded processes, the process can perform more than one task at a time.
- Nowadays, many of the operating system kernels are multithreaded so that several threads operate in the kernel and each thread can do specific task.
Benefits of using Threads
Thread consumes less time to create and manage threads.
One thread can share the memory and other resources with another thread which belong to same process. That means, they can share the same address space.
More economic to create and to context switch.
Increases the responsiveness of the user interactive applications.
better utilization of multiprocessor architecture.
Use of multithreading on multi-CPU machine increases the concurrency of tasks.
Types of Threads
There are two kind of threads:
- User Thread
- Kernel Thread