Updated November 30, 2023
Introduction to Threads in Operating System
In this article, we will discuss Threads in Operating System. The execution of process code by tracking the instructions one after the other in the process is called threads. Threads control the process and each thread represents the control of the code. The tasks are run in parallel and it is the subset of the process. We can call threads as a lightweight process or LWP. Different thread does different tasks and the flow of control is separate. Threads can make the application faster by doing different things at the same time. Threads in the same process share the address and memory and are easily accessible whenever needed.
What are Threads?
- The program is divided into different tasks using threads. These tasks give the illusion of running parallel but are carried out one after the other. Thread has a program counter, registers, and stack. The program counter has the information of all the tasks and the timing, registers save the current working variables and stack stores the history of execution.
- Information such as code and data related and the open files are shared between threads. When changes are made in one thread, the alterations are seen by other threads as well. Threads in the same process share the memory space and address space.
- All the threads have a parent process in the execution of codes and no threads run without a process. Servers, be it network or web, run using threads.
- When processors share the common memory, threads help in doing the parallel execution of processes and programs. Resources needed for threads are less and is always dependent on the process. The threads share the processes and threads and the process is done smoothly. Also, threads share the tasks as if one thread is blocked, the second thread takes up the task.
- A single thread in the process can read or write or update the details in the other thread. Also, multithreading in the same process requires less memory space than multi-threads in a different process.
- When a thread takes up the task of other thread or updates the information, OS is not kept informed and all the tasks are done with accuracy.
Types of Threads in Operating System
Threads are classified based on the way they are managed. There are two types of threads.
1. User Threads
These threads are implemented and used in the user library. They cannot be created using the system. While doing thread switching, if the OS is called there will be distractions. The user thread avoids all distractions and does not interrupt the kernel system. These threads are considered as single-threaded processes by the kernel. These are implemented on the systems that do not support multithreading as it is a single thread process. These are simply represented with a single, register, counter and stack. The user threads do not create any separate tasks for creation. The switching is also fast as there is no OS intervention. There is no coordination between threads and kernel and hence if one thread breaks, the entire process is blocked.
2. Kernel Threads
Kernel manages the threads and knows each and every thread. This is a multithreading type. The kernel manages a table to track the threads in each process. Also, there is a separate table to track the processes and update whenever the changes are made. OS makes the changes in the thread while creating and managing them. Knowledge of threads is shared with the kernel and hence the time for each process is scheduled according to the execution. Kernel threads are used for applications that break in between the process. Kernel threads are slow when compared with user threads. The thread control block is needed to control the tasks.
Advantages of Threads in Operating System
Below are the following advantages of threads in OS.
- The context switching time is reduced using threads. With traditional methods, it takes longer to switch the contexts between different processes even though they belong to the same OS. This helps in managing the time of the tasks.
- While using threads, one task after the other is carried out without being instructed always. Hence concurrency is achieved for the entire process using threads.
- Communication between threads and communication between processes is made efficient with the help of threads. This helps to manage the process without being tracking the entire process using a tracker. This reduces costs.
- Since it is easy to do context switching, the cost is less and hence the entire process is economical to create and manage and switch the threads between the processes.
- Multiprocessors are used in large scale by threads as both have the same characteristics. Building multiprocessor is easy and reliable when compared to other processors.
- When multithreading is employed, it responds to the user every now and then and hence customer satisfaction is achieved.
Disadvantages of Threads in Operating System
- All the variables both local and global are shared between threads. This creates a security issue as the global variables give access to any process in the system.
- When the entire application is dependent on threads, if a single thread breaks, the entire process is broken and blocked. Thus the application is crashed. This particularly happens when the application runs the process with a single thread. When many threads are used, the threads crash each other making the communication difficult.
- Threads depend on the system and the process to run. It is not independent. Also, the execution of the process via threads is time-consuming. But processes cannot be run without threads.
- Threads are not reusable and it requires more hardware than software due to application changes from the base. Threads cannot be made work without process as they do not have their own address space.
Threads are important to the process and hence to the system. Threads are even used in designing the operating systems. The threads to be used should be carefully determined based on user thread or kernel thread. Safety and security of the codes must be considered as threads share global variables to other threads in the same address space.
Recommended Articles
This is a guide to Threads in Operating System. Here we discuss the basic overview and its types along with the advantages and disadvantages of threads in operating system. You may also have a look at the following articles to learn more –