Chapter 12 - Concurrency essentials Flashcards
Sequential Computing:
Sequential computing involves executing instructions or tasks one after another in a linear, step-by-step fashion. Each instruction or task is completed before the next one begins. In this approach, the processor handles one task at a time, and the execution follows a single path or sequence. This is the traditional form of computing.
Advantages:
Simplicity in programming and debugging.
Deterministic behavior, making it easier to reason about the program’s execution flow.
Absence of concurrency-related issues.
Disadvantages:
Potential inefficiencies, as the processor may remain idle during some parts of the computation.
Limited scalability and slower execution for complex or time-consuming tasks.
Parallel Computing
Parallel computing involves simultaneously executing multiple tasks or instructions at the same time. It leverages the presence of multiple processing units (e.g., multiple cores, multiple computers) to perform computations concurrently. Parallelism can be at different levels, including instruction-level, task-level, data-level, or higher levels of granularity.
Advantages:
Faster processing and improved performance, especially for complex and computationally intensive tasks.
Enhanced scalability, allowing the system to handle larger workloads efficiently.
Effective utilization of resources, leading to higher throughput.
Disadvantages:
Complexity in programming and debugging due to the need to manage synchronization and coordination between concurrent tasks.
Potential for race conditions, deadlocks, and other concurrency-related issues that require careful design and management.
Parallel computing architechture
Shared Memory Architecture:
Description: In this architecture, multiple processors share a common memory space, allowing them to directly access and modify shared data.
Distributed Memory Architecture:
Description: Each processor has its own private memory, and communication between processors occurs explicitly via message passing.
SIMD (Single Instruction, Multiple Data):
Description: Executes the same instruction on multiple data points simultaneously using a single control unit.
MIMD (Multiple Instruction, Multiple Data):
Description: Allows different processors to execute different instructions on different data concurrently.
NUMA (Non-Uniform Memory Access):
Description: Multiple processors have access to a shared memory space, but access times can vary based on the distance of the memory from the processor.
Shared Memory
Shared Memory:
Description: In a shared memory architecture, multiple processors share a common, global memory space. All processors can directly access any part of this shared memory to read or write data.
Distributed Memory
Distributed Memory:
Description: In a distributed memory architecture, each processor has its own private memory, and there is no shared global memory. Processors communicate by explicitly sending and receiving messages to share data.
Comparison of shared and distributed memory
Scalability:
Shared Memory: Limited scalability due to contention for shared memory.
Distributed Memory: Highly scalable due to dedicated memory for each processor.
Programming Model:
Shared Memory: Simpler programming model, as all processors can access shared data directly.
Distributed Memory: More complex programming model, as communication and synchronization require explicit message passing.
Communication:
Shared Memory: Implicit communication through shared memory.
Distributed Memory: Explicit communication via message passing.
Synchronization:
Shared Memory: Synchronization is typically easier due to shared memory constructs.
Distributed Memory: Requires careful synchronization through message passing and other synchronization mechanisms.
SMP (Symmetric Multiprocessing)
architecture is a common and widely used parallel computing architecture that involves multiple identical processors or cores that share a common memory and are capable of processing tasks concurrently. In an SMP system, each processor has equal access to all the memory and I/O devices, and each processor can perform any task or job in the system.
NUMA (Non-Uniform Memory Access):
Description: In a NUMA architecture, multiple processors (or nodes) are connected to a shared memory system, but the memory access time can vary depending on the proximity of the memory to the processor. Each processor has access to its local memory, but it can also access memory from other nodes, albeit with higher latency.
UMA (Uniform Memory Access)
:
Description: In a UMA architecture, all processors have equal and uniform access time to the shared main memory. It implies that the memory access time is the same regardless of the location of the memory in the system.
difference between processes and threads
Processes and threads are fundamental concepts in operating systems and concurrent programming. They are both units of execution, but they have key differences in terms of resource allocation, memory space, and communication.
Communication and Synchronization:
Process: Communication between processes is typically more complex and involves inter-process communication (IPC) mechanisms such as pipes, sockets, message queues, or shared memory.
Thread: Communication between threads within the same process is simpler and can be achieved through shared variables and data structures. Threads can also use synchronization mechanisms like locks, semaphores, and barriers to coordinate their actions.
Context Switching:
Process: Context switching between processes is typically more time-consuming and resource-intensive due to the need to save and restore the entire process state.
Thread: Context switching between threads within the same process is faster and more efficient because threads share the same memory space and most of the process state.
Independence:
Process: Processes are independent and can execute independently of each other. Failure in one process does not affect others.
Thread: Threads within a process share the process’s resources and are dependent on each other. If one thread encounters an error, it can affect the entire process.
Process
A process is a standalone program execution unit that has its own memory space, file handles, and system resources. It encapsulates the program code, data, and resources needed for the program to execute.
Thread
A thread is a subset of a process, representing a single sequential flow of control within the process. Threads share the same memory space and resources within a process.
is threads part of processes/
Yes, threads are typically part of processes. A process is a unit of execution in an operating system that includes its own memory space, resources, and at least one thread of execution. When a program is run, the operating system creates a process for it, and within that process, at least one thread is created to execute the program’s instructions.
Difference between concurency and parallelism
Key Differences:
Execution Model:
Concurrency: Concurrent tasks can overlap in time, and the system manages their execution to maximize overall throughput. It doesn’t guarantee simultaneous execution but ensures efficient progress on multiple tasks.
Parallelism: Parallel tasks or subtasks are executed simultaneously, taking advantage of multiple processing units (cores, processors) to achieve faster execution.
Resource Usage:
Concurrency: Concurrency can be achieved in a single-core system or a multi-core system. In a single-core system, tasks may time-share the available processing unit.
Parallelism: Requires a multi-core system or a distributed computing environment where tasks can execute in parallel across multiple processing units.
Achieving Efficiency:
Concurrency: Aimed at improving the responsiveness and efficiency of a system by overlapping tasks and managing their execution efficiently.
Parallelism: Aimed at improving performance and throughput by executing tasks simultaneously, which can lead to faster task completion.
Interdependency:
Concurrency: Tasks can be independent or dependent on each other, and concurrency handles the management of their overlapping execution.
Parallelism: Tasks are typically independent or loosely coupled to allow for efficient parallel execution.
execution scheduling
Thread execution scheduling is a crucial aspect of multithreading, where multiple threads within a program compete for execution time on the CPU. Effective scheduling can improve system performance, resource utilization, and overall responsiveness. Different operating systems and programming languages may have their own mechanisms for thread scheduling, but I’ll provide a general overview of thread scheduling concepts.
Thread Scheduling Concepts:
Preemptive vs. Non-preemptive Scheduling:
Preemptive scheduling: The operating system can interrupt a thread’s execution and allocate the CPU to another thread if a higher-priority thread becomes available.
Non-preemptive (cooperative) scheduling: Threads yield control voluntarily, and scheduling decisions are made by the threads themselves. This is generally less common in modern operating systems.
Scheduling Algorithms:
Round Robin: Threads are assigned a fixed time slice (quantum) during which they can execute. After this time slice, they are moved to the back of the queue, and the next thread in line gets a turn.
Priority-based Scheduling: Threads are assigned priorities, and the scheduler executes higher-priority threads first. This can be preemptive or non-preemptive.
First-Come-First-Serve (FCFS): Threads are scheduled in the order they request execution.
Shortest Job First (SJF): Threads with the shortest estimated execution time are scheduled first.
Multilevel Queue Scheduling: Threads are placed into different priority queues, and each queue has its own scheduling algorithm. Threads move between queues based on their behavior and priority.
Priority Levels:
Threads can be assigned different priority levels, usually based on their importance or criticality. Higher-priority threads are scheduled to run before lower-priority ones.
Thread States:
Running: The thread is currently executing on a CPU.
Ready: The thread is ready to run but is waiting for its turn to execute.
Blocked (or Waiting): The thread is unable to execute due to waiting for some event or resource.
Terminated (or Finished): The thread has completed its execution.
Thread Synchronization:
Synchronization mechanisms like locks, semaphores, and barriers are used to control access to shared resources and coordinate thread execution.
Thread Prioritization and Affinity:
Some operating systems allow setting thread priorities and specifying CPU affinity, which determines on which CPU core a thread should run.