📝 Threads in operating system overview (30 MCQs)
Thread Abstraction and Execution Within a Process
Threads are the smallest units of CPU execution scheduled independently within a process. Each thread has its own thread identifier, program counter, register set, and stack, while sharing the process address space, code, data, and operating system resources. Thread management belongs to process management and allows applications to perform multiple execution activities without creating an entirely separate process for each task.
Why Multiple Execution Paths Are Needed
A single-threaded process performs one sequence of instructions at a time, which can delay other activities when an operation waits for input, output, or communication. Multiple threads allow independent activities to progress concurrently while sharing common application resources. This arrangement reduces the need for separate process creation but introduces coordination requirements when threads access shared data.
Thread Creation, Scheduling, and Shared Process Resources
A process creates threads through a supported threading interface. The runtime or kernel maintains each thread's execution context and scheduling state. A scheduler selects runnable threads for available CPU cores, while context switching saves and restores thread registers and stack information. Threads access shared process memory, but their individual stacks and execution states remain separate.
Executing Two Threads Within One Application
Consider a web browser using two threads, one to process user-interface events and another to retrieve a 5 MB resource from a server. While the network operation waits for data, the interface thread can continue responding to user input. Both threads use the same process address space but maintain separate stacks and processor contexts.
Concurrency, Responsiveness, and Shared-Memory Risks
Multithreading improves application responsiveness and can increase CPU utilisation when independent activities overlap. It is useful in browsers, servers, editors, and interactive applications. However, shared memory introduces race conditions, synchronisation overhead, and possible deadlocks. The operating system must also schedule runnable threads efficiently, particularly when many threads compete for a limited number of processor cores.
Misconceptions About Threads and Processes
'Every thread has a separate process address space' is incorrect because threads within a process normally share its address space. 'Threads always execute simultaneously' is incorrect because a single-core CPU interleaves their execution. 'Creating a thread requires creating a complete new process' is incorrect because threads can share existing process resources.
Exam Focus: Thread Components and Process-Level Sharing
Distinguish process and thread resources, identifying which information is private and which is shared. Explain thread creation, scheduling, context switching, and execution states. Questions may compare single-threaded and multithreaded processes or examine how shared memory enables communication while introducing synchronisation requirements and race conditions.
Key Terms of Thread Architecture
- Thread - Independently schedulable execution unit.
- Thread ID - Identifier assigned to a thread.
- Stack - Stores thread-specific execution data.
- Concurrency - Overlapping execution activities.
- Register set - Stores processor execution state.
- Shared memory - Memory accessible by multiple threads.
📝 All Threads in operating system overview MCQs
Q1. A photo-editing application runs one thread for user input and another for image rendering. Which statement best defines a thread?
📖 Explanation: A thread is an execution path inside a process. Threads in one process share process-owned resources, while each thread retains its own execution context, allowing multiple activities to progress within the same application.
Q2. Which set of components is normally maintained separately for each thread in a multithreaded process?
📖 Explanation: The program counter identifies the next instruction, registers hold the thread's current CPU values, and the stack records its active calls and local execution data. These items must remain distinct for independently progressing threads.
Q3. Which resources do threads belonging to the same process typically share?
📖 Explanation: Threads in the same process normally use one virtual address space, so code, global data, and heap allocations are accessible to them. Each thread still has separate registers, program counter, and stack.
Q4. A student claims that threads share one stack and register set, while each thread owns a separate copy of the program code and open files. What is wrong with this claim?
📖 Explanation: The conclusion is incorrect because the stack and register state belong to individual threads, not to a shared process-wide copy. Code, global data, heap, and process-managed open files are commonly shared.
Q5. In a document editor, the user-interface thread changes a global document-status variable. Why can an autosave thread in the same process read the updated variable?
📖 Explanation: The changed value resides in shared process memory, so another thread using the same address space can observe it. This sharing supports communication, but simultaneous updates may require synchronization to preserve correctness.
Q6. Threads are often described as lightweight processes. What is the most accurate reason for this description?
📖 Explanation: The lightweight description refers to avoiding a separate process environment for every execution path. Threads share many process resources, but each keeps its own CPU context and stack so execution can proceed independently.
Q7. Two threads take turns running on a single-core processor. Which statement correctly describes their execution?
📖 Explanation: On one core, the processor can run only one thread at an instant. The scheduler creates concurrency by interleaving execution, so progress overlaps over time without simultaneous instruction execution on that core.
Q8. A process has two runnable threads on a four-core computer. Under what condition can both threads execute at the same instant?
📖 Explanation: If both threads are runnable and the scheduler assigns them to separate available cores, they can execute at the same instant. Sharing one process does not prevent parallel execution on multicore hardware.
Q9. A single-core server records completed requests per minute as active threads increase: (1,18), (2,31), (3,40), (4,43), (5,43), (6,42). Which interpretation is best supported by these data?
📖 Explanation: Throughput rises rapidly at first, reaches 43 requests per minute at four and five threads, then falls slightly. This pattern indicates saturation: extra runnable threads add scheduling overhead without improving completed work.
Q10. A file-processing application has one thread waiting for disk input while another has CPU work ready. Why can multithreading improve responsiveness in this situation?
📖 Explanation: When one thread is waiting for an event such as disk completion, it cannot use the CPU productively. The scheduler may run another ready thread, allowing useful work to continue during the delay.
Q11. On a single-core CPU, an execution trace shows T1 runs from 0 to 2 ms, T2 from 2 to 5 ms, T1 from 5 to 7 ms, and T2 from 7 to 8 ms. What does this trace demonstrate?
📖 Explanation: The trace alternates execution between T1 and T2 on one core. Both threads make progress during the same eight-millisecond period, demonstrating concurrency; because their intervals do not overlap, it does not demonstrate parallel execution.
Q12. A process has five runnable threads on a machine with two physical cores and no simultaneous multithreading. What is the maximum number that can execute at one instant, and how many others must remain off the CPU?
📖 Explanation: Each physical core can execute at most one software thread at an instant under the stated assumption. Therefore, two of the five runnable threads can run simultaneously, leaving three ready but not executing.
Q13. During a context switch from one thread to another thread in the same process, which information is essential to preserve the first thread's execution point?
📖 Explanation: The processor state determines where and how a thread resumes. Saving its program counter, stack pointer, and relevant registers preserves its execution context; shared code, heap data, and process file resources are not duplicated.
Q14. Three threads are ready to run in the same process. What determines which one receives the CPU next?
📖 Explanation: Runnable threads compete for processor time, and the scheduler selects among them according to the operating system's policy and current conditions. Source-code order, stack size, and variable placement do not establish execution order.
Q15. A browser tab uses one thread for interface events and another for downloading a large image. The download is waiting for network data. What benefit can this arrangement provide?
📖 Explanation: The download thread may be blocked while waiting for network data, but the interface thread can remain runnable. Scheduling the interface thread allows the browser to respond without waiting for the network operation to finish.
Q16. A student says that switching between two threads of one process requires loading a new address space, duplicating the heap, and reopening every file. Which correction is most accurate?
📖 Explanation: The student's description confuses a thread switch with a change of process. Threads in one process retain shared address-space and process resources, while the processor context needed by each thread is saved and restored.
Q17. A parallel application records throughput as processor cores increase: (1,40), (2,72), (3,95), (4,110), (5,116), (6,119) tasks per second. Which conclusion follows from the data?
📖 Explanation: Throughput increases from 40 to 119 tasks per second, but successive gains shrink from 32 to 3. The measurements show diminishing returns, consistent with coordination costs or portions of work that cannot run in parallel.
Q18. A shared counter starts at 4. Thread A and Thread B both read 4, each adds 1 locally, and each writes 5. What value remains after both writes?
📖 Explanation: Both threads read the same initial value, 4, before either writes. Each therefore computes 5, and the later write replaces the earlier one. The final value is 5, showing a lost update.
Q19. Two threads independently call the same recursive function at the same time. Why can each call maintain its own return addresses and local variables?
📖 Explanation: Each thread uses its own stack to record function-call frames, return addresses, and local variables. The threads can execute the same code while preserving separate call histories and independent points of execution.
Q20. A service must process 12 independent CPU-bound jobs. Each job needs 3 ms of CPU time, and four cores are available. Assuming perfect balancing and no overhead, what is the ideal completion time?
📖 Explanation: Four cores can process four jobs simultaneously. Twelve jobs therefore require waves; each wave takes 3 ms, giving an ideal completion time of ms.
Q21. An intern argues that because every thread has a private stack, a multithreaded process contains no data that can be accessed by more than one thread. What is the flaw?
📖 Explanation: A private stack separates each thread's active calls and local stack data, but it does not isolate the process's address space. Threads can still access shared global variables, heap objects, and other process resources.
Q22. Which identifier is used to distinguish one thread from other threads managed within a process or system?
📖 Explanation: A thread identifier provides a way for the operating system or threading environment to refer to a particular thread. It is distinct from the process identifier, which identifies the process that owns the thread.
Q23. What is the main communication advantage of threads sharing one process address space?
📖 Explanation: Because threads share an address space, one thread can place data in memory that another thread can access directly. This can simplify communication, although concurrent modifications still require appropriate coordination.
Q24. A student claims that a race condition cannot occur on a single-core processor because only one thread executes at any instant. Which reasoning identifies the error?
📖 Explanation: The claim overlooks interleaving. A thread can be interrupted after reading shared data but before writing its result; another thread may perform the same steps, producing an incorrect outcome even on one core.
Q25. Thread A holds lock L1 and waits for L2, while Thread B holds L2 and waits for L1. Which problem can prevent both threads from making progress?
📖 Explanation: Each thread waits for a lock held by the other, creating a circular wait. Neither can proceed to release its lock, so both remain blocked until some external action breaks the deadlock.
Q26. Three threads each perform one unsynchronized increment of shared integer X, initially 0. Each increment consists of separate read, add, and write steps, and switching may occur between any steps. What is the smallest possible final value after all threads finish?
📖 Explanation: All three threads can read 0 before any writes occur. Each then adds one and writes 1, so the final value can be 1. No completed increment writes a value below 1, making 1 the minimum.
Q27. A program creates 100 short-lived threads for 100 tiny CPU tasks on a two-core machine. Why could this design perform worse than using fewer long-lived execution threads?
📖 Explanation: When each task performs little work, creating and scheduling many threads can consume more time than the tasks themselves. Thread-management overhead and frequent context switches may therefore reduce total throughput.
Q28. A data-analysis application divides independent records among worker threads. The workers also update one shared summary value. Which design concern combines the benefit and risk of threads?
📖 Explanation: Shared address space lets workers contribute to one summary without separate process communication. However, simultaneous read-modify-write operations can overwrite one another, so the update must be protected or made atomic.
Q29. Why can adding more runnable threads to a CPU-bound application eventually reduce performance?
📖 Explanation: Additional threads can expose more parallel work, but they also require scheduling and processor-state switching. Once available cores are saturated, excess runnable threads may add overhead without increasing useful CPU capacity.
Q30. A process runs on a two-core machine. Thread T1 is running on core 0, T2 is ready, and T3 is blocked waiting for network data. Which statement is correct?
📖 Explanation: T1 is using one core, while T2 is eligible for another available core. T3 is blocked and cannot run until its event completes; nevertheless, all three threads remain within the same process and share its resources.