What is a Thread in OS and what are the differences between a Process and a Thread?
While reading this blog, you may be listening to music or doing some other work. In other words, you may be performing different tasks parallelly. In the same way, in a computer for the processes to run in parallel, threads are used. In a computer or mobile game when you see objects like cars, trucks, etc, these are just threads that run in the game application. Consider another example where a web browser may have a thread to display images or videos while other threads to fetch the data from the internet. So, a single application may need to perform several similar tasks parallel. Thread is used to improve the application through parallelism. So, let's get started and learn more about threads.
A thread is an execution unit that has its own program counter, a stack and a set of registers that reside in a process. Threads can’t exist outside any process. Also, each thread belongs to exactly one process. The information like code segment, files, and data segment can be shared by the different threads.
Threads are popularly used to improve the application through parallelism. Actually only one thread is executed at a time by the CPU, but the CPU switches rapidly between the threads to give an illusion that the threads are running parallelly.
Threads are also known as light-weight processes.
The diagram above shows the single-threaded process and the multi-threaded process. A single-threaded process is a process with a single thread. A multi-threaded process is a process with multiple threads. As the diagram clearly shows that the multiple threads in it have its own registers, stack, and counter but they share the code and data segment.
Types of Thread
- The user-level threads are managed by users and the kernel is not aware of it.
- These threads are faster to create and manage.
- The kernel manages them as if it was a single-threaded process.
- It is implemented using user-level libraries and not by system calls. So, no call to the operating system is made when a thread switches the context.
- Each process has its own private thread table to keep the track of the threads.
- The kernel knows about the thread and is supported by the OS.
- The threads are created and implemented using system calls.
- The thread table is not present here for each process. The kernel has a thread table to keep the track of all the threads present in the system.
- Kernel-level threads are slower to create and manage as compared to user-level threads.
Advantages of threads
- Performance: Threads improve the overall performance(throughput, computational speed, responsiveness) of a program.
- Resource sharing: As the threads can share the memory and resources of any process it allows any application to perform multiple activities inside the same address space.
- Utilization of Multiple Processor Architecture: The different threads can run parallel on the multiple processors hence, this enables the utilization of the processor to a large extent and efficiency.
- Reduced Context Switching Time: The threads minimize the context switching time as in Thread Switching, the virtual memory space remains the same.
- Concurrency: Thread provides concurrency within a process.
- Parallelism: Parallel programming techniques are easier to implement.
Difference between process and thread
- Definition: Process means a program that is currently under execution, whereas thread is an entity that resides within a process that can be scheduled for execution.
- Termination Time: The processes take more time to terminate, whereas threads take less time to terminate.
- Creation Time: The process creation time takes more time as compared to thread creation time.
- Context Switching Time: Process context switching takes more time as compared to the thread context switching.
- Communication: The communication between threads requires less time as compared to the communication between processes.
- Resources: Processes are also called heavyweight processes as they use more resources. The threads are called light-weight processes as they share resources.
- Memory: A Process is run in separate memory space, whereas threads run in shared memory space.
- Sharing Data: Different processes have different copies of data, files, and codes whereas threads share the same copy of data, file and code segments.
- Example: Opening a new browser (say Chrome, etc) is an example of creating a process. At this point, a new process will start to execute. On the contrary, opening multiple tabs in the browser is an example of creating the thread.
That’s it for this blog. We will learn more about multi-threading and its models in the next blog. Hope you enjoyed this blog.
Do share this blog with your friends to spread the knowledge. Visit our YouTube channel for more content.
Keep Learning :)