Concurrency Flashcards
How do modules interact in the Shared Memory Model?
They read and write shared objects in memory.
What are the risks in the Share Memory Model?
Race conditions and incorrect states can occur.
How do modules communicate in the Message Passing Model?
They pass messages through channels.
What’s the benefit of the Message Passing Model?
Avoids shared state but still faces race condition risks.
Given Moore’s Law limitations what should we consider?
Processor clock speeds have plateaued.
What is a Race Condition?
A race condition occurs when multiple threads or processes access shared resources concurrently leading to unpredictable behavior due to the order of execution.
How can we prevent Race Conditions?
- Use synchronization mechanisms like locks semaphores
- Atomic operations to ensure exclusive access to shared resources.
- Design thread-safe data structures.
What is Deadlock?
Deadlock happens when two or more threads are blocked waiting for each other to release resources they hold. None can proceed resulting in a standstill.
How do we avoid Deadlocks?
Implement
* locking hierarchies,
* timeouts, or
* resource allocation strategies
to prevent deadlocks from occurring
What is Thread Starvation?
Thread starvation occurs when a thread doesn’t get enough CPU time due to priority inversion or resource contention.
What is Priority Inversion?
Priority inversion is a phenomenon in multi-threading where a higher-priority thread is forced to wait for a lower-priority thread to release a resource. This happens because the lower-priority thread holds a resource that the higher-priority thread needs, and an unrelated medium-priority thread preempts the lower-priority thread, causing the higher-priority thread to wait even longer
How can we address Priority Inversion?
Priority Inheritance Protocol (PIP): Temporarily raises the priority of the lower-priority thread holding the resource to match that of the highest-priority thread waiting for it2.
Priority Ceiling Protocol (PCP): Assigns a priority ceiling to each resource, ensuring that a thread can only acquire the resource if its priority is higher than the ceiling2.
Both of these approaches would prevent preemption of Lower priority thread (L) by medium priority thread (M) causing the higher priority thread (H) to wait even longer.
What are Locks and Mutexes?
Locks (or mutexes) provide mutual exclusion allowing only one thread to access a critical section at a time.
What is a Read-Write Lock?
A read-write lock allows multiple threads to read simultaneously but ensures exclusive access for writing.
What are Condition Variables?
Condition variables allow threads to wait for specific conditions (e.g. a shared resource becoming available).
What is the Producer-Consumer Problem?
It’s a classic synchronization problem where producers add items to a shared buffer and consumers remove them.
How can we solve the Producer-Consumer Problem?
- Use semaphores or monitors to coordinate producers and consumers.
- Implement bounded buffers to prevent overflow or underflow.
What are Thread Pools?
Thread pools manage a fixed set of worker threads improving efficiency by reusing threads instead of creating new ones.
What is Parallelism vs. Concurrency?
Parallelism involves executing tasks simultaneously while concurrency deals with managing multiple tasks concurrently.
What is the Actor Model?
The Actor Model represents concurrent systems as actors (independent entities) that communicate via messages.
What are Fork-Join Frameworks?
Fork-Join frameworks (e.g. Java’s ForkJoinPool) divide tasks into smaller subtasks execute them concurrently and then combine results.
What is Amdahl’s Law?
Amdahl’s Law quantifies the speedup achievable by parallelizing a program based on the fraction of serial code.
How does the operating system achieve concurrency through time-slicing?
The operating system switches between threads frequently and unpredictably allowing them to execute concurrently.
What are some synchronization primitives used for mutual exclusion?
Examples include
- locks
- monitors
- semaphores
- mutexes.
Why is programming for mutual exclusion error-prone?
Ensuring exclusive access to shared state or memory can lead to correctness issues and performance bottlenecks.
What is the impact of thread switching on overall throughput?
Frequent thread switching requires saving and resuming thread states which can be time-consuming.
How can we prevent the “hold-and-wait” condition in concurrent systems?
Processes should request all resources before execution begins to avoid holding resources while waiting.
What does “no preemption” mean in the context of preventing deadlock?
Processes automatically release held resources if a newly requested resource is unavailable.
What distinguishes livelock from deadlock?
Livelock involves processes continuously changing resource states without making progress unlike deadlock.
What is starvation in concurrent systems?
Starvation occurs when a process cannot access shared resources and cannot make progress.
How can we prevent starvation?
Use a priority queue with aging to increase the priority of waiting processes over time.
Or use fairness policy in Java Lock Api
What is the Actor Model in concurrent programming?
The Actor Model treats everything as an actor allowing actors to pass messages to each other.