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 sequentiall
- Both can create children
Differences between Threads and Processes –
- Threads are not independent, processes are.
- Threads are designed to assist each other, processes may or may not do it
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 does’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.
Disadvantages 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.
Disadvantages 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).
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.
- Traps and System Calls in Operating System (OS)
- System Protection in Operating System
- User View Vs Hardware View Vs System View of Operating System
- System Programs in Operating System
- File System Implementation in Operating System
- Xv6 Operating System -adding a new system call
- Producer-Consumer solution using threads in Java
- What are Threads in Computer Processor or CPU?
- C++ File Writer-Reader application using Windows Threads
- Cryptography and its Types
- Address Binding and its Types
- Types of Operating Systems
- Logical and Physical Address in Operating System
- Privileged and Non-Privileged Instructions in Operating System
- Booting and Dual Booting of Operating System
- Difference between Operating System and Kernel
- Difference between Application Software and Operating System
- Difference between UNIX and Windows Operating System
- Encryption, Its Algorithms And Its Future
- Introduction of Deadlock in Operating System
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.