


































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



































CS 234 Concurrent Programming module 2
A) Atomicity B) Mutual exclusion C) Synchronization D) Concurrency Correct Answer: Mutual exclusion Rationale: Mutual exclusion is the property that ensures only one thread can be inside a critical section at any given moment. Atomicity refers to an operation that appears indivisible, synchronization is the broader coordination of threads, and concurrency is the simultaneous execution of tasks. Mutual exclusion prevents race conditions.
condition variable allows threads to wait for conditions. Semaphores are versatile synchronization primitives.
B) A semaphore whose value is restricted to 0 or 1 C) A semaphore that never blocks D) A semaphore that can only be used once Correct Answer: A semaphore whose value is restricted to 0 or 1 Rationale: A binary semaphore, also called a mutex, has a value that can only be 0 or 1, making it suitable for mutual exclusion. A counting semaphore can have any non-negative integer value. Binary semaphores do block and can be used multiple times. They are essential for protecting critical sections.
Rationale: A consumer must wait if the buffer is empty, as there is nothing to remove. The buffer being full or having producers waiting is not the consumer's condition. Consumers remove items when at least one item is available. This coordination prevents busy-waiting and race conditions.
Rationale: Condition variables within monitors allow threads to wait for a condition and be signaled when it becomes true. They do not store results, define data types, or count threads. Operations typically include wait, signal, and broadcast. They enable condition synchronization.
A) To wake up all threads waiting on the condition B) To wake up one thread waiting on the condition C) To acquire the associated lock D) To release the associated lock Correct Answer: To wake up all threads waiting on the condition Rationale: The broadcast operation wakes up all threads waiting on the condition, allowing them to reacquire the lock and recheck their conditions. It does not wake only one thread (signal), acquire the lock, or release the lock. It is useful when multiple threads may be able to proceed.
A) A lock where a waiting thread repeatedly checks the lock's availability B) A lock that blocks the waiting thread and puts it to sleep C) A lock that can be acquired by multiple threads simultaneously D) A lock that never releases Correct Answer: A lock where a waiting thread repeatedly checks the lock's availability Rationale: A spinlock is a busy-waiting lock where the thread repeatedly polls the lock until it becomes available. It does not block or sleep the thread. Spinlocks are useful when wait times are expected to be short and can waste CPU cycles. They are not multi-acquire or permanent locks.
Correct Answer: To atomically test a value and set it to a new value Rationale: The Test-and-Set instruction atomically reads a memory location, sets it to a new value, and returns the old value. It is used to implement spinlocks and other synchronization primitives. It does not branch, set without testing, or compare two values.
C) To synchronize threads D) To protect memory from unauthorized access Correct Answer: To enforce ordering constraints on memory operations Rationale: A memory barrier (fence) ensures that memory operations before the barrier are completed before operations after the barrier, preventing reordering by the compiler or CPU. It does not increase capacity, synchronize threads by itself, or protect memory. Barriers are used in low-level synchronization.
Rationale: Peterson's algorithm is a classic software-based mutual exclusion algorithm for two processes, using shared variables (turn and flag array). It does not implement semaphores, detect deadlocks, or schedule threads. It demonstrates a correct software solution without hardware support.
Correct Answer: To reuse a fixed number of threads for executing multiple tasks Rationale: A thread pool maintains a set of worker threads that can be reused to execute tasks, reducing the overhead of thread creation and destruction. It does not create a new thread per task, eliminate synchronization, or store results. Thread pools improve efficiency.