📝 Windows Threads in operating system (33 MCQs)
Windows Thread Architecture and Kernel Scheduling
Windows threads are the primary units of CPU scheduling within Windows processes. Each thread maintains its own execution context, including a stack, register state, scheduling information, and thread identifier, while sharing the process address space and handles to process resources. The Windows kernel manages thread states, dispatching, priorities, and synchronisation through operating system objects and scheduling facilities.
Why Windows Separates Threads from Process Resources
A Windows process provides a protected resource container, but applications often need several independent activities within that container. Separate threads enable concurrent operations while sharing memory, files, and other process resources. This arrangement supports responsive interfaces and server applications without requiring every task to maintain an independent process address space.
Thread Creation, Scheduling, and Synchronisation Objects
Windows applications can create threads using functions such as CreateThread() or the commonly used _beginthreadex() for C runtime-aware applications. The kernel maintains thread objects and schedules ready threads according to priority and processor availability. Synchronisation objects such as mutexes, semaphores, events, and critical sections coordinate access to shared resources and execution dependencies.
Launching Two Workers in a Windows Process
Consider a Windows application that creates two worker threads using _beginthreadex(). One processes the first half of a 100 MB file, while the other processes the remaining portion. Both threads share the process address space but use separate stacks. The application waits for both handles to become signalled before combining their results.
Priority Scheduling, Handle Management, and Thread Costs
Windows threads support parallel execution, responsive applications, and concurrent background tasks. Priority-based scheduling influences CPU allocation, while synchronisation objects help control access to shared resources. However, excessive thread creation increases kernel object and stack overhead. Incorrect priority selection may cause starvation-like delays, while failure to manage thread handles can retain unnecessary system resources.
Misconceptions About Windows Process and Thread Objects
'A Windows process is the unit directly scheduled on a CPU' is incorrect because its threads are scheduled. 'Every Windows thread has a separate process address space' is incorrect because process threads normally share that space. 'A thread handle is the thread execution context' is incorrect because the handle references a kernel-managed object.
Exam Focus: Windows Thread Creation and Synchronisation
Distinguish Windows process objects from thread objects and explain the thread scheduling role of the kernel. Recognise CreateThread(), _beginthreadex(), and common synchronisation objects. Questions may examine thread handles, priority levels, shared address spaces, waiting for thread completion, and why multiple threads within one process can execute on different processor cores.
Key Terms of Windows Threading
- Thread object - Kernel-managed thread representation.
- CreateThread() - Windows function creating a thread.
- Thread handle - Reference to a thread object.
- Priority - Scheduling importance assigned to a thread.
- Critical section - Synchronisation for shared resources.
- Event object - Kernel object used for notification.
📝 All Windows Threads in operating system MCQs
Q1. Which characteristic most directly distinguishes a Windows thread from its process?
📖 Explanation: A process provides resources such as a virtual address space and handles, while its threads execute instructions within it. Threads generally share process memory and resources but maintain separate execution contexts.
Q2. A Windows process has three threads. One changes a global configuration value, and the others read it. Why can they access the same variable?
📖 Explanation: Threads in one process normally share its virtual address space, including global variables and heap objects. A write by one thread can therefore be observed by another, although synchronization is needed for safe concurrent access.
Q3. A developer assumes a new Windows thread receives a private copy of the process heap. Which correction is accurate?
📖 Explanation: A Windows thread has its own stack and processor context, but threads in one process generally share the process heap and address space. Dynamically allocated objects may therefore be accessed by multiple threads and require coordination.
Q4. A worker dereferences an invalid pointer and raises an unhandled exception. Which conclusion about the other threads is safest?
📖 Explanation: An unhandled exception in a thread can terminate the entire process, depending on the exception and handling mechanisms. It is unsafe to assume that only the faulting thread disappears or that remaining threads continue normally.
Q5. Several threads increment one shared counter using separate read, add, and write steps without synchronization. Which result is possible?
📖 Explanation: A read-modify-write increment is not necessarily atomic. Two threads may read the same old value and both write the same new value, losing an increment. Use an atomic operation or synchronization to protect the update.
Q6. A Windows thread routine expects a pointer to a request structure, but the creator passes an integer address with an unrelated layout. What is the main risk?
📖 Explanation: The start routine must interpret its argument using the agreed type and layout. Passing an incompatible pointer can produce invalid values or unsafe memory access; Windows does not infer the intended structure or repair the mismatch.
Q7. When creating a Windows thread with CreateThread, which information is essential to specify the code where the new thread begins?
📖 Explanation: CreateThread requires a start address identifying the code the new thread will execute, along with other creation parameters. The thread procedure receives its parameter when it begins, while the scheduler determines when it runs.
Q8. A program calls CreateThread successfully and immediately assumes the new thread has already completed its first instruction. What is the error?
📖 Explanation: A successful creation call does not establish a precise execution order. The new thread becomes eligible to run, but scheduling may let the creator continue first or may dispatch the new thread immediately.
Q9. CreateThread returns a thread HANDLE and a thread identifier. Which statement correctly distinguishes them?
📖 Explanation: A thread handle is a reference to a thread object that can be used with APIs requiring a handle and suitable access rights. A thread identifier is a numeric identity, not a substitute for that handle.
Q10. A Windows service closes a worker's thread handle while the worker is still executing. What normally happens?
📖 Explanation: Closing a thread handle releases that handle reference; it is not a termination request. If the worker is still running and no other action stops it, execution may continue after the handle is closed.
Q11. A worker terminates, and another thread is waiting on its thread handle. What does the completed wait indicate?
📖 Explanation: When a Windows thread terminates, its thread object becomes signaled. A thread waiting on that handle can then proceed, which provides a completion notification without implying that the entire process has ended.
Q12. A Windows worker returns from its thread procedure. Which event normally follows?
📖 Explanation: Returning from the thread procedure terminates that thread. Its thread object becomes signaled, allowing waiters to proceed; the process continues if other threads remain active and no process-wide termination occurs.
Q13. A developer assumes each thread shares one stack because all threads share the process address space. Which analysis is correct?
📖 Explanation: Threads share the process virtual address space, but each normally has its own stack for calls, local variables, and return addresses. Shared address-space membership does not mean their stack pointers or stack contents are identical.
Q14. A developer suspends a worker while it holds a mutex, expecting the mutex to become available. What is the likely risk?
📖 Explanation: Suspending a thread can freeze it while it owns a mutex or another resource. Threads that need that resource may then block, potentially causing deadlock; suspension is not a safe replacement for cooperative shutdown.
Q15. A user-interface thread owns a window control, while a worker computes an update. The framework requires control access on the owning thread. What should the worker do?
📖 Explanation: A shared address space does not make every operation valid from every thread. When a framework requires UI access on the owning thread, the worker should post or marshal an update so that the UI thread performs it.
Q16. A service stores a temporary request identifier in thread-local storage. What benefit does this provide?
📖 Explanation: Thread-local storage gives each thread a distinct instance of a variable. It suits per-thread state such as request identifiers, but does not synchronize separate shared objects or make other process data automatically thread-safe.
Q17. A Windows application creates thousands of threads that mostly wait for network responses. CPU use remains low, but memory use rises. Which explanation is most reasonable?
📖 Explanation: A waiting thread may consume little processor time but still has an object, stack reservation, and management state. Large thread populations can therefore raise memory use and system overhead even when most threads are blocked.
Q18. A program creates a new Windows thread for every tiny task. Measurements show creation and scheduling overhead dominate useful work. Which redesign is most appropriate?
📖 Explanation: Short-lived threads can make creation, termination, and scheduling costs large relative to useful work. A bounded worker pool reuses existing threads, limits active concurrency, and can reduce overhead for many small tasks.
Q19. A thread uses an atomic increment on a shared counter, but reads a separate shared flag without synchronization. Which conclusion is correct?
📖 Explanation: Atomicity applies only to the operations and memory-ordering guarantees provided by the particular atomic mechanism. It does not protect unrelated variables or multi-step invariants, so the separate flag requires its own safe access protocol.
Q20. A worker must stop when a user presses Cancel, but it may be processing a valid request. Which shutdown design is generally safest?
📖 Explanation: Cooperative cancellation lets a worker observe a stop request, finish or abandon work safely, and release resources. Abrupt termination or indefinite suspension can leave shared state inconsistent and locks or other resources held.
Q21. A developer raises a worker's priority to fix a slow application, but the interface becomes less responsive. What is the likely explanation?
📖 Explanation: A higher-priority worker may receive processor time more aggressively when runnable. If it competes with the interface thread on limited cores, UI responsiveness can deteriorate; priority changes should be measured rather than applied indiscriminately.
Q22. A thread repeatedly checks a Boolean flag until another thread changes it, consuming nearly one core while waiting. Which improvement is best?
📖 Explanation: Busy waiting repeatedly consumes processor time while the desired condition is unavailable. An event or other blocking mechanism lets the thread sleep until notified, reducing wasted CPU use and often improving overall responsiveness.
Q23. A main thread needs a worker's result only after the worker has finished. Which sequence is appropriate?
📖 Explanation: Waiting for worker termination provides a clear completion point before the result is consumed. The program must still respect shared-data synchronization and object lifetime; thread creation alone does not prove that the result is ready.
Q24. One thread locks a mutex and updates a shared balance. A second thread reads the balance without locking. Which statement is correct?
📖 Explanation: A mutex coordinates only code that follows the same locking protocol. A reader that accesses the balance without acquiring the mutex or using another valid mechanism can observe inconsistent data or participate in a data race.
Q25. Several Windows worker threads remove items from one shared queue. Which design prevents two workers from removing the same item?
📖 Explanation: A shared queue must coordinate operations that inspect and remove items. A lock can make the removal sequence mutually exclusive, while a suitable concurrent queue provides defined thread-safe behavior; shared memory alone does not prevent duplicate removal.
Q26. A Windows application becomes unresponsive during a long calculation on its only thread. The developer proposes splitting the calculation among workers. What should be evaluated first?
📖 Explanation: Parallel decomposition helps when useful work can execute independently or be divided with manageable coordination. Serial dependencies may prevent speedup, and synchronization adds overhead, so the workload should be analyzed and measured before redesign.
Q27. The main thread waits for a worker to finish, while that worker waits for an event that only the main thread will signal after its wait returns. What problem exists?
📖 Explanation: The main thread cannot signal the event until its wait returns, while the worker cannot finish until the event is signaled. This circular dependency prevents progress and can cause deadlock; the completion protocol must be redesigned.
Q28. An image contains 12,000 independent pixels that need the same operation. A program uses four balanced workers. Which design best fits the workload?
📖 Explanation: Independent pixel operations can be divided among workers using disjoint regions. Balanced assignments reduce idle time, while separate output locations avoid conflicting writes; the design must still account for any dependencies at region boundaries.
Q29. Two workers spend most of their time waiting for the same exclusive mutex, and adding workers does not improve throughput. Which redesign is most promising?
📖 Explanation: A single exclusive lock serializes access to the protected region, limiting parallel progress. Shortening the critical section or partitioning state can reduce contention, whereas removing necessary synchronization risks corrupting shared data.
Q30. A service creates a new thread for every request. During traffic spikes, memory use and context-switching overhead rise sharply. Which change addresses the root cause?
📖 Explanation: Creating a thread for every request can cause excessive resource use during bursts. A bounded pool limits active concurrency, while queuing and backpressure manage excess requests and keep memory and scheduling costs under control.
Q31. A scheduler saves a Windows thread's instruction pointer, registers, and stack position before switching away. What do these values represent together?
📖 Explanation: A thread's execution context includes its instruction position, registers, and stack state. Preserving this information allows the scheduler to resume the thread from its previous point rather than restarting its computation.
Q32. A shared variable sometimes appears stale in another Windows thread even though both use the same address space. What is the appropriate response?
📖 Explanation: A shared address space makes data accessible but does not by itself define safe ordering or atomicity. A correct synchronization protocol or suitable atomic operations are needed to make concurrent updates and their visibility reliable.
Q33. A worker has finished and its thread handle remains open. Which statement about the thread object is most accurate?
📖 Explanation: Thread termination makes the thread object signaled, but the object can remain while handles are still open. Closing the remaining handles releases references, allowing the system to reclaim the object when it is no longer needed.