Docsity

CS 234 Concurrent Programming module 4, Exams of Computer Science

Toggle sidebar
bg1
CS 234 Concurrent Programming module 4
1. Which of the following best describes a deadlock in a concurrent system?
A) A situation where a task runs faster than expected
B) A situation where two or more tasks are blocked forever, each waiting for a
resource held by another task in the cycle
C) A situation where a task is permanently denied access to a resource even though
the resource is available
D) A situation where a task continuously retries an operation without success
Correct Answer: A situation where two or more tasks are blocked forever, each
waiting for a resource held by another task in the cycle
Rationale: A deadlock occurs when two or more tasks are blocked indefinitely, each
waiting for a resource that is held by another task in a circular chain. This creates a
permanent blocking condition where no task can proceed. Starvation involves
indefinite postponement, and livelock involves active but unproductive execution.
Deadlock is a liveness failure.
2. What is the primary purpose of synchronization in multi-tasking systems?
A) To increase the execution speed of individual tasks
B) To ensure orderly access to shared resources and prevent data corruption
C) To eliminate the need for task scheduling
D) To reduce the total number of tasks in the system
Correct Answer: To ensure orderly access to shared resources and prevent data
corruption
Rationale: Synchronization is essential in multi-tasking systems to manage
concurrent access to shared resources such as global variables, sensor interfaces,
or data structures. Without proper synchronization, concurrent access can lead to
data corruption, inconsistencies, or unexpected system behavior. It does not
increase individual task speed, eliminate scheduling, or reduce task count.
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
pf2b

Partial preview of the text

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

CS 234 Concurrent Programming module 4

  1. Which of the following best describes a deadlock in a concurrent system? A) A situation where a task runs faster than expected B) A situation where two or more tasks are blocked forever, each waiting for a resource held by another task in the cycle C) A situation where a task is permanently denied access to a resource even though the resource is available D) A situation where a task continuously retries an operation without success Correct Answer: A situation where two or more tasks are blocked forever, each waiting for a resource held by another task in the cycle Rationale: A deadlock occurs when two or more tasks are blocked indefinitely, each waiting for a resource that is held by another task in a circular chain. This creates a permanent blocking condition where no task can proceed. Starvation involves indefinite postponement, and livelock involves active but unproductive execution. Deadlock is a liveness failure.
  2. What is the primary purpose of synchronization in multi-tasking systems? A) To increase the execution speed of individual tasks B) To ensure orderly access to shared resources and prevent data corruption C) To eliminate the need for task scheduling D) To reduce the total number of tasks in the system Correct Answer: To ensure orderly access to shared resources and prevent data corruption Rationale: Synchronization is essential in multi-tasking systems to manage concurrent access to shared resources such as global variables, sensor interfaces, or data structures. Without proper synchronization, concurrent access can lead to data corruption, inconsistencies, or unexpected system behavior. It does not increase individual task speed, eliminate scheduling, or reduce task count.
  1. According to the module, which term refers to a situation where the outcome of a process depends on the unpredictable sequence of execution of concurrent tasks? A) Deadlock B) Starvation C) Race condition D) Priority inversion Correct Answer: Race condition Rationale: A race condition occurs when the outcome of a process depends on the unpredictable sequence of execution of concurrent tasks. For example, if two tasks read, modify, and write back a shared counter value at nearly the same time, one update could be lost, resulting in an incorrect final value. Deadlock involves circular waiting, starvation involves indefinite postponement, and priority inversion involves priority conflicts.
  2. In the context of synchronization, what does mutual exclusion (mutex) ensure? A) That all tasks can access a shared resource simultaneously B) That only one task can access a shared resource at a time C) That tasks are executed in a specific order D) That no task can ever access a shared resource Correct Answer: That only one task can access a shared resource at a time Rationale: Mutual exclusion, or mutex, is a mechanism that ensures only one task can access a shared resource at a time, preventing race conditions. It acts as a "key" to a resource: a task must first acquire the key, and any other task needing the resource must wait. It does not permit simultaneous access, enforce execution order, or prevent all access.
  3. Which of the following best describes a mutex in FreeRTOS? A) A counter that controls access to a number of resources B) A "key" to a resource that a task must take before accessing it and release when finished

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.

  1. What is the primary purpose of a counting semaphore? A) To provide mutual exclusion for a single resource B) To manage access to a pool of identical resources C) To signal events between two tasks D) To prevent deadlocks automatically Correct Answer: To manage access to a pool of identical resources Rationale: A counting semaphore has a count value greater than 1 and is useful for managing access to a pool of identical resources, such as connections to a server, memory buffers, or slots in a pool. A binary semaphore is used for signaling or single-resource mutual exclusion. Counting semaphores do not automatically prevent deadlocks.
  2. In the context of deadlock, what does the mutual exclusion condition refer to? A) At least one resource must be non-sharable and held by only one task at a time B) A task must be holding at least one resource while waiting for additional resources C) Resources cannot be forcibly taken away from a task D) A circular chain of tasks exists where each waits for a resource held by the next Correct Answer: At least one resource must be non-sharable and held by only one task at a time

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.

  1. What is the hold and wait condition for deadlock? A) Tasks must release all resources before requesting new ones B) A task is holding at least one resource while waiting to acquire additional resources held by other tasks C) Resources can be preempted from tasks at any time D) A circular chain of waiting tasks exists Correct Answer: A task is holding at least one resource while waiting to acquire additional resources held by other tasks Rationale: The hold and wait condition occurs when a task holds at least one resource while waiting for additional resources held by other tasks. This is one of the four necessary conditions for deadlock. Releasing all resources before requesting new ones would prevent this condition. Preemption and circular wait are separate conditions.
  2. What is the most likely outcome if all four Coffman conditions are present simultaneously? A) Deadlock is impossible B) Deadlock may occur if the circular wait persists C) The system will automatically recover from deadlock D) All tasks will complete successfully Correct Answer: Deadlock may occur if the circular wait persists Rationale: The four Coffman conditions (mutual exclusion, hold and wait, no preemption, circular wait) must all be present for deadlock to occur. However, their presence does not guarantee deadlock; it only indicates the possibility. Deadlock

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.

  1. What is the primary characteristic of a safe state in deadlock avoidance? A) All tasks are blocked waiting for resources B) At least one task can complete without requiring additional resources C) A circular wait exists among tasks D) The system has no available resources Correct Answer: At least one task can complete without requiring additional resources Rationale: A safe state means there exists at least one sequence in which all tasks can complete without deadlocking. This is determined by checking if each task's remaining resource needs can be satisfied by currently available resources. An unsafe state does not guarantee deadlock but indicates the possibility. A safe state ensures that at least one task can proceed to completion.
  2. Which of the following best describes deadlock detection? A) Ensuring that at least one necessary condition for deadlock cannot occur B) Identifying when a deadlock has occurred by detecting circular waits C) Preventing deadlocks by acquiring locks in a consistent order D) Recovering from deadlocks by terminating tasks

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.

  1. What is a resource-allocation graph used for in deadlock detection? A) To schedule processes for execution B) To show the allocation and request relationships between processes and resources 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. It helps identify whether a deadlock exists.
  2. Which of the following is a method for deadlock recovery? A) Terminating one or more tasks involved in the deadlock B) Ignoring the deadlock C) Preventing all tasks from running D) Adding more resources without analysis Correct Answer: Terminating one or more tasks involved in the deadlock

progress. Both are liveness failures, but the task states differ. Livelock can occur in multi-threaded programs. Deadlock involves permanent blocking.

  1. According to the module, which term refers to a situation where a task is perpetually denied access to a resource it needs even though the resource is available? A) Deadlock B) Starvation C) Livelock D) Race condition Correct Answer: Starvation Rationale: Starvation occurs when a task 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 that can occur when scheduling or synchronization policies are unfair.
  2. What is the primary purpose of a lock timeout in deadlock avoidance? A) To prevent a task from waiting indefinitely for a lock B) To make locks expire automatically after a fixed time C) To increase lock performance D) To create new locks Correct Answer: To prevent a task from waiting indefinitely for a lock Rationale: A lock timeout allows a task to give up waiting for a lock after a specified period, preventing indefinite blocking and potential deadlock. It does not make locks expire automatically, increase performance, or create locks. Timeouts are a deadlock avoidance technique that allows tasks to recover from potential deadlocks.
  3. What is the primary purpose of lock ordering in deadlock prevention?

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.

  1. Which of the following is a common technique for avoiding deadlock in concurrent programming? A) Acquiring locks in a consistent global order B) Acquiring locks in random order C) Holding locks indefinitely D) Never releasing locks Correct Answer: Acquiring locks in a consistent global order Rationale: Acquiring locks in a consistent global order prevents circular wait, one of the necessary conditions for deadlock. Random lock ordering can lead to deadlock, holding locks indefinitely is not avoidance, and never releasing locks is not a technique. Consistent ordering is a standard approach to deadlock prevention.
  2. What is the primary purpose of the tryLock method in Java's 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
  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 tasks 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 task until it is signaled or interrupted. It does not acquire locks, signal other tasks, or merely check the condition. It is analogous to Object.wait in Java.
  2. What is the primary purpose of the signal method on a Condition? A) To wake up one task waiting on the condition B) To wake up all tasks waiting on the condition C) To acquire the associated lock D) To release the associated lock Correct Answer: To wake up one task waiting on the condition Rationale: The signal method wakes up one task waiting on the condition, moving it to the lock's wait queue. signalAll wakes all waiting tasks. Signal does not acquire or release the lock. It is used to notify a waiting task that a condition may now be true.
  3. What is the primary purpose of the signalAll method on a Condition? A) To wake up all tasks waiting on the condition B) To wake up one task waiting on the condition C) To acquire the associated lock D) To release the associated lock

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.

  1. In the context of the producer-consumer problem, what is the primary synchronization challenge? A) Ensuring producers never produce items B) Coordinating access to a shared buffer with limited capacity C) Eliminating the need for a buffer D) Preventing consumers from consuming items Correct Answer: Coordinating access to a shared buffer with limited capacity Rationale: The producer-consumer problem involves a shared buffer of finite size. Producers must wait when the buffer is full, and consumers must wait when it is empty, while both must access the buffer mutually exclusively. It is not about preventing production or consumption entirely or removing the buffer.
  2. Which of the following is a classic synchronization problem involving multiple readers and writers accessing a shared resource? A) The readers-writers problem B) The producer-consumer problem C) The dining philosophers problem D) The sleeping barber problem Correct Answer: The readers-writers problem Rationale: The readers-writers problem deals with multiple readers who can access a shared resource concurrently and writers who require exclusive access. The

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.

  1. What is the primary function of the signal (V) operation on a semaphore? A) To decrement the semaphore's value and block the calling task B) To increment the semaphore's value and potentially wake a waiting task C) To reset the semaphore's value to its initial state D) To terminate all tasks waiting on the semaphore Correct Answer: To increment the semaphore's value and potentially wake a waiting task Rationale: The signal (V) operation increments the semaphore's value and, if tasks are waiting, wakes one of them. The wait (P) operation decrements and potentially blocks. Signal does not reset or terminate tasks. It is used to release resources.
  2. What is the primary purpose of the java.util.concurrent package in Java? A) To provide high-level concurrency utilities B) To replace all synchronization primitives C) To eliminate the need for threads D) To provide database connectivity

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.

  1. Which of the following is a concurrent collection in Java? A) ConcurrentHashMap B) ArrayList C) HashMap D) LinkedList Correct Answer: ConcurrentHashMap 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.
  2. 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.

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.

  1. 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 D) Synchronized methods are always faster Correct Answer: A synchronized method locks the entire object, while a synchronized block can lock a specific object Rationale: A synchronized method locks the object on which it is invoked (or the Class object for static methods). A synchronized block allows specifying which object to lock, providing finer-grained control. Synchronized blocks can reduce contention and improve performance.
  2. What is the primary purpose of the ReentrantLock class in Java? A) To provide a more flexible alternative to synchronized blocks B) To replace all synchronization C) To create new threads D) To store data permanently Correct Answer: To provide a more flexible alternative to synchronized blocks

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.

  1. Which of the following is a potential advantage of ReentrantLock over synchronized? A) It supports non-blocking tryLock attempts B) It is always faster C) It does not require unlocking D) It can be used without any synchronization Correct Answer: It supports non-blocking tryLock attempts Rationale: ReentrantLock provides a tryLock method that attempts to acquire the lock without blocking, allowing tasks 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.
  2. 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.
  3. What is the primary purpose of a CountDownLatch in Java?