Docsity

CS 234 Concurrent Programming module 3, Exams of Computer Science

Toggle sidebar
bg1
CS 234 Concurrent Programming module 3
1. Which of the following best describes the primary purpose of the Banker's
algorithm?
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.
2. What is the primary characteristic of a safe state in the context of deadlock
avoidance?
A) At least one process can complete without requiring additional resources
B) All processes are blocked waiting for resources
C) The system has no available resources
D) A circular wait exists among processes
Correct Answer: At least one process can complete without requiring additional
resources
Rationale: A safe state means there exists at least one sequence in which all
processes can complete without deadlocking. This is determined by checking if each
process's remaining resource needs can be satisfied by currently available
resources. An unsafe state does not guarantee deadlock but indicates the
possibility.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a

Partial preview of the text

Download CS 234 Concurrent Programming module 3 and more Exams Computer Science in PDF only on Docsity!

CS 234 Concurrent Programming module 3

  1. Which of the following best describes the primary purpose of the Banker's algorithm? 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.
  2. What is the primary characteristic of a safe state in the context of deadlock avoidance? A) At least one process can complete without requiring additional resources B) All processes are blocked waiting for resources C) The system has no available resources D) A circular wait exists among processes Correct Answer: At least one process can complete without requiring additional resources Rationale: A safe state means there exists at least one sequence in which all processes can complete without deadlocking. This is determined by checking if each process's remaining resource needs can be satisfied by currently available resources. An unsafe state does not guarantee deadlock but indicates the possibility.
  1. According to the module, which term refers to the situation where a set of processes are blocked because each is holding a resource and waiting for a resource held by another process? A) Starvation B) Livelock C) Deadlock D) Race condition Correct Answer: Deadlock Rationale: Deadlock occurs when a set of processes are permanently blocked, each waiting for a resource held by another in a circular chain. Starvation is indefinite postponement, livelock involves active but unproductive execution, and race conditions involve unpredictable timing. Deadlock is a permanent blocking condition.
  2. In the context of deadlock, what does the hold-and-wait condition refer to? A) Processes hold resources while waiting for additional resources B) Processes release all resources before requesting new ones C) Processes wait indefinitely without holding any resources D) Processes preempt resources from other processes Correct Answer: Processes hold resources while waiting for additional resources Rationale: The hold-and-wait condition is one of the four necessary conditions for deadlock. It occurs when a process holds at least one resource while waiting to acquire additional resources held by other processes. Releasing all resources before requesting new ones would prevent this condition.
  3. What is the most likely outcome if all four necessary conditions for deadlock are present? A) The system will always deadlock B) Deadlock may occur if the conditions persist

Rationale: Deadlock recovery by process termination involves aborting one or more processes involved in the deadlock to break the circular wait. Resource preemption takes resources from processes, rollback restores a previous state, and deadlock avoidance prevents deadlocks before they occur. Termination is a common recovery method.

  1. What is the primary function of a resource-allocation graph in deadlock detection? A) To show the allocation and request relationships between processes and resources B) To schedule processes for execution C) To prevent deadlocks from occurring D) To encrypt resource data Correct Answer: To show the allocation and request relationships between processes and resources Rationale: A resource-allocation graph visually represents which resources are allocated to which processes and which processes are requesting which resources. Cycles in the graph indicate potential deadlocks. It is a detection tool, not a prevention mechanism or scheduler.
  2. Which of the following best describes the circular wait condition for deadlock? A) Each process holds a resource and waits for a resource held by another process in a cycle B) Processes release resources before requesting new ones C) Processes can preempt resources from each other D) Resources are allocated in a linear order Correct Answer: Each process holds a resource and waits for a resource held by another process in a cycle Rationale: Circular wait occurs when a closed chain of processes exists, where each process holds at least one resource needed by the next process in the chain. This is

one of the four necessary conditions for deadlock. Releasing resources before requesting or allowing preemption would prevent it.

  1. What is the primary purpose of the wait-for graph in deadlock detection? A) To detect cycles indicating deadlock among processes B) To schedule processes in a fair order C) To allocate resources efficiently D) To prevent race conditions Correct Answer: To detect cycles indicating deadlock among processes Rationale: A wait-for graph is a directed graph where nodes are processes and edges indicate that one process is waiting for a resource held by another. A cycle in this graph indicates a deadlock. It is a detection tool, not a scheduling or allocation mechanism.
  2. In the context of the producer-consumer problem, what is the primary purpose of a semaphore? A) To coordinate access to the shared buffer and signal availability of space or items B) To terminate producer and consumer threads C) To encrypt data in the buffer D) To increase the speed of production Correct Answer: To coordinate access to the shared buffer and signal availability of space or items Rationale: Semaphores are used to synchronize producers and consumers by tracking available buffer slots (empty) and available items (full), while a mutex ensures mutual exclusion. They do not terminate threads, encrypt data, or directly increase production speed.
  3. What is the most likely outcome of using a counting semaphore with an initial value of 3 for a resource with 5 available instances?

Rationale: A mutex can only be unlocked by the thread that locked it, providing ownership semantics. A semaphore can be signaled by any thread, making it more flexible for signaling between threads. Both can provide mutual exclusion, but a mutex is stricter about ownership.

  1. In the context of process synchronization, what does the term "atomicity" refer to? A) An operation that executes as a single indivisible unit B) An operation that can be interrupted at any point C) An operation that requires multiple threads to complete D) An operation that permanently locks a resource Correct Answer: An operation that executes as a single indivisible unit Rationale: Atomicity means an operation appears to execute as a single, uninterruptible step from the perspective of other threads. It cannot be partially completed or interleaved with other operations. Atomic operations are fundamental to building synchronization primitives.
  2. What is the primary purpose of Peterson's solution? A) To provide a software-based mutual exclusion algorithm for two processes B) To implement a semaphore using hardware instructions C) To detect deadlocks in a concurrent system D) To schedule threads on a multi-core processor Correct Answer: To provide a software-based mutual exclusion algorithm for two processes 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.
  1. Which of the following best describes the critical section problem? A) The challenge of ensuring that only one process executes in a critical section at a time B) The challenge of increasing the number of processes in a critical section C) The challenge of eliminating all critical sections from a program D) The challenge of making critical sections execute faster Correct Answer: The challenge of ensuring that only one process executes in a critical section at a time Rationale: The critical section problem is to design a protocol that ensures mutual exclusion, progress, and bounded waiting when multiple processes access shared resources. It does not involve increasing process count, eliminating critical sections, or speeding up execution.
  2. In the context of the dining philosophers problem, what is the primary cause of deadlock if all philosophers pick up their left fork simultaneously? A) Circular wait B) Hold and wait C) No preemption D) Mutual exclusion Correct Answer: Circular wait Rationale: If all philosophers pick up their left fork, each holds one fork and waits for the right fork held by their neighbor, creating a circular chain of waiting. This is the circular wait condition. Hold and wait, no preemption, and mutual exclusion are also present but circular wait is the immediate cause.
  3. What is the most likely benefit of using a monitor over semaphores for synchronization? A) Monitors provide automatic mutual exclusion, reducing programmer error B) Monitors are more efficient in terms of memory usage

Rationale: Starvation occurs when a thread is indefinitely postponed from accessing a resource, even though the resource is available. Deadlock involves circular waiting, livelock involves active but unproductive execution, and race conditions involve unpredictable timing. Starvation is a fairness issue.

  1. What is the primary difference between deadlock and livelock? A) Deadlock threads are blocked, while livelock threads are active but make no progress B) Livelock threads are blocked, while deadlock threads are active C) Deadlock and livelock are the same D) Livelock occurs only in single-threaded programs Correct Answer: Deadlock threads are blocked, while livelock threads are active but make no progress Rationale: In deadlock, threads are blocked and cannot proceed. In livelock, threads are active and executing, but they repeatedly change state without making useful progress. Both are liveness failures, but the thread states differ. Livelock can occur in multi-threaded programs.
  2. Which of the following is a necessary condition for deadlock? A) Circular wait B) Mutual exclusion C) Hold and wait D) All of the above Correct Answer: All of the above Rationale: The four necessary conditions for deadlock are mutual exclusion, hold and wait, no preemption, and circular wait. All must be present simultaneously for deadlock to occur. Preventing any one condition prevents deadlock. Circular wait alone is not sufficient.
  3. What is the primary purpose of deadlock prevention?

A) To ensure that at least one of the necessary conditions for deadlock cannot occur B) To detect deadlocks after they occur C) To recover from deadlocks D) To ignore deadlocks Correct Answer: To ensure that at least one of the necessary conditions for deadlock cannot occur Rationale: Deadlock prevention techniques ensure that at least one of the four necessary conditions cannot hold, making deadlock impossible. Detection and recovery handle deadlocks after they occur. Prevention is proactive.

  1. What is the primary purpose of deadlock detection? A) To identify when a deadlock has occurred and take action B) To prevent deadlocks from occurring C) To recover from deadlocks automatically D) To ignore deadlocks Correct Answer: To identify when a deadlock has occurred and take action Rationale: Deadlock detection algorithms identify when a deadlock has occurred, often by detecting circular waits in a resource-allocation graph. Recovery mechanisms then break the deadlock. Detection does not prevent deadlocks, automatically recover, or ignore them.
  2. Which of the following is a method for deadlock recovery? A) Terminating one or more threads involved in the deadlock B) Ignoring the deadlock C) Preventing all threads from running D) Adding more resources Correct Answer: Terminating one or more threads involved in the deadlock
  1. What is the primary purpose of the tryLock method in ReentrantLock? A) To attempt to acquire the lock without blocking indefinitely B) To acquire the lock and block forever C) To release the lock D) To check if the lock is held Correct Answer: To attempt to acquire the lock without blocking indefinitely Rationale: tryLock attempts to acquire the lock and returns immediately with a boolean indicating success or failure, optionally waiting for a specified time. It does not block forever, release the lock, or just check status. It is useful for deadlock avoidance and responsive systems.
  2. What is the primary purpose of the lockInterruptibly method in ReentrantLock? A) To allow a thread to be interrupted while waiting for a lock B) To acquire the lock without interruption C) To release the lock D) To check if the lock is held Correct Answer: To allow a thread to be interrupted while waiting for a lock Rationale: lockInterruptibly allows a thread waiting for a lock to be interrupted, enabling it to respond to cancellation or timeout. It does not prevent interruption, release the lock, or check status. It is useful for responsive concurrent applications.
  3. What is the primary purpose of the Condition interface in Java? A) To provide a way for threads to wait and be signaled within a lock B) To define the type of data stored in a lock C) To count the number of threads waiting D) To create new locks

Correct Answer: To provide a way for threads to wait and be signaled within a lock Rationale: The Condition interface, used with ReentrantLock, provides await, signal, and signalAll methods, similar to monitor condition variables. It does not define data types, count threads, or create locks. Conditions enable fine-grained waiting and notification.

  1. What is the primary purpose of the await method on a Condition? A) To release the associated lock and wait until signaled B) To acquire the associated lock C) To signal other waiting threads D) To check the condition Correct Answer: To release the associated lock and wait until signaled Rationale: The await method on a Condition atomically releases the associated lock and blocks the calling thread until it is signaled or interrupted. It does not acquire locks, signal other threads, or merely check the condition. It is analogous to Object.wait.
  2. What is the primary purpose of the signal method on a Condition? A) To wake up one thread waiting on the condition B) To wake up all threads waiting on the condition C) To acquire the associated lock D) To release the associated lock Correct Answer: To wake up one thread waiting on the condition Rationale: The signal method wakes up one thread waiting on the condition, moving it to the lock's wait queue. signalAll wakes all waiting threads. Signal does not acquire or release the lock. It is used to notify a waiting thread.
  3. What is the primary purpose of the signalAll method on a Condition?

Rationale: ConcurrentHashMap is a thread-safe implementation of Map designed for concurrent access without external synchronization. ArrayList, HashMap, and LinkedList are not thread-safe and require external synchronization for concurrent use. Concurrent collections simplify concurrent programming.

  1. What is the primary purpose of the AtomicInteger class in Java? A) To provide atomic operations on an integer value B) To store large integers C) To provide a thread-safe integer list D) To synchronize threads Correct Answer: To provide atomic operations on an integer value Rationale: AtomicInteger provides atomic operations (like incrementAndGet, compareAndSet) on an integer value without using locks. It is not for large integers, thread-safe lists, or thread synchronization. Atomic classes are part of java.util.concurrent.atomic.
  2. What is the primary purpose of a thread-safe collection? A) To allow safe concurrent access by multiple threads without external synchronization B) To prevent all threads from accessing the collection C) To make collection operations faster D) To store only immutable objects Correct Answer: To allow safe concurrent access by multiple threads without external synchronization Rationale: A thread-safe collection ensures that its internal state remains consistent when accessed by multiple threads concurrently, without requiring external synchronization. It does not prevent all access, necessarily make operations faster, or require immutable objects. It simplifies concurrent programming.
  1. Which of the following is a common approach to making a class thread-safe? A) Using synchronization or locks B) Using only static variables C) Using only public fields D) Using recursion Correct Answer: Using synchronization or locks Rationale: Thread safety is typically achieved by using synchronization mechanisms (synchronized methods/blocks, locks) to protect shared mutable state. Static variables, public fields, and recursion do not inherently provide thread safety. Proper synchronization ensures data consistency.
  2. What is the primary purpose of the synchronized keyword in Java? A) To provide mutual exclusion for a method or block of code B) To make a variable constant C) To increase thread priority D) To create a new thread Correct Answer: To provide mutual exclusion for a method or block of code Rationale: The synchronized keyword ensures that only one thread 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.
  3. What is the primary difference between a synchronized method and a synchronized block? A) A synchronized method locks the entire object, while a synchronized block can lock a specific object B) A synchronized block locks the entire object, while a synchronized method locks a specific object C) They are exactly the same

Rationale: ReentrantLock provides a tryLock method that attempts to acquire the lock without blocking, allowing threads to proceed if the lock is unavailable. It is not always faster, requires explicit unlock, and is still a synchronization mechanism. This flexibility can prevent deadlocks.

  1. What is the primary purpose of the ReadWriteLock interface in Java? A) To allow multiple readers concurrent access while writers have exclusive access B) To allow multiple writers concurrent access while readers have exclusive access C) To eliminate all synchronization D) To provide a single lock for all operations Correct Answer: To allow multiple readers concurrent access while writers have exclusive access Rationale: ReadWriteLock allows multiple readers to access a resource concurrently, while writers require exclusive access. This improves performance for read-heavy workloads. It does not allow multiple writers, eliminate synchronization, or provide a single lock. It separates read and write locks.
  2. What is the primary purpose of a CountDownLatch in Java? A) To allow one or more threads to wait until a set of operations completes B) To count the number of threads created C) To provide mutual exclusion D) To store data permanently Correct Answer: To allow one or more threads to wait until a set of operations completes Rationale: CountDownLatch is a synchronization aid that allows threads to wait until a count reaches zero, typically representing completion of a set of operations. It is not for counting threads, providing mutual exclusion, or storing data. It is used for coordination.
  3. What is the primary purpose of a CyclicBarrier in Java?

A) To allow a set of threads to wait for each other at a common barrier point B) To provide mutual exclusion C) To store data permanently D) To create new threads Correct Answer: To allow a set of threads to wait for each other at a common barrier point Rationale: CyclicBarrier allows a fixed number of threads to wait for each other at a barrier point before proceeding, and can be reused. It is not for mutual exclusion, data storage, or thread creation. It is useful for parallel algorithms with phases.

  1. What is the primary purpose of a Semaphore in Java? A) To control access to a shared resource through permits B) To provide mutual exclusion only C) To create new threads D) To store data permanently Correct Answer: To control access to a shared resource through permits Rationale: A Semaphore maintains a set of permits; threads acquire permits before accessing a resource and release them afterward. It can control access to a pool of resources, not just mutual exclusion. It does not create threads or store data. It is more general than a mutex.
  2. What is the primary purpose of the Exchanger class in Java? A) To allow two threads to exchange objects at a synchronization point B) To exchange data with a database C) To provide mutual exclusion D) To create new threads