Thread Control Block in Operating System
Last Updated :
22 May, 2025
The operating system maintains management information about each process in a Process Control Block (PCB). When a process is divided into multiple threads, each thread has its own Thread Control Block (TCB). The TCB holds information specific to that thread’s execution, such as the thread ID, program counter, register states, and stack pointer.
While threads within the same process share most of the process management information stored in the PCB, the TCB contains the data unique to each thread’s execution context, allowing the operating system to manage and schedule threads independently.
Thread Control Blocks (TCBs)
A thread control block (TCB) is a fundamental data structure used by an operating system to manage threads. It contains all the information necessary to track and control the execution of a thread. It contains a thread identifier, a reference to the PCB for the process to which it belongs, a reference to the TCB for the thread that created it, and an execution snapshot.
Thread Control Block diagramKey Components of a Thread Control Block (TCB)
- Thread ID
It is a unique identifier assigned by the Operating System to the thread when it is being created. We can think of this as a name tag or ID card for a thread. It helps the operating system tell one thread apart from another.
- Thread State
This tells us what the thread is doing right now if it is running on the CPU or it is waiting for something or it is ready to run but waiting its turn. These are the states of the thread which changes as the thread progresses through the system
- Program Counter (PC)
It points to the next instruction the thread will execute. Imagine reading a book and keeping your finger on the last line you read. The program counter is like that it remembers the exact spot in the thread's code where it left off, so it knows where to continue.
- Registers
It stores the CPU register contents for the thread. These are saved when a context switch occurs. These are small storage areas in the CPU that hold temporary information like numbers the thread is working with. When a thread is paused, its register data needs to be saved, so it can pick up exactly where it left off.
- Stack Pointer
It points to the thread's own stack in memory, which holds function calls, local variables, etc. Each thread has its own little “to-do list” called a stack, which includes things like function calls and temporary variables. The stack pointer tells the OS where that list is stored in memory.
- Priority
It indicates the thread's priority for scheduling. This is a number that tells the operating system how important this thread is for execution. It indicates the weight (or priority) of the thread over other threads which helps the thread scheduler to determine which thread should be selected next from the READY queue. Higher priority threads may get more CPU time, kind of like letting VIPs skip the line.
- Thread-Specific Data
Any user-defined data associated with the thread. Sometimes a thread needs to carry around its own special data like a suitcase of personal stuff. This part of the TCB holds that custom information.
- Pointer to Process Control Block (PCB)
It links the thread to the process it belongs to (in multithreaded environments). This is like saying a thread is a worker, and the process is the company it works for. This pointer connects the thread to the information about the whole process of which it is a part .
How TCB Works in Multithreading
The Thread Control Block (TCB) is a data structure used by the operating system to store information about a thread. It plays a crucial role in managing and scheduling threads by maintaining each thread’s execution context.
TCB workingStep | What Happens |
---|
Thread is created | The OS allocates a TCB to store the new thread’s state and context. |
Thread is scheduled | The CPU loads the thread’s register values and program counter from the TCB. |
Thread blocks (e.g., I/O wait) | The thread's current state (registers, stack pointer, etc.) is saved in its TCB. |
Thread resumes | The OS restores the thread’s context from its TCB so it can continue execution. |
This mechanism allows the OS to switch between threads efficiently and ensures that each thread resumes execution exactly where it left off.
Functions of TCB (Thread Control Block)
- Thread Creation
Allocates and initializes a new TCB with information such as thread ID, priority, stack pointer, and initial state. - Context Switching
Saves the current thread's context (registers, program counter, etc.) into its TCB and restores the next thread's context from its TCB. - Scheduling
Contains scheduling-related metadata (e.g., priority, state) that helps the operating system decide which thread to run next. - Synchronization
Stores synchronization information like mutex ownership, condition variables, semaphores, or wait queues to coordinate thread execution. - Termination
Cleans up and deallocates the TCB and associated resources when a thread finishes execution or is terminated.
Real-World Applications of Threads and TCBs
- Web Servers : Handle multiple simultaneous client requests using threads.
Example: Apache HTTP Server spawns worker threads to manage each connection efficiently.
- GUI Applications : Maintain a responsive user interface by offloading tasks like I/O or computations to background threads.
Example: A media player decodes video in a separate thread while the UI remains smooth.
- Database Systems : Use threads for parallel execution of queries and background maintenance tasks.
Example: MySQL or PostgreSQL uses worker threads for query execution, indexing, and replication.