Threads and its types in Operating System
Thread is a single sequence stream within a process. Threads have same properties as of the process so they are called as light weight processes. Threads are executed one after another but gives the illusion as if they are executing in parallel. Each thread has different states. Each thread has
- A program counter
- A register set
- A stack space
Threads are not independent of each other as they share the code, data, OS resources etc.
Similarity between Threads and Processes –
- Only one thread or process is active at a time
- Within process both execute sequential
- Both can create children
- Both can be scheduled by the operating system: Both threads and processes can be scheduled by the operating system to execute on the CPU. The operating system is responsible for assigning CPU time to the threads and processes based on various scheduling algorithms.
- Both have their own execution context: Each thread and process has its own execution context, which includes its own register set, program counter, and stack. This allows each thread or process to execute independently and make progress without interfering with other threads or processes.
- Both can communicate with each other: Threads and processes can communicate with each other using various inter-process communication (IPC) mechanisms such as shared memory, message queues, and pipes. This allows threads and processes to share data and coordinate their activities.
- Both can be preempted: Threads and processes can be preempted by the operating system, which means that their execution can be interrupted at any time. This allows the operating system to switch to another thread or process that needs to execute.
- Both can be terminated: Threads and processes can be terminated by the operating system or by other threads or processes. When a thread or process is terminated, all of its resources, including its execution context, are freed up and made available to other threads or processes.
Differences between Threads and Processes –
- Resources: Processes have their own address space and resources, such as memory and file handles, whereas threads share memory and resources with the program that created them.
- Scheduling: Processes are scheduled to use the processor by the operating system, whereas threads are scheduled to use the processor by the operating system or the program itself.
- Creation: The operating system creates and manages processes, whereas the program or the operating system creates and manages threads.
- Communication: Because processes are isolated from one another and must rely on inter-process communication mechanisms, they generally have more difficulty communicating with one another than threads do. Threads, on the other hand, can interact with other threads within the same programme directly.
Threads, in general, are lighter than processes and are better suited for concurrent execution within a single programme. Processes are commonly used to run separate programmes or to isolate resources between programmes.
Types of Threads:
- User Level thread (ULT) – Is implemented in the user level library, they are not created using the system calls. Thread switching does not need to call OS and to cause interrupt to Kernel. Kernel doesn’t know about the user level thread and manages them as if they were single-threaded processes.
- Advantages of ULT –
- Can be implemented on an OS that doesn’t support multithreading.
- Simple representation since thread has only program counter, register set, stack space.
- Simple to create since no intervention of kernel.
- Thread switching is fast since no OS calls need to be made.
- Limitations of ULT –
- No or less co-ordination among the threads and Kernel.
- If one thread causes a page fault, the entire process blocks.
- Kernel Level Thread (KLT) – Kernel knows and manages the threads. Instead of thread table in each process, the kernel itself has thread table (a master one) that keeps track of all the threads in the system. In addition kernel also maintains the traditional process table to keep track of the processes. OS kernel provides system call to create and manage threads.
- Advantages of KLT –
- Since kernel has full knowledge about the threads in the system, scheduler may decide to give more time to processes having large number of threads.
- Good for applications that frequently block.
- Limitations of KLT –
- Slow and inefficient.
- It requires thread control block so it is an overhead.
- Each ULT has a process that keeps track of the thread using the Thread table.
- Each KLT has Thread Table (TCB) as well as the Process Table (PCB).