What is a Thread?
A thread is a path of execution within a process. Also, a process can contain multiple threads.
Thread is also known as lightweight process. The idea is achieve parallelism by dividing a process into multiple threads. For example, in a browser, multiple tabs can be different threads. MS word uses multiple threads, one thread to format the text, other thread to process inputs etc. More advantages of multithreading are discussed below
Process vs Thread?
The typical difference is that threads within the same process run in a shared memory space, while processes run in separate memory spaces.
Threads are not independent of one other like processes as a result threads shares with other threads their code section, data section and OS resources like open files and signals. But, like process, a thread has its own program counter (PC), a register set, and a stack space.
Advantages of Thread over Process
1. Responsiveness: If the process is divided into multiple threads, if one thread completed its execution, then its output can be immediately responded.
2. Faster context switch: Context switch time between threads is less compared to process context switch. Process context switch is more overhead for CPU.
3. Effective Utilization of Multiprocessor system: If we have multiple threads in a single process, then we can schedule multiple threads on multiple processor. This will make process execution faster.
4. Resource sharing: Resources like code, data and file can be shared among all threads within a process.
Note : stack and registers can’t be shared among the threads. Each thread have its own stack and registers.
5. Communication: Communication between multiple thread is easier as thread shares common address space. while in process we have to follow some specific communication technique for communication between two process.
6. Enhanced Throughput of the system: If process is divided into multiple threads and each thread function is considered as one job, then the number of jobs completed per unit time is increased. Thus, increasing the throughput of the system.
Types of Thread
There are two types of thread.
User Level Thread
Kernel Level Thread.
Refer User Thread vs Kernel Thread for more details.
Below are Previous Year Gate Questions on Threads:
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.