Design synchronization for water molecule assembly
Problem: Concurrency for Water Molecule Assembly
You are tasked with designing a synchronization mechanism for two types of concurrent threads: hydrogen threads and oxygen threads.
Context
Each hydrogen thread repeatedly calls a function hydrogen(releaseHydrogen), and each oxygen thread repeatedly calls a function oxygen(releaseOxygen).
The releaseHydrogen() action emits/prints "H", and the releaseOxygen() action emits/prints "O".
Goal
Your primary goal is to design the synchronization logic such that the program outputs a valid sequence of water molecules (H2O). This means that for every molecule, exactly two 'H' actions and one 'O' action must be released together. Crucially, the three actions forming a single molecule must proceed as an atomic group, and these groups must not interleave with actions from other molecules.
Constraints
- Synchronization Primitives: You must use semaphores, locks, and/or condition variables. Busy-waiting is strictly prohibited.
- Safety: Ensure that no extra atoms are emitted, and every molecule formed releases exactly two 'H' atoms and one 'O' atom.
- Liveness: The system must be free from starvation, assuming that both hydrogen and oxygen atom types arrive infinitely often.
- Scalability: The design should handle an unbounded stream of incoming atoms without leading to unbounded resource growth (e.g., memory, thread pools).
Deliverables
- Provide pseudocode for the
hydrogenandoxygenfunctions, including any necessary supporting synchronization primitives and their initialization. - Offer a brief explanation detailing how your design ensures correctness with respect to safety, liveness, and scalability.
AI interview prep
Core dilemma
Coordinating multiple producer threads (H and O) to consume resources (atoms) in a specific ratio (2 H : 1 O) and release them atomically as a group, while ensuring liveness, fairness, and bounded resource usage in an unbounded stream.
3 tradeoffs and 2 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Design synchronization for water molecule assembly
Problem: Concurrency for Water Molecule Assembly
You are tasked with designing a synchronization mechanism for two types of concurrent threads: hydrogen threads and oxygen threads.
Context
Each hydrogen thread repeatedly calls a function hydrogen(releaseHydrogen), and each oxygen thread repeatedly calls a function oxygen(releaseOxygen).
The releaseHydrogen() action emits/prints "H", and the releaseOxygen() action emits/prints "O".
Goal
Your primary goal is to design the synchronization logic such that the program outputs a valid sequence of water molecules (H2O). This means that for every molecule, exactly two 'H' actions and one 'O' action must be released together. Crucially, the three actions forming a single molecule must proceed as an atomic group, and these groups must not interleave with actions from other molecules.
Constraints
- Synchronization Primitives: You must use semaphores, locks, and/or condition variables. Busy-waiting is strictly prohibited.
- Safety: Ensure that no extra atoms are emitted, and every molecule formed releases exactly two 'H' atoms and one 'O' atom.
- Liveness: The system must be free from starvation, assuming that both hydrogen and oxygen atom types arrive infinitely often.
- Scalability: The design should handle an unbounded stream of incoming atoms without leading to unbounded resource growth (e.g., memory, thread pools).
Deliverables
- Provide pseudocode for the
hydrogenandoxygenfunctions, including any necessary supporting synchronization primitives and their initialization. - Offer a brief explanation detailing how your design ensures correctness with respect to safety, liveness, and scalability.