Docsity

CS 234 Concurrent Programming module 2, Exams of Computer Science

Toggle sidebar
bg1
CS 234 Concurrent Programming module 2
1. Which of the following best describes a race condition in concurrent
programming?
A) A situation where two threads block each other indefinitely
B) A situation where the result of a computation depends on the unpredictable
timing of operations
C) A situation where a thread is permanently denied access to a resource
D) A situation where a thread continuously retries an operation without success
Correct Answer: A situation where the result of a computation depends on the
unpredictable timing of operations
Rationale: A race condition occurs when multiple threads access shared data
concurrently and the final outcome depends on the order of execution. This
unpredictable timing can lead to data corruption and incorrect results. Deadlock
involves mutual blocking, starvation involves indefinite postponement, and livelock
involves active but unproductive retries.
2. What is the primary purpose of a critical section in concurrent programming?
A) To allow multiple threads to execute simultaneously
B) To protect shared resources from concurrent access by multiple threads
C) To increase the overall speed of the program
D) To store data permanently for later retrieval
Correct Answer: To protect shared resources from concurrent access by multiple
threads
Rationale: A critical section is a segment of code that accesses shared resources
and must be executed by only one thread at a time to prevent race conditions. It
does not permit simultaneous execution, increase speed, or store data. The critical
section enforces mutual exclusion.
3. According to the module, which term refers to the requirement that only one
thread can execute in a critical section at a time?
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 2 and more Exams Computer Science in PDF only on Docsity!

CS 234 Concurrent Programming module 2

  1. Which of the following best describes a race condition in concurrent programming? A) A situation where two threads block each other indefinitely B) A situation where the result of a computation depends on the unpredictable timing of operations C) A situation where a thread is permanently denied access to a resource D) A situation where a thread continuously retries an operation without success Correct Answer: A situation where the result of a computation depends on the unpredictable timing of operations Rationale: A race condition occurs when multiple threads access shared data concurrently and the final outcome depends on the order of execution. This unpredictable timing can lead to data corruption and incorrect results. Deadlock involves mutual blocking, starvation involves indefinite postponement, and livelock involves active but unproductive retries.
  2. What is the primary purpose of a critical section in concurrent programming? A) To allow multiple threads to execute simultaneously B) To protect shared resources from concurrent access by multiple threads C) To increase the overall speed of the program D) To store data permanently for later retrieval Correct Answer: To protect shared resources from concurrent access by multiple threads Rationale: A critical section is a segment of code that accesses shared resources and must be executed by only one thread at a time to prevent race conditions. It does not permit simultaneous execution, increase speed, or store data. The critical section enforces mutual exclusion.
  3. According to the module, which term refers to the requirement that only one thread can execute in a critical section at a time?

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.

  1. In the context of concurrent programming, what does the term "atomic operation" mean? A) An operation that can be interrupted at any point B) An operation that executes as a single indivisible unit 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: An atomic operation is one that 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 most likely outcome if a critical section is not properly protected? A) The program will always produce correct results B) Race conditions may occur, leading to data corruption C) The program will automatically synchronize the threads D) The critical section will become atomic

condition variable allows threads to wait for conditions. Semaphores are versatile synchronization primitives.

  1. What is the primary function of the wait (P) operation on a semaphore? A) To increment the semaphore value and wake waiting threads B) To decrement the semaphore value and block if the value becomes negative C) To reset the semaphore to its initial value D) To query the current value of the semaphore Correct Answer: To decrement the semaphore value and block if the value becomes negative Rationale: The wait (P) operation decrements the semaphore's integer value and blocks the calling thread if the result is negative, indicating the resource is unavailable. The signal (V) operation increments the value and wakes waiting threads. Wait does not reset or query the semaphore value.
  2. What is the primary function of the signal (V) operation on a semaphore? A) To decrement the semaphore value and block the calling thread B) To increment the semaphore value and potentially wake a waiting thread C) To reset the semaphore to zero D) To terminate all threads waiting on the semaphore Correct Answer: To increment the semaphore value and potentially wake a waiting thread Rationale: The signal (V) operation increments the semaphore's value and, if threads are waiting, wakes one of them. The wait (P) operation decrements and potentially blocks. Signal does not reset the semaphore or terminate threads. It releases a resource permit.
  3. Which of the following best describes a binary semaphore? A) A semaphore whose value can be any positive integer

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.

  1. What is the primary difference between a counting semaphore and a binary semaphore? A) A counting semaphore can have any non-negative integer value, while a binary semaphore is limited to 0 or 1 B) A binary semaphore can have any integer value, while a counting semaphore is limited to 0 or 1 C) A counting semaphore does not block, while a binary semaphore does D) There is no difference between them Correct Answer: A counting semaphore can have any non-negative integer value, while a binary semaphore is limited to 0 or 1 Rationale: A counting semaphore can represent multiple resources with an integer value, while a binary semaphore is restricted to 0 or 1, functioning as a lock. Both can block threads. The distinction is in the range of values and the number of resources they can manage.
  2. Which of the following is a classic synchronization problem that involves a shared buffer of limited size? A) The dining philosophers problem B) The readers-writers problem C) The producer-consumer problem D) The sleeping barber problem

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.

  1. Which of the following best describes the dining philosophers problem? A) A problem where philosophers must share a limited number of forks to eat B) A problem where philosophers must sort a list of numbers C) A problem where philosophers must communicate over a network D) A problem where philosophers must calculate prime numbers Correct Answer: A problem where philosophers must share a limited number of forks to eat Rationale: The dining philosophers problem models philosophers who alternate between thinking and eating, needing two forks to eat, with one fork between each pair. It illustrates synchronization challenges like deadlock and starvation. It is not about sorting, networking, or prime numbers.
  2. What is the primary risk in the dining philosophers problem if all philosophers pick up their left fork simultaneously? A) Deadlock B) Livelock C) Starvation D) Race condition Correct Answer: Deadlock Rationale: If all philosophers simultaneously pick up their left fork, each holds one fork and waits indefinitely for the right fork, creating a circular wait and deadlock. Livelock involves active but unproductive movement, starvation is indefinite postponement, and race condition involves unpredictable timing.
  1. Which of the following is a classic 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 producer-consumer problem involves buffer coordination, dining philosophers involves fork sharing, and sleeping barber involves customer service. Each illustrates different synchronization issues.
  2. In the readers-writers problem, what is the primary constraint for writers? A) Writers can access the resource simultaneously with other writers B) Writers require exclusive access to the resource C) Writers must wait for all readers to finish before writing D) Writers can only write when no readers are waiting Correct Answer: Writers require exclusive access to the resource Rationale: In the readers-writers problem, writers need exclusive access, meaning no other readers or writers can access the resource simultaneously. Readers can share access with other readers. The challenge is balancing reader concurrency with writer exclusivity while preventing starvation.
  3. Which of the following best describes a monitor in concurrent programming? A) A low-level synchronization primitive based on integer variables B) A high-level synchronization construct that encapsulates shared data and operations C) A type of hardware device for monitoring system performance

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.

  1. What is the primary purpose of the wait operation on a condition variable? A) To release the associated lock and block until signaled B) To acquire the associated lock C) To wake up other waiting threads D) To check the condition without blocking Correct Answer: To release the associated lock and block until signaled Rationale: The wait operation atomically releases the associated lock and blocks the calling thread until another thread signals the condition variable. It does not acquire locks, wake other threads, or merely check the condition. When signaled, wait reacquires the lock before returning.
  2. What is the primary purpose of the signal operation on a condition variable? A) To wake up one waiting thread B) To wake up all waiting threads C) To acquire a lock D) To release a lock Correct Answer: To wake up one waiting thread Rationale: The signal operation on a condition variable wakes up at least one thread waiting on that condition. The broadcast operation wakes all waiting threads. Signal does not acquire or release locks. It is used to notify a waiting thread that a condition may now be true.
  3. What is the primary purpose of the broadcast (signalAll) operation on a condition variable?

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.

  1. Which of the following best describes a mutex? A) A synchronization primitive that provides mutual exclusion B) A type of semaphore that counts resources C) A condition variable for signaling D) A thread scheduling algorithm Correct Answer: A synchronization primitive that provides mutual exclusion Rationale: A mutex (mutual exclusion) is a lock that ensures only one thread can access a critical section at a time. It is a binary semaphore-like construct. It is not a counting semaphore, condition variable, or scheduling algorithm. Mutexes are widely used for protecting shared data.
  2. What is the primary difference between a mutex and a binary semaphore? A) A mutex can only be unlocked by the thread that locked it B) A binary semaphore can only be unlocked by the thread that locked it C) A mutex can hold values greater than one D) A binary semaphore does not provide mutual exclusion Correct Answer: A mutex can only be unlocked by the thread that locked it

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.

  1. What is the primary advantage of a blocking lock over a spinlock? A) Blocking locks are always faster B) Blocking locks free the CPU for other threads while waiting C) Blocking locks do not require any synchronization D) Blocking locks can be acquired by multiple threads Correct Answer: Blocking locks free the CPU for other threads while waiting Rationale: A blocking lock puts the waiting thread to sleep, allowing the CPU to execute other threads, whereas a spinlock busy-waits and consumes CPU cycles. Blocking locks are not always faster, still require synchronization, and are mutually exclusive. They are preferable when wait times may be long.
  2. What is the primary purpose of the Test-and-Set instruction? A) To atomically test a value and set it to a new value B) To test a condition and branch C) To set a variable without testing D) To compare two values

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.

  1. Which hardware instruction is commonly used to implement locks? A) Compare-and-Swap (CAS) B) Add C) Subtract D) Jump Correct Answer: Compare-and-Swap (CAS) Rationale: Compare-and-Swap (CAS) atomically compares a memory location's value to an expected value and, if equal, swaps it with a new value. It is widely used to implement lock-free data structures and locks. Add, subtract, and jump are not used for lock implementation.
  2. What is the primary benefit of lock-free data structures? A) They avoid the problems associated with locks, such as deadlock B) They are always faster than lock-based structures C) They eliminate all synchronization D) They use less memory Correct Answer: They avoid the problems associated with locks, such as deadlock Rationale: Lock-free data structures use atomic operations (like CAS) instead of locks, avoiding deadlock, priority inversion, and other lock-related issues. They are not always faster, still require synchronization, and may use more memory. They provide progress guarantees.

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.

  1. Which of the following is a common memory consistency model? A) Sequential consistency B) Random consistency C) Alphabetical consistency D) Circular consistency Correct Answer: Sequential consistency Rationale: Sequential consistency is a memory consistency model where all memory operations appear to execute in a single global order, preserving program order for each thread. Random, alphabetical, and circular consistency are not standard models. Sequential consistency is intuitive but can be expensive to implement.
  2. What is the primary purpose of Peterson's algorithm? A) To provide a software solution for mutual exclusion between 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 solution for mutual exclusion between 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 Dekker's algorithm? A) A hardware-based solution for mutual exclusion B) The first known software solution for mutual exclusion between two processes C) A method for detecting race conditions D) A technique for optimizing semaphore operations Correct Answer: The first known software solution for mutual exclusion between two processes Rationale: Dekker's algorithm is historically the first correct software solution to the mutual exclusion problem for two processes. It uses shared variables and busy waiting. It is not hardware-based, does not detect race conditions, and is not an optimization technique. Peterson's algorithm is a simpler successor.
  2. What is the primary difference between Peterson's algorithm and Dekker's algorithm? A) Peterson's algorithm is simpler and more elegant B) Dekker's algorithm works for more than two processes C) Peterson's algorithm requires hardware support D) Dekker's algorithm is not correct Correct Answer: Peterson's algorithm is simpler and more elegant Rationale: Peterson's algorithm is a simpler and more elegant software solution for two-process mutual exclusion compared to Dekker's algorithm. Both are correct software solutions for two processes. Neither requires hardware support, and neither works for more than two processes without generalization.
  3. What is the primary purpose of the compareAndSet method in atomic classes?

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.

  1. Which of the following best describes a future in concurrent programming? A) A placeholder for a result that will be available later B) A thread that has already completed execution C) A synchronization primitive for mutual exclusion D) A type of lock Correct Answer: A placeholder for a result that will be available later Rationale: A future represents the result of an asynchronous computation that may not yet be complete. It allows the caller to retrieve the result when ready. It is not a completed thread, synchronization primitive, or lock. Futures are common in concurrent frameworks.
  2. What is the primary purpose of the fork-join framework? A) To divide a task into subtasks and combine their results B) To create a new process for each task C) To eliminate the need for synchronization D) To store data in a shared buffer Correct Answer: To divide a task into subtasks and combine their results Rationale: The fork-join framework recursively divides a task into smaller subtasks that can be executed in parallel, then combines their results. It does not create processes, eliminate synchronization, or store data in buffers. It is designed for divide-and-conquer algorithms.
  1. Which of the following is a potential consequence of improper synchronization? A) Race conditions and data corruption B) Automatic error correction C) Increased program simplicity D) Elimination of all concurrency issues Correct Answer: Race conditions and data corruption Rationale: Improper synchronization can lead to race conditions, where shared data is accessed concurrently without proper protection, causing data corruption and unpredictable behavior. It does not lead to automatic correction, increased simplicity, or elimination of issues. Proper synchronization is essential.
  2. What is the primary difference between deadlock and starvation? A) Deadlock involves circular waiting, while starvation involves indefinite postponement B) Starvation involves circular waiting, while deadlock involves indefinite postponement C) Deadlock and starvation are the same thing D) Deadlock occurs only in message-passing systems Correct Answer: Deadlock involves circular waiting, while starvation involves indefinite postponement Rationale: Deadlock occurs when threads wait indefinitely for each other in a circular dependency. Starvation occurs when a thread is perpetually denied access to a resource it needs, even though the resource is available. Both are liveness failures but have different causes.
  3. Which of the following is a necessary condition for deadlock? A) Circular wait B) Mutual exclusion C) Hold and wait