- OS - Home
- OS - Overview
- OS - History
- OS - Evolution
- OS - Functions
- OS - Components
- OS - Structure
- OS - Architecture
- OS - Services
- OS - Properties
- Process Management
- Processes in Operating System
- States of a Process
- Process Schedulers
- Process Control Block
- Operations on Processes
- Process Suspension and Process Switching
- Process States and the Machine Cycle
- Inter Process Communication (IPC)
- Context Switching
- Threads
- Types of Threading
- Multi-threading
- System Calls
- Scheduling Algorithms
- Process Scheduling
- Types of Scheduling
- Scheduling Algorithms Overview
- FCFS Scheduling Algorithm
- SJF Scheduling Algorithm
- Round Robin Scheduling Algorithm
- HRRN Scheduling Algorithm
- Priority Scheduling Algorithm
- Multilevel Queue Scheduling
- Lottery Scheduling Algorithm
- Starvation and Aging
- Turn Around Time & Waiting Time
- Burst Time in SJF Scheduling
- Process Synchronization
- Process Synchronization
- Solutions For Process Synchronization
- Hardware-Based Solution
- Software-Based Solution
- Critical Section Problem
- Critical Section Synchronization
- Mutual Exclusion Synchronization
- Mutual Exclusion Using Interrupt Disabling
- Peterson's Algorithm
- Dekker's Algorithm
- Bakery Algorithm
- Semaphores
- Binary Semaphores
- Counting Semaphores
- Mutex
- Turn Variable
- Bounded Buffer Problem
- Reader Writer Locks
- Test and Set Lock
- Monitors
- Sleep and Wake
- Race Condition
- Classical Synchronization Problems
- Dining Philosophers Problem
- Producer Consumer Problem
- Sleeping Barber Problem
- Reader Writer Problem
- OS Deadlock
- Introduction to Deadlock
- Conditions for Deadlock
- Deadlock Handling
- Deadlock Prevention
- Deadlock Avoidance (Banker's Algorithm)
- Deadlock Detection and Recovery
- Deadlock Ignorance
- Resource Allocation Graph
- Livelock
- Memory Management
- Memory Management
- Logical and Physical Address
- Contiguous Memory Allocation
- Non-Contiguous Memory Allocation
- First Fit Algorithm
- Next Fit Algorithm
- Best Fit Algorithm
- Worst Fit Algorithm
- Buffering
- Fragmentation
- Compaction
- Virtual Memory
- Segmentation
- Buddy System
- Slab Allocation
- Overlays
- Free Space Management
- Locality of Reference
- Paging and Page Replacement
- Paging
- Demand Paging
- Page Table
- Page Replacement Algorithms
- Optimal Page Replacement Algorithm
- Belady's Anomaly
- Thrashing
- Storage and File Management
- File Systems
- File Attributes
- Structures of Directory
- Linked Index Allocation
- Indexed Allocation
- Disk Scheduling Algorithms
- FCFS Disk Scheduling
- SSTF Disk Scheduling
- SCAN Disk Scheduling
- LOOK Disk Scheduling
- I/O Systems
- I/O Hardware
- I/O Software
- I/O Programmed
- I/O Interrupt-Initiated
- Direct Memory Access
- OS Types
- OS - Types
- OS - Batch Processing
- OS - Multiprocessing
- OS - Hybrid
- OS - Monolithic
- OS - Zephyr
- OS - Nix
- OS - Linux
- OS - Blackberry
- OS - Garuda
- OS - Tails
- OS - Clustered
- OS - Haiku
- OS - AIX
- OS - Solus
- OS - Tizen
- OS - Bharat
- OS - Fire
- OS - Bliss
- OS - VxWorks
- OS - Embedded
- OS - Single User
- Miscellaneous Topics
- OS - Security
- OS Questions Answers
- OS - Questions Answers
- OS Useful Resources
- OS - Quick Guide
- OS - Useful Resources
- OS - Discussion
Operating System - Threads
What are Threads in OS?
Threads in OS are single sequence stream within a process. They are also called lightweight processes. A thread is a flow of execution through the process code, with its own program counter that keeps track of which instruction to execute next, system registers which hold its current working variables, and a stack which contains the execution history.
A thread shares with its peer threads few information like code segment, data segment and open files. When one thread alters a data segment memory item, all other threads can see that change and can also modify it.
Threads are used to achieve parallelism. It divides a process into multiple threads which have independent path of execution while sharing the same common memory, space, and resource. For example: tabs of a web browser, text editors and many more.
Each thread belongs to exactly one process and no thread can exist outside a process. Each thread represents a separate flow of control. Threads have been successfully used in implementing network servers and web servers. They also provide a suitable foundation for parallel execution of applications on shared memory multiprocessors.
The following figure shows the working of a single-threaded and a multithreaded process.
Why Do We Need Threads?
Threads are needed in operating systems for various reasons mentioned below −
- Responsiveness − It allows applications to be responsive to user input i.e., other threads can react to user's action if one thread is busy.
- Resource Sharing − Due to memory and resource sharing, there is efficient communication without inter-process communication.
- Cost Efficient − To create and manage threads, it requires less resources and time compared to creating separate processes.
- Parallelism − Threads implement parallelism on multi-core and multiprocessor systems. It improves the performance.
- Better CPU Utilization − When one thread is blocked for I/O operations, other threads can continue execution. It helps in maximizing CPU usage.
- Simplified Program Design − Threads divide a bigger task into smaller concurrent tasks. It makes code easier to maintain.
Components of a Thread
The key components of a thread are mentioned below −
- Thread id − It represents a unique identifier for each thread to differentiate one thread from other. The operating system uses this id to track and manage the threads.
- Program Counter − It is a register that holds the address of the next instruction that will be executed by the thread. Each thread has its own program counter.
- Register Set − It is a collection of registers that holds the current working variables and temporary data for the thread. Each thread maintains its own set of register values.
- Stack − It stores local variables, function parameters, and return addresses for the thread. Each thread has its own stack. The stack of the thread maintains execution history, function call sequence, and prevents interference with other threads.
Types of Threads
An operating System is divided into two spaces: User Space and Kernel Space. Based on these two spaces, threads can be classified into −
User-Level Threads(ULTs)
ULTs or User-level threads are implemented in user space by thread libraries. In this, the kernel is not aware of ULTs. Kernel manages them just like a single threaded processes. In this space, operations like thread creation, scheduling, synchronization is done.
Kernel-Level Threads(KLTs)
The kernel of operation system directly manages KLTs or Kernel-level threads. The kernel maintains thread information and also performs operations like thread creation, scheduling, and management. In this space, the kernel is aware of all threads in the system unlike ULTs.
Thread Operations
You can perform various operations on a thread that are mentioned below −
- Thread Creation − You can create a new thread within a process. The new thread will have its own program counter, register set, and stack space. It will also share the code, data, and files with other threads in the same process.
- Thread Termination − It occurs either when a thread completes its execution or is manually terminated. When a thread is terminated, then all its resources are deallocated.
- Thread Join: In this operation, one thread waits for another thread to complete its execution.
Difference between Process and Thread
The differences between a process and a thread is given below −
| Process | Thread |
|---|---|
| Process is heavy weight or resource intensive. | Thread is light weight, taking lesser resources than a process. |
| Process switching requires more involvement of operating system. | Thread switching requires less involvement of operating system. |
| Each process has its own memory space and resources. | Threads have shared memory and resources as they are part of the same process. |
| In case of processes, complete process waits if blocked and other process can execute. | In case of threads, only one thread waits and other threads can execute if blocked. |
| In case of processes, each process are independent of each other and operates independently. | In threading, a thread can read, write or change another thread's data. |
Advantages of Thread
The key advantages of threading are as follows −
- Threads context switching is faster than process context switching.
- Use of threads provides concurrency within a process.
- Efficient communication due to shared memory and resources.
- It is more economical to create and context switch threads.
- Threads allow utilization of multiprocessor architectures to a greater scale and efficiency.
Disadvantages of Threads
Threads have many advantages as discussed above, but there are some disadvantages too that are mentioned below −
- Memory overhead for stack space and CPU time overhead for context switching.
- May create inconsistencies since they share same memory space.
- Increased programming complexity.
- Since threads have access of each other's data, it increases security risks.
Conclusion
In this chapter, we understood about basics of threading. A thread is a single sequence stream within a process which is also known as lightweight processes. It has four components: thread id, program counter, register set, and a stack. A thread is of two types based on the space: User-Level Threads(kernel unaware about it) and Kernel-Level Threads(kernel managed). We also understood about operations that can be performed on a thread i.e., creation, termination and thread join. The next chapter will be based on the types of threading.