Haskell is a powerful functional programming language that supports concurrency and parallelism, enabling efficient execution of multiple tasks simultaneously. Concurrency allows multiple tasks to progress without necessarily executing simultaneously, while parallelism takes advantage of multiple processors or cores to perform tasks at the same time. Haskell provides various abstractions like forkIO, MVar, Chan, and STM (Software Transactional Memory) for managing concurrency and parallel computation, making it a popular choice for high-performance applications.
Topics Overview:
Introduction to concurrency in Haskell
Using forkIO, MVar, and Chan for concurrency
STM (Software Transactional Memory)
Basic parallel computation and lazy parallelism
MCQs on Concurrency and Parallelism in Haskell
1. Introduction to Concurrency in Haskell
What is the primary concept of concurrency in Haskell?
A) Executing tasks on separate threads
B) Managing multiple tasks simultaneously without direct synchronization
C) Running tasks sequentially
D) Distributing tasks across multiple machines
Which of the following describes Haskell’s approach to concurrency?
A) Cooperative multitasking
B) Preemptive multitasking
C) Explicit multithreading only
D) No concurrency support
Which abstraction does Haskell provide to manage concurrency?
A) Threads
B) Channels
C) Forking
D) MVar
What is the main benefit of concurrency in Haskell?
A) Parallel execution of code on multiple processors
B) Efficient management of multiple independent tasks
C) Automatic memory management
D) Reducing CPU usage
In Haskell, concurrency is achieved by:
A) Using non-blocking IO
B) Using forkIO to create new threads
C) Automatically splitting tasks across processors
D) Using lazy evaluation
2. Using forkIO, MVar, and Chan for Concurrency
What does the forkIO function in Haskell do?
A) It creates a new thread that runs concurrently
B) It handles software transactional memory
C) It synchronizes multiple threads
D) It performs parallel computation
Which data structure is used in Haskell to safely share state between threads?
A) IORef
B) MVar
C) Chan
D) List
The MVar in Haskell is used for:
A) Storing values in a thread-safe way
B) Passing messages between threads
C) Defining parallel tasks
D) Synchronizing IO operations
What is the primary use of Chan in Haskell?
A) Managing IO operations
B) Passing messages between threads
C) Defining parallel computation
D) Ensuring lazy evaluation
Which of the following is true about MVar and Chan?
A) Both are designed for memory management
B) MVar is used for synchronization, while Chan is for message passing
C) MVar is for parallel computation, while Chan is for lazy evaluation
D) MVar and Chan are interchangeable in Haskell
3. STM (Software Transactional Memory)
What does STM stand for in Haskell?
A) Synchronous Transaction Management
B) Software Transactional Memory
C) Single-threaded Memory
D) Systematic Task Management
What is the primary goal of STM in Haskell?
A) To simplify concurrent programming by making it easier to handle shared memory
B) To ensure that all threads run in parallel
C) To avoid using memory in concurrent tasks
D) To support lazy evaluation in multi-core systems
Which function in Haskell is used to begin a transaction in STM?
A) atomically
B) forkSTM
C) runSTM
D) executeTransaction
What is a key advantage of using STM in Haskell?
A) It avoids deadlocks by using retry mechanisms
B) It ensures maximum CPU utilization
C) It improves sequential performance
D) It limits memory usage in multi-core systems
Which of the following is a key feature of STM in Haskell?
A) Non-blocking synchronization
B) Transactional isolation of state changes
C) Automatic garbage collection
D) Direct manipulation of hardware
4. Basic Parallel Computation and Lazy Parallelism
What does parallelism mean in Haskell?
A) Running multiple tasks on different processors at the same time
B) Performing tasks in sequence but managing them efficiently
C) Lazy evaluation of tasks
D) Using a single processor to manage multiple threads
Which function in Haskell can be used for parallel computations?
A) par
B) forkIO
C) split
D) runTask
What is lazy parallelism in Haskell?
A) Delaying computations until their results are needed
B) Automatically distributing tasks across multiple processors
C) Using STM to manage parallel tasks
D) Running computations in sequence for better performance
How does Haskell achieve parallelism without explicit multi-threading?
A) Using lazy evaluation and parallel combinators
B) Manually splitting tasks into threads
C) Relying on external libraries for parallelism
D) Using STM for automatic parallel execution
In Haskell, the par function is used for:
A) Parallelizing computations in a lazy manner
B) Running tasks sequentially
C) Synchronizing multiple threads
D) Managing shared state between threads
Answer Key:
Qno
Answer (Option with Text)
1
B) Managing multiple tasks simultaneously without direct synchronization
2
A) Cooperative multitasking
3
D) MVar
4
B) Efficient management of multiple independent tasks
5
B) Using forkIO to create new threads
6
A) It creates a new thread that runs concurrently
7
B) MVar
8
A) Storing values in a thread-safe way
9
B) Passing messages between threads
10
B) MVar is used for synchronization, while Chan is for message passing
11
B) Software Transactional Memory
12
A) To simplify concurrent programming by making it easier to handle shared memory
13
A) atomically
14
A) It avoids deadlocks by using retry mechanisms
15
B) Transactional isolation of state changes
16
A) Running multiple tasks on different processors at the same time
17
A) par
18
A) Delaying computations until their results are needed