ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCOMP3139Software Architectures…
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP3139

    Parallel & Distributed Computing

    Progress0 / 33 done
    Topics33
    1Introduction to Parallel and Distributed Systems2Why Use Parallel and Distributed Systems?3Speedup and Amdahl's Law
    4Multi Processors (Shared Memory)5Networks of Workstations (Distributed Memory)6Clusters (Latest Variation)
    7Threads and Shared Memory8Processes and Message Passing9Distributed Shared Memory (DSM)10Distributed Shared Data (DSD)11Parallel Algorithms12Concurrency and Synchronization13Data and Work Partitioning14Common Parallelization Strategies15Granularity16Load Balancing
    17Parallel Search18Parallel Sorting19Shared-Memory Programming20Threads in Shared-Memory Programming21P Threads22Locks and Semaphores23Distributed-Memory Programming24Message Passing25Map Reduce26Distributed-Memory Programming with PI27Google's Map Reduce28Hadoop29Other Parallel Programming Systems30Tread Marks31Distributed Shared Memory
    32Scoped Behavior and Abstract Data Types
    33Process Templates
    9 min read
    Topic 7 of 33
    Topic Study Note Illustration
    07

    Software Architectures: Threads and Shared Memory

    In the world of parallel and distributed computing , threads and shared memory are fundamental concepts that enable multiple tasks to be executed simultaneously and share data...

    Software Architectures: Threads and Shared Memory

    In the world of parallel and distributed computing, threads and shared memory are fundamental concepts that enable multiple tasks to be executed simultaneously and share data efficiently. Understanding these concepts is crucial for designing software architectures that can leverage multi-core processors and distributed systems.

    Let’s break down threads and shared memory and explore how they fit into software architectures.


    1. What Are Threads?

    A thread is the smallest unit of execution within a process. In most modern operating systems, a program (or process) can contain multiple threads that run concurrently. Threads share the same resources of the process, such as memory, file handles, and state, but each thread has its own execution context, including a program counter, registers, and stack.

    Key Features of Threads:

    • Concurrency: Threads allow for multiple tasks to run concurrently within a single process. Each thread can execute independently, but they share the same memory and resources.
    • Lightweight: Threads are often referred to as "lightweight processes" because they share the resources of their parent process, unlike separate processes which have their own memory space.
    • Parallelism: When running on multi-core processors, threads can be executed in parallel, leading to performance improvements in multi-threaded applications.

    Types of Threads:

    1. User-level Threads:

      • Managed entirely by user-level libraries (such as pthreads in C), without kernel intervention. The operating system is unaware of these threads.
      • Scheduling and management are handled by the application, which may lead to inefficiency since the OS cannot schedule these threads independently.
    2. Kernel-level Threads:

      • Managed by the operating system kernel, which schedules threads independently and can take full advantage of multi-core systems.
      • Each thread is recognized by the OS as a separate entity, so they are scheduled like independent processes.
    3. Hybrid Threads:

      • A combination of user-level and kernel-level threads, often using both user-level threading libraries and OS-level thread management.

    2. What Is Shared Memory?

    Shared memory refers to a memory model where multiple threads or processes can access the same region of memory. This allows different threads (within the same process) or processes (in a multi-process system) to communicate and share data directly.

    Key Features of Shared Memory:

    • Efficient Communication: In shared memory systems, threads or processes can communicate by reading from and writing to the same memory location, eliminating the need for slower forms of communication, such as message passing.
    • Direct Access: Since all threads share the same memory space, they can access any part of the memory directly without the need for inter-process communication mechanisms.
    • Synchronization: Shared memory systems require mechanisms to prevent data conflicts, such as race conditions, where multiple threads or processes access the same memory simultaneously.

    Shared memory is commonly used in multi-threaded applications where threads within a single process share access to global variables or buffers.


    3. Thread and Shared Memory Relationship

    In a shared memory architecture, multiple threads of the same process can access and manipulate the same data. This is crucial for achieving concurrency and parallelism in multi-threaded applications. Since threads share the same memory, data exchange and communication between them become very efficient. However, the challenge arises when multiple threads try to read and write to the same data at the same time, which can lead to data races and inconsistent results.

    Thread Access in Shared Memory Systems:

    • Threads in the Same Process: Threads within a single process naturally share the same memory space. They can read and write data directly in shared memory locations.
    • Synchronization: To manage concurrent access to shared data, synchronization mechanisms like mutexes, semaphores, locks, and atomic operations are used to ensure that only one thread accesses a memory location at any given time, preventing race conditions.

    Example:

    Imagine a multi-threaded banking application where multiple threads are handling different customer transactions. All threads may need access to a shared bank account balance. To avoid conflicting updates (e.g., two threads trying to withdraw money at the same time), synchronization is needed to ensure that one thread completes its transaction before another thread starts.


    4. Types of Shared Memory Architectures

    Shared memory architectures can be categorized based on the scope and type of memory sharing.

    Single-Processor Systems with Shared Memory:

    • Single-Core Systems: In a single-core system, multi-threaded applications can take advantage of shared memory to execute multiple threads. However, all threads must wait their turn on the CPU, as there’s only one core to execute them.

    Multi-Processor Systems with Shared Memory:

    • Symmetric Multi-Processing (SMP): In SMP systems, multiple processors share the same physical memory, allowing threads to run concurrently on different processors. These systems can run threads in parallel, significantly improving performance.

      • Example: A multi-core CPU (e.g., Intel Core i7) has several cores, each capable of running multiple threads. These cores can share access to the same memory pool, allowing for faster communication between threads.

    Non-Uniform Memory Access (NUMA):

    • In NUMA systems, memory is divided into regions, each attached to a specific processor or set of processors. While processors can access any part of memory, local memory (attached to the processor) is faster to access than remote memory (attached to other processors).

      • Example: NUMA-enabled servers typically have multiple processors (CPUs) and memory banks, where each processor has its local memory but can access memory attached to other processors. The system must be aware of the memory topology to optimize performance.

    5. Challenges in Threaded and Shared Memory Systems

    While threads and shared memory provide great benefits in terms of performance and efficiency, they also come with challenges that developers must address:

    Race Conditions:

    • A race condition occurs when multiple threads access shared data simultaneously, and the final result depends on the sequence or timing of their execution. This leads to unpredictable behavior and bugs.
    • Example: Two threads simultaneously update a shared variable without proper synchronization, leading to an inconsistent result.

    Deadlocks:

    • Deadlock happens when two or more threads are blocked, each waiting for the other to release resources, leading to a standstill in the execution.
    • Example: Thread A locks Resource 1 and waits for Resource 2, while Thread B locks Resource 2 and waits for Resource 1. Both threads are stuck waiting for each other to release resources, resulting in a deadlock.

    Synchronization Overhead:

    • Proper synchronization can introduce overhead due to the need for acquiring and releasing locks, which can reduce the overall performance of the system, especially if threads spend significant time waiting for locks.

    Memory Consistency:

    • In multi-processor systems, ensuring memory consistency between threads or cores becomes important. When multiple threads or processors cache data locally, it may result in inconsistent views of memory.
    • Cache Coherency Protocols like MESI (Modified, Exclusive, Shared, Invalid) are used to ensure that all processors or threads see a consistent view of memory.

    6. Common Synchronization Mechanisms

    To ensure that threads interact safely in a shared memory system, several synchronization mechanisms are commonly used:

    1. Mutexes (Mutual Exclusion Locks):

      • A mutex is used to ensure that only one thread can access a particular piece of data or resource at a time. Other threads attempting to acquire the mutex will be blocked until the mutex is released.
    2. Semaphores:

      • Semaphores are used for controlling access to shared resources. A semaphore has a counter that tracks how many threads can access a resource concurrently.
    3. Condition Variables:

      • Condition variables are used to block a thread until a particular condition is met. For example, a thread may wait for another thread to complete a task before continuing its execution.
    4. Atomic Operations:

      • Atomic operations allow threads to read or modify shared memory in a way that ensures no other thread can interfere in the middle of the operation. These are typically used to implement lock-free data structures.
    5. Read-Write Locks:

      • Read-write locks allow multiple threads to read a shared resource simultaneously but ensure that only one thread can write to the resource at a time.

    7. Use Cases of Threads and Shared Memory

    Multithreaded Web Servers:

    • A web server uses multiple threads to handle concurrent client requests. Shared memory can be used to store session data or shared cache, and synchronization mechanisms ensure that the data is safely accessed and modified by different threads.

    Parallel Scientific Simulations:

    • In simulations like fluid dynamics or weather forecasting, each thread may handle a different part of the simulation. Shared memory allows threads to share the global state of the simulation, while synchronization mechanisms ensure consistency.

    Multimedia Applications:

    • In video processing or image rendering, threads can be used to process different parts of an image or video frame. Shared memory allows these threads to work together on the same data, while synchronization ensures no data conflicts.

    Conclusion

    In software architectures that use threads and shared memory, multiple threads can work concurrently and share data efficiently. This model is widely used in multi-core systems and high-performance applications, such as scientific computing, web servers, and real-time systems. While shared memory provides significant advantages in terms of speed and communication between threads, it introduces challenges like race conditions, deadlocks, and synchronization overhead. Careful design and the use of synchronization techniques are necessary to ensure correct and efficient operation in multi-threaded systems.

    ← Previous topic 6
    Hardware Architectures: Clusters (Latest Variation)
    Next topic 8 →
    Software Architectures: Processes and Message Passing

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    9mremaining
    1.5kwords
    Intermediatelevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Parallel & Distributed Computing