



































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 4
Correct Answer: A semaphore with a maximum count of 1, often used for signaling between tasks Rationale: A binary semaphore has a maximum count of 1 (it can be either 0 or 1). It is often used for signaling between tasks (event synchronization) rather than for pure mutual exclusion, as it lacks a priority inheritance mechanism. A counting semaphore has a count value greater than 1. Binary semaphores do not provide priority inheritance.
Rationale: Mutual exclusion is one of the four necessary conditions for deadlock. It requires that at least one resource must be non-sharable, meaning only one task can use it at a time. Hold and wait refers to holding resources while waiting, no preemption means resources cannot be forcibly taken, and circular wait involves a circular chain of waiting tasks.
A) To detect deadlocks after they have occurred B) To avoid deadlocks by ensuring the system remains in a safe state C) To prevent deadlocks by breaking the circular wait condition D) To recover from deadlocks by terminating processes Correct Answer: To avoid deadlocks by ensuring the system remains in a safe state Rationale: The Banker's algorithm is a deadlock avoidance technique that checks whether granting a resource request will leave the system in a safe state. If a safe sequence exists, the request is granted; otherwise, it is denied. Detection identifies existing deadlocks, prevention breaks necessary conditions, and recovery terminates processes.
Correct Answer: Identifying when a deadlock has occurred by detecting circular waits Rationale: Deadlock detection algorithms identify when a deadlock has occurred, often by detecting circular waits in a resource-allocation graph or wait-for graph. Recovery mechanisms then break the deadlock. Detection does not prevent deadlocks or automatically recover from them. It is a reactive approach.
progress. Both are liveness failures, but the task states differ. Livelock can occur in multi-threaded programs. Deadlock involves permanent blocking.
A) To prevent deadlock by ensuring locks are acquired in a consistent global order B) To increase lock performance C) To create new locks D) To eliminate all synchronization Correct Answer: To prevent deadlock by ensuring locks are acquired in a consistent global order Rationale: Lock ordering prevents deadlock by ensuring all tasks acquire locks in the same predefined order, eliminating circular wait. It does not increase performance, create locks, or eliminate synchronization. It is a simple and effective deadlock prevention technique that breaks the circular wait condition.
Correct Answer: To wake up all tasks waiting on the condition Rationale: The signalAll method wakes up all tasks waiting on the condition, allowing them to reacquire the lock and recheck their conditions. It does not wake only one task (signal), acquire the lock, or release the lock. It is useful when multiple tasks may be able to proceed.
A) To increment the semaphore's value and signal waiting tasks B) To decrement the semaphore's value and block if the result is negative C) To reset the semaphore's value to zero D) To check the semaphore's value without modifying it Correct Answer: To decrement the semaphore's value and block if the result is negative Rationale: The wait (P) operation decrements the semaphore's value and blocks the calling task if the value becomes negative, indicating the resource is unavailable. The signal (V) operation increments the value and wakes waiting tasks. Wait does not reset or merely check the value.
Correct Answer: To provide high-level concurrency utilities Rationale: The java.util.concurrent package provides high-level concurrency utilities such as thread pools, concurrent collections, and synchronization aids. It does not replace all primitives, eliminate threads, or provide database connectivity. It simplifies concurrent programming in Java.
Correct Answer: To provide mutual exclusion for a method or block of code Rationale: The synchronized keyword ensures that only one task can execute a synchronized method or block at a time, providing mutual exclusion. It does not make variables constant, increase priority, or create threads. Synchronized is Java's built-in locking mechanism.
Rationale: ReentrantLock provides explicit lock acquisition and release, with features like tryLock, timed lock, and fairness policies. It is more flexible than synchronized but requires manual unlock. It does not replace all synchronization, create threads, or store data.