Skip to main content

Command Palette

Search for a command to run...

OS Part 5: Threads 🧶

Published
•6 min read
OS Part 5: Threads 🧶
A

That Indian Dev 🇮🇳

In this instalment, we will delve into the world of concurrency and parallelism using threads and their impact on system performance. Discover the intricacies of thread scheduling, context switching, thread control block and the benefits of multithreading, including improved resource utilization, responsiveness, and the ability to leverage multiprocessor architectures.

Concurrency vs Parallelism

ConcurrencyParallelism
MeaningOverlapping execution of tasks or processes in timeSimultaneous execution of tasks or processes
Resource utilisationShared resources can be used efficientlyMultiple resources are utilised simultaneously
Execution styleTasks may not execute simultaneously but make progress concurrentlyTasks execute simultaneously

It's important to note that concurrency and parallelism are not mutually exclusive, and they can be combined to achieve optimal performance in certain scenarios.

Thread

A thread is a single sequence stream within a process, representing an independent path of execution. Threads are lightweight processes used to achieve parallelism by dividing a process's tasks into separate, concurrent paths of execution. For example, in a browser, multiple tabs can be considered as separate threads, each handling its own tasks simultaneously. Similarly, in a text editor, while you are typing, spell checking, text formatting, and saving the document can be performed concurrently by multiple threads, enhancing efficiency and responsiveness.

Thread Scheduling

Thread scheduling is a vital aspect of managing threads within a system, where each thread is assigned a priority and allocated processor time slices by the operating system. This ensures fair utilization of system resources and allows for concurrent execution of multiple threads, optimizing performance, and resource allocation, and providing a responsive multitasking experience.

Thread Context Switching

Thread context switching is a crucial mechanism in which the operating system saves the current state of a thread and switches to another thread within the same process. This context switch involves preserving important information such as the program counter, registers, and stack, while excluding the switching of memory address space. Notably, thread context switching is faster compared to process switching since it allows for the preservation of the CPU's cache state, resulting in improved performance and efficiency.

Context Switching: Process vs Thread

Process Context SwitchingThread Context Switching
Saving and SwitchingOS saves the current state of a process and switches to another process by restoring its stateOS saves the current state of a thread and switches to another thread within the same process
Memory Address SpaceProcess context switching involves switching of memory address spaceThread context switching doesn't include switching of memory address space
Speed of SwitchingProcess context switching is relatively slower due to the inclusion of memory address space switchingThread context switching is generally faster due to not involving memory address space switching
CPU's Cache State PreservationThe CPU's cache state may be flushed or invalidated during process context switching, as the memory address space is switchedThe CPU's cache state is typically preserved during thread context switching

Thread Execution

During thread execution, each thread operates with its own program counter, which keeps track of the currently executing instruction within the thread. The operating system plays a crucial role in scheduling these threads based on the designated thread scheduling algorithm. When a thread is scheduled for execution, the operating system fetches the instructions corresponding to the program counter of that particular thread and proceeds to execute them. This enables efficient and coordinated execution of multiple threads within a system, ensuring that each thread progresses through its instructions in the correct order and at the appropriate time.

Thread Control Block (TCB)

The Thread Control Block (TCB) is a data structure maintained by the operating system for each thread in a system. It contains essential information about the thread, facilitating its management and execution. The TCB serves as a central repository of information that the operating system needs to handle thread scheduling, context switching, and resource allocation.

The TCB typically includes the following information:

  1. Thread Identifier: A unique identifier assigned to the thread, allowing the operating system to distinguish between different threads.

  2. Thread State: The current state of the thread, such as running, ready, blocked, or terminated. This information helps the operating system determine the availability of the thread for execution.

  3. Program Counter (PC): The program counter value indicates the address of the next instruction to be executed by the thread.

  4. Register Set: The TCB includes a snapshot of the thread's register set, which includes general-purpose registers, stack pointers, and other registers necessary for proper execution.

  5. Thread Priority: The priority assigned to the thread, which helps the operating system determine the order and importance of thread execution.

  6. Stack Pointer: The stack pointer points to the current top of the thread's stack, enabling the operating system to manage the thread's execution context and memory usage.

  7. Thread-specific Data: Any additional information specific to the thread, such as thread-local variables or thread-specific settings, can be stored in the TCB.

Multithreading in a Single CPU

When it comes to multithreading on a single CPU, there is no significant gain because two threads have to undergo context switching to utilize that single CPU. This means that while one thread is executing, the other thread needs to wait for its turn, resulting in potential delays and overhead associated with context switching. In this scenario, the overall performance improvement from multithreading is limited due to the inherent limitations of a single CPU.

Benefits of Multithreading

  1. Responsiveness: Multithreading allows for improved responsiveness in applications. By dividing tasks into multiple threads, time-consuming operations can be executed concurrently. This means that while one thread is performing a computationally intensive task, other threads can continue to respond to user interactions, keeping the application more responsive and interactive.

  2. Resource Sharing: Multithreading enables efficient sharing of system resources. Threads within the same process can easily share data and resources, such as memory, files, and network connections, without the need for complex communication mechanisms. This facilitates efficient and seamless collaboration between threads, resulting in better resource utilization and overall system performance.

  3. Economy: Creating and context-switching threads is more economical compared to creating and managing separate processes. The overhead associated with process creation, including memory allocation and resource setup, is significantly reduced when using threads within the same process. This makes multithreading a more cost-effective approach, particularly when dealing with tasks that can be divided into smaller, independent units of work.

  4. Utilization of Multiprocessor Architectures: Multithreading allows for the efficient utilization of multiprocessor systems. Each thread can be assigned to a different processor core, enabling parallel execution and leveraging the processing power of multiple CPUs. This can lead to significant performance gains, as the workload is distributed across multiple processors, enhancing scalability and overall system efficiency.

As we conclude our exploration of threads and their impact on system performance, we have witnessed the advantages they bring in terms of concurrency, resource utilization, and responsiveness. Looking ahead to Part 6, we will delve into the crucial topic of synchronization, where we will uncover the challenges posed by concurrent execution and unveil essential techniques and strategies for maintaining data integrity and preventing race conditions.