Docsity

CS 234 Concurrent Programming module 1, Exams of Computer Science

Toggle sidebar
bg1
CS 234 Concurrent Programming module 1
1. Which of the following best describes the primary goal of concurrent
programming?
A) To execute instructions in a strictly sequential order
B) To allow multiple tasks to make progress simultaneously
C) To reduce the total number of instructions in a program
D) To eliminate the need for synchronization mechanisms
Correct Answer: To allow multiple tasks to make progress simultaneously
Rationale: Concurrent programming enables multiple tasks or threads to execute in
overlapping time periods, improving resource utilization and responsiveness. It does
not enforce sequential execution, reduce instruction count, or remove the need for
synchronization. The goal is to manage simultaneous progress correctly.
2. What is the primary distinction between a process and a thread?
A) A process shares memory with other processes, while a thread has its own
isolated memory
B) A process has its own memory space, while threads within a process share
memory
C) A thread can contain multiple processes, while a process contains a single thread
D) A process is lighter weight than a thread and faster to create
Correct Answer: A process has its own memory space, while threads within a
process share memory
Rationale: A process is an independent execution unit with its own memory space,
while threads within the same process share the process's memory and resources.
Threads are lighter weight and faster to create than processes. A process can
contain multiple threads, not the reverse.
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 1 and more Exams Computer Science in PDF only on Docsity!

CS 234 Concurrent Programming module 1

  1. Which of the following best describes the primary goal of concurrent programming? A) To execute instructions in a strictly sequential order B) To allow multiple tasks to make progress simultaneously C) To reduce the total number of instructions in a program D) To eliminate the need for synchronization mechanisms Correct Answer: To allow multiple tasks to make progress simultaneously Rationale: Concurrent programming enables multiple tasks or threads to execute in overlapping time periods, improving resource utilization and responsiveness. It does not enforce sequential execution, reduce instruction count, or remove the need for synchronization. The goal is to manage simultaneous progress correctly.
  2. What is the primary distinction between a process and a thread? A) A process shares memory with other processes, while a thread has its own isolated memory B) A process has its own memory space, while threads within a process share memory C) A thread can contain multiple processes, while a process contains a single thread D) A process is lighter weight than a thread and faster to create Correct Answer: A process has its own memory space, while threads within a process share memory Rationale: A process is an independent execution unit with its own memory space, while threads within the same process share the process's memory and resources. Threads are lighter weight and faster to create than processes. A process can contain multiple threads, not the reverse.
  1. According to the module, which term refers to a situation where the result of a computation depends on the unpredictable timing or interleaving of concurrent operations? A) Deadlock B) Race condition C) Livelock D) Starvation Correct Answer: Race condition Rationale: A race condition occurs when multiple threads access shared data concurrently and the outcome depends on the order of execution. Deadlock involves threads waiting indefinitely for each other, livelock involves threads active but making no progress, and starvation occurs when a thread is perpetually denied access.
  2. In the context of concurrent programming, what is a critical section? A) A section of code that can be executed by multiple threads simultaneously B) A section of code that must be executed by only one thread at a time C) A section of code that never accesses shared data D) A section of code that cannot be interrupted by the operating system Correct Answer: A section of code that must be executed by only one thread at a time Rationale: A critical section is a portion of code that accesses shared resources and must be executed atomically to prevent race conditions. It requires mutual exclusion, meaning only one thread can execute it at a time. Critical sections do not inherently involve simultaneous execution or non-interruptibility.
  3. What is the primary purpose of mutual exclusion in concurrent programming? A) To allow multiple threads to execute critical sections simultaneously B) To ensure that only one thread can execute a critical section at a time

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 thread if the value becomes negative, indicating the resource is unavailable. The signal (V) operation increments the value and wakes waiting threads. 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 thread B) To increment the semaphore's value and potentially wake a waiting thread C) To reset the semaphore's value to its initial state D) To terminate all threads waiting on the semaphore Correct Answer: To increment the semaphore's 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 or terminate threads. It is used to release resources.
  2. 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 D) A loop that continuously checks a condition Correct Answer: A high-level synchronization construct that encapsulates shared data and operations Rationale: A monitor is a high-level abstraction that combines shared data with the procedures that operate on it, ensuring mutual exclusion automatically.

Semaphores are lower-level primitives. Monitors are not hardware devices or busy- wait loops. They provide structured synchronization.

  1. What is the primary advantage of using monitors over semaphores? A) Monitors are more efficient in terms of memory usage B) Monitors provide automatic mutual exclusion, reducing programmer error C) Monitors can be used without any synchronization D) Monitors do not require any operating system support Correct Answer: Monitors provide automatic mutual exclusion, reducing programmer error Rationale: Monitors encapsulate shared data and ensure mutual exclusion implicitly, so programmers cannot forget to acquire and release locks. Semaphores require explicit wait and signal calls, which are error-prone. Monitors are not necessarily more memory-efficient and still require system support.
  2. In the context of monitors, what is a condition variable used for? A) To store the result of a computation B) To allow threads to wait for a specific condition to become true C) To define the type of data stored in the monitor D) To count the number of threads accessing the monitor Correct Answer: To allow threads to wait for a specific condition to become true 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.
  3. Which of the following is an example of a classic concurrency problem? A) The sorting problem B) The producer-consumer problem

Correct Answer: A problem involving philosophers who must share a limited number of forks to eat Rationale: The dining philosophers problem models a group of philosophers who alternate between thinking and eating, needing two forks to eat, with only one fork between each pair. It illustrates synchronization challenges like deadlock and starvation. It is not about sorting, networking, or prime numbers.

  1. 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.
  2. 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.

  1. 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.
  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.
  3. 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

Rationale: A lock is a synchronization mechanism that ensures only one thread can enter a critical section at a time, providing mutual exclusion. It does not prevent all access to shared data, increase speed, or allow simultaneous critical section execution. Locks are fundamental to concurrent programming.

  1. Which of the following correctly describes a spinlock? 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.
  2. 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.
  3. What is a mutex in concurrent programming?

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.

  1. 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 Rationale: A mutex has ownership semantics: only the thread that locked it can unlock it. A binary semaphore can be signaled by any thread, making it more flexible but also more error-prone. Both provide mutual exclusion. A mutex holds values of 0 or 1, not greater.
  2. Which of the following is a potential problem with using locks? A) Deadlock if locks are acquired in different orders B) Increased code simplicity C) Elimination of race conditions D) Automatic lock release Correct Answer: Deadlock if locks are acquired in different orders

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.

  1. Which of the following best describes a monitor's condition variable? A) A queue of threads waiting for a specific condition within the monitor B) A lock that provides mutual exclusion C) A counter that tracks the number of threads in the monitor D) A variable that stores shared data Correct Answer: A queue of threads waiting for a specific condition within the monitor Rationale: A condition variable in a monitor maintains a queue of threads waiting for a condition to become true. It is not a lock, counter, or data storage variable. Condition variables enable threads to wait and be signaled within the monitor's mutual exclusion.
  2. What is the primary difference between a monitor and a semaphore? A) Monitors provide automatic mutual exclusion, while semaphores require explicit wait and signal B) Semaphores provide automatic mutual exclusion, while monitors require explicit wait and signal C) Monitors can only be used in Java D) Semaphores cannot be used for mutual exclusion

Correct Answer: Monitors provide automatic mutual exclusion, while semaphores require explicit wait and signal Rationale: Monitors automatically ensure mutual exclusion for their procedures, while semaphores require programmers to explicitly call wait and signal. Semaphores are language-independent, and both can be used for mutual exclusion. Monitors are higher-level constructs.

  1. Which of the following is a message-passing concurrency model? A) Shared memory with locks B) Communicating Sequential Processes (CSP) C) Monitor-based synchronization D) Semaphore-based coordination Correct Answer: Communicating Sequential Processes (CSP) Rationale: CSP is a message-passing concurrency model where processes communicate by sending and receiving messages through channels. Shared memory with locks, monitors, and semaphores are shared-memory models. CSP avoids shared state, making it suitable for distributed systems.
  2. What is the primary advantage of message passing over shared memory? A) Message passing is always faster B) Message passing avoids race conditions by eliminating shared state C) Message passing requires less memory D) Message passing does not require synchronization Correct Answer: Message passing avoids race conditions by eliminating shared state Rationale: Message passing eliminates shared state, so race conditions cannot occur on shared variables. It is not always faster, may use more memory for

B) To write concurrent programs for the user C) To eliminate all race conditions automatically D) To prevent threads from communicating Correct Answer: To provide synchronization primitives and thread scheduling Rationale: The operating system provides synchronization primitives (locks, semaphores) and schedules threads on available processors. It does not write programs, automatically eliminate race conditions, or prevent communication. The OS enables concurrent execution and resource management.

  1. What is a context switch in concurrent programming? A) The process of saving and restoring a thread's state to switch execution B) A switch statement used in concurrent code C) The process of changing a variable's value D) A type of lock Correct Answer: The process of saving and restoring a thread's state to switch execution Rationale: A context switch saves the state of the currently running thread and restores the state of another thread, allowing multiple threads to share a CPU. It is not a switch statement, variable change, or lock type. Context switches enable multitasking but have overhead.
  2. What is the primary purpose of thread scheduling? A) To determine which thread runs next on a processor B) To create new threads C) To destroy terminated threads D) To synchronize thread access to shared data Correct Answer: To determine which thread runs next on a processor

Rationale: Thread scheduling decides the order in which threads execute on available processors. It does not create, destroy, or synchronize threads. Scheduling policies affect performance, fairness, and responsiveness. The scheduler is a core OS component.

  1. Which of the following is a common thread scheduling policy? A) Round-robin B) Alphabetical C) Random D) First-come, first-served by thread name Correct Answer: Round-robin Rationale: Round-robin is a common scheduling policy where each thread gets a fixed time slice in a circular order. Alphabetical, random by name, and name-based policies are not standard. Round-robin provides fairness and is widely used in time- sharing systems.
  2. What is the primary benefit of multithreading on a multi-core processor? A) True parallel execution of threads B) Reduced memory usage C) Elimination of synchronization needs D) Automatic deadlock prevention Correct Answer: True parallel execution of threads Rationale: On a multi-core processor, multiple threads can execute simultaneously on different cores, achieving true parallelism. Multithreading does not reduce memory usage, eliminate synchronization needs, or prevent deadlock. It can improve performance for CPU-bound tasks.
  3. What is the primary purpose of a thread pool?

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.
  1. 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.
  2. 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 (mutual exclusion, hold and wait, no preemption, circular wait) cannot hold, making deadlock impossible. Detection and recovery handle deadlocks after they occur. Prevention is proactive.
  3. 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