📝 Grand Central Dispatch in operating system (33 MCQs)
Task-Based Concurrency in Apple's Grand Central Dispatch
Grand Central Dispatch (GCD) is Apple's concurrency technology for managing asynchronous tasks through dispatch queues. Rather than requiring applications to create and manage individual threads for every operation, developers submit blocks of work to queues. The system manages execution using available worker threads and processor resources, supporting concurrent application design within Apple's operating system environments.
Why Dispatch Queues Simplify Thread Management
Manual thread creation and coordination become difficult when applications contain many short or independent operations. Unrestricted thread creation can waste memory and processor time, while direct thread management complicates scheduling. GCD separates task submission from execution management, allowing the system to coordinate worker resources and schedule eligible work according to queue properties.
Serial and Concurrent Queues with Dispatch Blocks
A dispatch block represents a unit of work submitted to a dispatch queue. A serial queue executes submitted blocks in order, with no overlapping execution on that queue. A concurrent queue allows multiple blocks to execute concurrently, subject to available resources. Dispatch functions and synchronization facilities coordinate submission, execution, and completion.
Submitting Three Tasks to Different Dispatch Queues
Suppose an application submits three independent image-processing blocks to a concurrent queue. The system can schedule eligible blocks on available worker threads, potentially executing them on separate cores. If the same blocks enter a serial queue, they execute one after another in submission order. The queue type therefore influences execution ordering and concurrency.
Worker Efficiency, Queue Ordering, and Synchronisation
GCD simplifies asynchronous programming, reduces direct thread-management responsibilities, and supports efficient execution in Apple applications. Serial queues help preserve operation order, while concurrent queues expose parallel work. However, blocked tasks, excessive synchronisation, and shared-state races can reduce throughput. Developers must avoid assumptions that concurrent queue tasks always execute simultaneously or in a predictable completion order.
Misconceptions About Dispatch Queue Execution
'A dispatch queue is always a dedicated operating system thread' is incorrect because queues manage tasks rather than representing individual threads. 'A concurrent queue guarantees simultaneous execution of every block' is incorrect because available resources determine execution. 'Serial queues execute blocks in random order' is incorrect because their submitted work executes sequentially.
Exam Focus: Serial Queues and Concurrent Task Scheduling
Compare serial and concurrent dispatch queues and trace the submission and execution of multiple blocks. Distinguish task queues from worker threads and explain how GCD reduces manual thread management. Questions may test execution ordering, asynchronous submission, completion handlers, shared-data synchronisation, and the difference between task concurrency and guaranteed parallel execution.
Key Terms of Grand Central Dispatch
- GCD - Apple's task-based concurrency technology.
- Dispatch queue - Ordered collection of submitted tasks.
- Dispatch block - Unit of executable submitted work.
- Serial queue - Executes tasks sequentially.
- Concurrent queue - Allows overlapping task execution.
- Asynchronous execution - Work proceeds without immediate waiting.
📝 All Grand Central Dispatch in operating system MCQs
Q1. A developer needs to execute independent background tasks on Apple platforms without manually creating and managing a separate thread for every task. Which approach best fits this requirement?
📖 Explanation: GCD allows applications to submit units of work to dispatch queues while the system manages execution resources. This reduces the need to create, assign, and destroy a separate thread manually for every task.
Q2. A user interface becomes unresponsive because a developer performs a large image-processing operation synchronously on the main thread. Which change is most appropriate?
📖 Explanation: Long computation on the main queue delays input handling and screen updates. Moving suitable work to a background queue keeps the interface responsive, while the final interface change should be scheduled on the main queue.
Q3. A developer dispatches a task asynchronously to a queue and immediately reads a result that the task has not yet produced. Occasionally, the application uses an old value. What is the most likely cause?
📖 Explanation: Asynchronous submission returns before the submitted block necessarily finishes. Reading its result immediately can therefore use an old value; the program needs a completion handler, group notification, or another valid synchronization mechanism before consuming the result.
Q4. A developer submits three independent calculations to a serial dispatch queue. Their durations are 2, 3, and 4 seconds. Ignoring overhead, what is the minimum total execution time if all must finish on that queue?
📖 Explanation: A serial queue executes one submitted block at a time. The durations therefore add: seconds, assuming no overhead and that all three calculations execute on that queue in submission order.
Q5. A developer calls a synchronous dispatch operation on the main queue from code already executing on that same queue. The application hangs. What is the best explanation?
📖 Explanation: Synchronous dispatch blocks the caller until the submitted block completes. On the same serial main queue, the new block cannot run until the current block returns, while the current block is waiting, producing deadlock.
Q6. A developer needs to perform a background download and then update the interface only after the download succeeds. Which design best expresses the dependency without blocking the interface?
📖 Explanation: The download can run asynchronously without blocking the interface. Its completion path can then submit the required update to the main queue, preserving the dependency while ensuring UI work occurs in the appropriate execution context.
Q7. A task group contains independent file-processing jobs with highly variable execution times. The developer wants to avoid assigning every job to a specific thread. Which GCD property is most relevant?
📖 Explanation: GCD separates task submission from low-level thread assignment. Dispatch queues organize work, while the system schedules eligible tasks using managed execution resources, so developers need not assign each variable-duration job to a particular thread.
Q8. A developer submits a long-running task to a global concurrent queue and expects the system to create one new thread dedicated to that task. Which correction is most accurate?
📖 Explanation: A global concurrent queue represents work eligible for concurrent execution, not a promise of one dedicated thread per block. The system chooses execution resources, and the thread used for a task is generally not guaranteed.
Q9. A developer puts CPU-intensive work on a concurrent queue. A benchmark records core count and speedup: (1, 1.0) ; (2, 1.7) ; (4, 2.2) ; (6, 2.3) ; (8, 2.3). The interface remains responsive, but total processing time barely improves. Which interpretation is most reasonable?
📖 Explanation: Moving CPU-intensive work away from the main thread can improve responsiveness without shortening total computation. Performance still depends on available cores, serial sections, contention, synchronization, memory behavior, and scheduling overhead.
Q10. A developer wants to execute one setup block before several dependent processing blocks on the same serial queue. Which property helps preserve the required sequence?
📖 Explanation: A serial dispatch queue executes its submitted blocks one at a time in submission order. Placing setup before dependent processing blocks on the same queue preserves that sequence without requiring each block to manage its own thread.
Q11. A developer needs to execute several independent tasks concurrently but must protect a shared database cache from simultaneous modifications. Which design is generally reasonable?
📖 Explanation: Independent tasks can use a concurrent queue, while cache mutations are protected through a serial queue, lock, or suitable synchronization mechanism. This limits conflicting updates without unnecessarily serializing unrelated computation.
Q12. A task is submitted with a low-priority quality-of-service setting, while another task has a higher-priority setting. Which expectation is most appropriate?
📖 Explanation: Quality-of-service information communicates relative importance and responsiveness needs. The system may use it when scheduling work, but dependencies, competing activity, and resource availability mean it does not guarantee exact start or completion order.
Q13. A developer dispatches a block asynchronously to a serial queue and immediately waits on a semaphore that the block signals. The developer is already executing on that same serial queue. The application makes no progress. What should be checked first?
📖 Explanation: A semaphore wait blocks the calling thread. If that thread is needed to execute the queued block that signals the semaphore, neither can progress; redesign the dependency or avoid blocking the queue execution path.
Q14. A developer uses a dispatch group to coordinate four independent downloads and wants to run a final aggregation step after all downloads finish. Which approach best matches this requirement?
📖 Explanation: A dispatch group tracks several asynchronous tasks as one unit. The program can wait for the group or register a notification that runs after all associated tasks complete, making it suitable for final aggregation.
Q15. A developer uses dispatch_apply for 100 independent calculations and assumes each iteration executes on a newly created thread. Which statement is most accurate?
📖 Explanation: dispatch_apply invokes the supplied block a specified number of times and waits for all iterations to finish before returning. On a concurrent queue, iterations may overlap, but the block must be safe for concurrent invocation.
Q16. A program submits three blocks to the same concurrent queue. The first block creates a value required by the third block, while the second and third are submitted immediately afterward. What is the main correctness concern?
📖 Explanation: A concurrent queue does not guarantee that a later block waits for an earlier block's result. The program must express the dependency through a serial queue, group, barrier where appropriate, or another synchronization mechanism.
Q17. A developer wants several tasks to execute one after another in a fixed sequence, with each task seeing the previous task's completed changes. Which choice is most direct when all tasks can use the same queue?
📖 Explanation: A serial queue executes blocks one at a time in submission order. Submitting each dependent task after its prerequisite on that same queue ensures the later task begins only after the earlier block has completed.
Q18. A developer creates a custom serial dispatch queue and assumes it will always execute blocks on the same physical thread. Which assessment is correct?
📖 Explanation: A serial queue guarantees serialized execution, not permanent use of one physical thread. The system manages worker threads and may use different execution resources over time, except for queues explicitly bound to a particular thread.
Q19. A developer submits short tasks to a concurrent queue. A benchmark records task count and elapsed time: (1, 12 ms) ; (2, 9 ms) ; (4, 8 ms) ; (8, 11 ms) ; (16, 17 ms). Which experiment is most useful for understanding why larger batches become slower?
📖 Explanation: The useful experiment compares controlled sequential and concurrent runs while measuring elapsed time, task durations, contention, and resource use. This reveals whether scheduling overhead or synchronization costs outweigh the concurrency benefit for the workload.
Q20. A program has two independent background operations and one final operation that requires both results. Which coordination pattern best represents this dependency?
📖 Explanation: A dispatch group can track both independent operations and notify when all have completed. The final operation can then be scheduled after group completion, ensuring it does not consume either result prematurely.
Q21. A developer uses a dispatch source to monitor repeated events such as a timer firing or a file descriptor becoming ready. Which description best captures its role?
📖 Explanation: A dispatch source coordinates low-level events and delivers event handling through a dispatch mechanism. It can monitor timers, file-system activity, file-descriptor readiness, or signals without requiring an application to poll continuously.
Q22. A background task reports progress at times and percentages: (0 ms, 10%) ; (20 ms, 30%) ; (40 ms, 25%) ; (60 ms, 55%) ; (80 ms, 45%). Updates sometimes show an older percentage. Which design is most robust?
📖 Explanation: Concurrent progress reports can arrive out of order. Protect the progress state as needed, schedule UI changes on the main queue, and discard stale values so an older completion does not overwrite a newer display.
Q23. A developer submits a block asynchronously and releases an object that the block still needs. The release ends that object's lifetime before the block accesses it. What is the primary issue?
📖 Explanation: Asynchronous blocks may execute after the submitting function returns. Objects and other resources they use must remain valid until execution finishes; ownership rules, capture semantics, and cancellation behavior should preserve that lifetime.
Q24. A developer uses a barrier-style operation on a private concurrent dispatch queue created by the application to protect a shared resource. What is the intended effect?
📖 Explanation: On an appropriately created concurrent queue, a barrier runs exclusively relative to other work submitted to that queue, establishing an ordered access interval. It does not protect accesses from unrelated queues or external code.
Q25. A developer submits tasks to a global queue and uses a dispatch group notification targeting the main queue. Why is this arrangement useful for a graphical application?
📖 Explanation: The background tasks run without blocking the interface, and the group notification waits for their completion before scheduling a handler on the main queue. This provides a safe point for interface updates after aggregation work finishes.
Q26. A student claims that tasks submitted to a concurrent queue always run in parallel on separate cores. Which response is correct?
📖 Explanation: A concurrent queue permits multiple blocks to be eligible for overlapping execution, but actual parallelism depends on processor availability and scheduling. Tasks may wait or take turns, so separate-core execution is not guaranteed.
Q27. A serial queue processes requests. One request synchronously waits for another request submitted to the same queue. The application stops progressing. Which reasoning best explains the failure?
📖 Explanation: A serial queue runs only one block at a time. If the active block synchronously waits for another block queued behind it, that block cannot start until the active one returns, causing deadlock.
Q28. A developer processes a large collection of independent items and collects one result from each iteration. Which design avoids assuming that iterations finish in index order?
📖 Explanation: Independent iterations can finish in an order different from their indices. Store each result in its predetermined slot or coordinate updates to a shared collection, then wait for all required iterations before reading results.
Q29. A chart records worker count and throughput for tasks that repeatedly acquire the same lock: (1, 900) ; (2, 720) ; (4, 500) ; (8, 280) ; (16, 150) operations per second. Which change is most worth evaluating?
📖 Explanation: A shared lock serializes the protected operations, so adding tasks can increase waiting instead of useful work. Restructure shared-state access, reduce lock hold time, or batch updates while preserving correctness.
Q30. A developer wants a task to run after a delay without blocking the current thread. Which approach best fits the requirement?
📖 Explanation: A delayed dispatch operation schedules work for a future deadline and returns without making the current thread wait. The block may execute later than that deadline because scheduling and system load affect actual start time.
Q31. A developer submits a block to a queue and assumes that returning from the submission call means the block has completed. Which distinction is important?
📖 Explanation: Returning from an asynchronous submission means the work was enqueued, not completed. The caller continues while the block may still be pending or running, so a separate completion mechanism is required before using its result.
Q32. A highly parallel pipeline has stage two begin processing each item only after stage one produces that item's data, while unrelated items should overlap. Which design principle is best?
📖 Explanation: Each item's second stage depends on its first-stage output, but different items are independent. Represent per-item dependencies so each item preserves correct ordering while unrelated items can overlap through concurrent scheduling.
Q33. A developer sees that a task submitted to a background queue sometimes runs later than expected when the device is under heavy load. Which explanation is most accurate?
📖 Explanation: Submitting a block makes it eligible for execution but does not guarantee immediate scheduling. System load, competing work, priority, and available execution resources can delay its start, so applications should tolerate timing variability.