🎓 BookMCQ
← Back to 4. Threads in Operating System

📝 Thread libraries in operating system (33 MCQs)

Programming Interfaces for Creating and Managing Threads

Thread libraries provide programming interfaces through which applications create, synchronise, manage, and terminate threads. They belong to the operating system's threading environment and may implement operations through user-space runtime code, kernel facilities, or both. Common facilities include thread creation, joining, detaching, mutexes, condition variables, and thread-specific data, allowing developers to organise concurrent execution systematically.

Why Applications Need Standard Threading Interfaces

Manually managing execution contexts, processor states, and concurrent access is complex and error-prone. Thread libraries hide low-level management details behind standard functions and synchronisation abstractions. They also provide reusable programming interfaces across application components. However, library behaviour and available capabilities depend on the supported threading standard, operating system, and underlying implementation.

Creation, Joining, Synchronisation, and Thread Attributes

An application invokes a thread-creation function with a start routine and its argument. The library establishes the execution context and schedules or registers the thread through the supported implementation. A joining operation waits for termination and may collect its result. Mutexes and condition variables coordinate shared data and communication between execution activities.

Creating and Joining Two Worker Threads

Consider a program that creates two worker threads to calculate separate portions of an array containing 20,000 values. Each thread processes 10,000 elements and returns its partial sum. The main thread joins both workers, collects their results, and adds the partial sums. Joining ensures that the final calculation uses completed worker results.

Portability, Thread Control, and Synchronisation Overhead

Thread libraries simplify concurrent programming and support reusable code for servers, scientific applications, and interactive software. Standardised interfaces can improve portability, but platform-specific extensions may reduce it. Mutex contention, frequent thread creation, and incorrect locking can reduce performance or cause deadlocks. A library also cannot automatically eliminate race conditions in application logic.

Misconceptions About Thread Library Functions

'A thread library always schedules threads entirely in user space' is incorrect because many implementations rely on kernel-supported threads. 'Joining a thread creates another thread' is incorrect because joining waits for an existing thread to finish. 'Using a mutex automatically prevents every concurrency error' is incorrect because incorrect locking can still create races and deadlocks.

Exam Focus: Thread Creation and Synchronisation Interfaces

Identify the purpose of thread creation, joining, detaching, mutex, and condition-variable functions. Trace a program that creates workers, distributes tasks, and collects results. Questions may distinguish user-level from kernel-level thread management, compare library interfaces, and examine how synchronisation protects shared data.

Key Terms of Thread Libraries

  • Thread library - Interface for managing concurrent threads.
  • Start routine - Function executed by a new thread.
  • Join - Waits for thread termination.
  • Detach - Releases join-related thread resources.
  • Mutex - Lock providing mutual exclusion.
  • Condition variable - Coordinates waiting and notification.
10
Easy
12
Medium
11
Hard

📝 All Thread libraries in operating system MCQs

Q1. What is the primary role of a thread library in an application?

A.to replace the operating system kernel
B.to provide interfaces for creating, managing, and synchronizing threads ✅
C.to guarantee that every thread receives a separate processor
D.to convert every thread into an independent process
💡 Difficulty: easy | ✅ Correct: B

📖 Explanation: A thread library exposes programming interfaces for creating threads, waiting for completion, configuring attributes, and coordinating shared data. It simplifies thread management but does not replace the operating system or guarantee separate processors.

Q2. A developer wants a worker to calculate a result while the main thread continues accepting user input. Which library facility is required?

A.a function that creates a new thread with a start routine ✅
B.a function that duplicates the operating system
C.a function that disables all synchronization
D.a function that converts the worker into a device driver
💡 Difficulty: easy | ✅ Correct: A

📖 Explanation: Creating a worker thread gives the calculation its own execution path while the main thread handles input. Actual simultaneous execution depends on processor availability and implementation, but creation enables overlapping activities.

Q3. Which statement correctly distinguishes a thread library from the operating system scheduler?

A.the library is always hardware, while the scheduler is always firmware
B.the scheduler only creates variables, while the library manages disks
C.the library exposes thread-management operations, while the scheduler selects kernel-schedulable entities for processors ✅
D.both are necessarily the same software component
💡 Difficulty: easy | ✅ Correct: C

📖 Explanation: A thread library provides application-facing operations for creating and coordinating threads. The operating system scheduler selects kernel-schedulable entities for processor time. Their responsibilities interact, but they remain conceptually distinct responsibilities.

Q4. A program calls a thread-creation function with a start routine and an argument pointer. What does the new thread normally do?

A.it executes the caller's remaining instructions before the start routine
B.it begins execution at the supplied start routine using the supplied argument ✅
C.it restarts the entire process from its entry point
D.it waits until the creating thread terminates
💡 Difficulty: easy | ✅ Correct: B

📖 Explanation: A creation operation establishes a new thread whose execution begins at the designated start routine. The supplied argument provides input to that routine, allowing the creator to pass task-specific information to the worker.

Q5. A developer assumes a newly created thread must execute before the creation function returns. Which assessment is safest?

A.the assumption holds whenever the machine has multiple cores
B.the assumption holds only for threads with high priority
C.thread creation guarantees that the creator runs first
D.creation makes a thread eligible, but scheduling does not guarantee an exact execution order ✅
💡 Difficulty: medium | ✅ Correct: D

📖 Explanation: A successful creation call generally makes the new thread eligible to run, but the scheduler determines when it receives processor time. The creator or another thread may execute first, so explicit synchronization is needed when order matters.

Q6. A program creates three workers, but the second creation call fails. The main thread later attempts to join all three identifiers without checking return statuses. What is the best correction?

A.track successful creations and wait only for threads that were actually created ✅
B.assume creation cannot fail if memory is available
C.join the failed identifier repeatedly until it becomes valid
D.increase every worker stack size and ignore the error
💡 Difficulty: hard | ✅ Correct: A

📖 Explanation: Thread creation can fail because of resource limits, invalid attributes, or implementation restrictions. The program should check each return status, record successful creations, and join only valid thread identifiers to avoid invalid waits.

Q7. A worker calculates a result and terminates. The main thread must use that result before continuing. Which operation is most appropriate for a joinable worker?

A.detach the worker and immediately read its result
B.create another worker with the same identifier
C.join the worker and obtain its completion result through the supported interface ✅
D.assume the result is available as soon as creation succeeds
💡 Difficulty: easy | ✅ Correct: C

📖 Explanation: Joining waits until the target thread has terminated and can provide its completion result through the library interface. This ensures the main thread does not consume a partial result while the worker is still calculating.

Q8. A program creates a worker and immediately joins it before doing any independent work. What is the likely consequence?

A.the worker is guaranteed to use another processor
B.the program may lose the opportunity to overlap the worker's computation with other useful work ✅
C.the worker automatically becomes detached
D.the program becomes immune to synchronization errors
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: Joining immediately blocks the calling thread until the worker finishes. If the caller has independent work available, waiting first prevents that work from overlapping the computation and can reduce the benefit of creating a separate thread.

Q9. A worker is configured as detached. Which consequence should the application expect?

A.the worker cannot execute application code
B.the worker must be joined before it terminates
C.the worker receives a dedicated processor
D.its termination resources are generally reclaimed automatically, and ordinary joining is unavailable ✅
💡 Difficulty: easy | ✅ Correct: D

📖 Explanation: A detached thread is not intended to be joined for its termination result. Its implementation can reclaim associated resources automatically when it exits, so applications needing a join result should use a joinable design.

Q10. A thread finishes its assigned calculation and calls the library's thread-exit operation. What is the intended effect?

A.the entire operating system shuts down
B.every thread in the process is necessarily terminated
C.the calling thread terminates without necessarily terminating the whole process ✅
D.the thread is converted into a new process
💡 Difficulty: medium | ✅ Correct: C

📖 Explanation: A thread-exit operation terminates the calling thread and may provide a return value. Other threads in the same process can continue running unless the application or process termination rules cause them to stop.

Q11. A program sets each new worker's stack reservation to 2 MB. A capacity report gives these pairs: (1,2 MB) ; (2,4 MB) ; (4,8 MB) ; (8,16 MB) ; (12,24 MB), where each pair is (thread count, total reserved stack). Which conclusion follows?

A.reserved stack capacity grows by 2 MB per worker under the stated setting ✅
B.adding workers reduces total reserved stack capacity
C.stack reservation remains fixed regardless of thread count
D.the data prove every worker uses its entire stack
💡 Difficulty: medium | ✅ Correct: A

📖 Explanation: Each worker adds 2 MB to the reserved total; for example, 12×2=2412 \times 2 = 24 MB. The pattern describes configured reservation, not actual stack consumption by every worker during execution or peak memory use.

Q12. A thread library provides an attribute for stack size. Why might an application adjust it?

A.to guarantee execution on a particular processor core
B.to remove the need for registers
C.to prevent the thread from ever blocking
D.to balance per-thread memory use against required call depth and local storage ✅
💡 Difficulty: medium | ✅ Correct: D

📖 Explanation: Each thread needs stack space for calls, local variables, and related execution data. Reducing its stack can save memory but risks overflow; increasing it supports deeper calls while consuming more address-space resources.

Q13. A thread library lets an application set scheduling attributes for a new thread. What should the programmer avoid assuming?

A.that the requested priority or policy always overrides operating-system permissions and scheduling rules ✅
B.that attributes can affect thread creation
C.that thread attributes are configured before execution
D.that a library can expose scheduling-related settings
💡 Difficulty: hard | ✅ Correct: A

📖 Explanation: Thread attributes express requested settings, but the implementation and operating system determine which policies are supported and permitted. Applications should check errors and avoid assuming a requested priority or scheduling policy is always honored.

Q14. A program shares a pointer to a local variable with a worker. The creating function returns before the worker reads it. What is the principal risk?

A.the pointer automatically becomes a thread identifier
B.the library extends every local variable's lifetime
C.the worker may access storage whose lifetime has already ended ✅
D.the worker is guaranteed to terminate before the function returns
💡 Difficulty: hard | ✅ Correct: C

📖 Explanation: The local variable may cease to exist when its creating function returns. If the worker later dereferences the pointer, it can access invalid storage; the data must remain alive until the worker no longer needs it.

Q15. Two workers update the same shared counter without a mutex or atomic operation. The final value is occasionally too small. What explains this result?

A.thread creation automatically discards counter updates
B.concurrent read-modify-write sequences can overwrite one another and lose increments ✅
C.the counter resets whenever any worker terminates
D.a thread library permits only one variable per process
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: An increment commonly involves separate read, modify, and write steps. Concurrent interleaving can let both workers read the same old value and then overwrite one another's result, producing a lost update and an incorrect total.

Q16. A server benchmark records worker count and throughput while all workers contend for one mutex: (1,1000) ; (2,760) ; (4,490) ; (8,260) ; (16,130) requests per second. Which trend is most consistent with these measurements?

A.throughput rises linearly because more workers share the lock
B.throughput declines as contention increases, indicating serialization and lock-wait overhead ✅
C.the mutex guarantees parallel access to protected data
D.the measurements prove threads cannot run on separate cores
💡 Difficulty: hard | ✅ Correct: B

📖 Explanation: Throughput drops from 1000 to 130 requests per second as workers increase from one to sixteen. This pattern indicates rising contention and serialized access, not increased parallel efficiency under the measured workload.

Q17. A server updates two independent counters from different cores. The counters occupy the same cache line and are frequently modified. Which effect can occur even though the counters are distinct?

A.false sharing can cause cache-coherence traffic and reduce performance without creating a data race on those distinct variables ✅
B.the counters automatically become one variable
C.the thread library combines the updates into one atomic instruction
D.the operating system stops scheduling the writer threads
💡 Difficulty: medium | ✅ Correct: A

📖 Explanation: False sharing occurs when independent variables share a cache line that different cores modify. Coherence protocols may transfer the line repeatedly, reducing performance even though each thread accesses a distinct variable without a data race.

Q18. A producer signals a condition variable after adding an item to a shared queue. Why should a waiting consumer check the queue predicate in a loop after waking?

A.the signal guarantees the consumer owns every queue item
B.a condition variable removes the item automatically
C.a wake-up may be spurious or another consumer may take the item first ✅
D.the loop forces the producer to terminate
💡 Difficulty: medium | ✅ Correct: C

📖 Explanation: A notification does not guarantee the predicate is still true when the consumer resumes. Spurious wake-ups or competition among consumers can occur, so the condition must be rechecked while protecting the shared queue state.

Q19. A thread waits on a condition variable until a shared queue becomes nonempty. Which synchronization pattern is generally correct?

A.wait without a mutex and assume the queue cannot change
B.protect the predicate with a mutex, wait using the associated condition variable, then recheck the predicate after waking ✅
C.signal the condition variable before checking the queue and never check again
D.use a detached thread instead of checking the predicate
💡 Difficulty: hard | ✅ Correct: B

📖 Explanation: The mutex protects the queue predicate and coordinates the transition into waiting. After waking, the consumer reacquires the mutex and checks again because notification alone does not establish that an item remains available.

Q20. A producer adds one item to a queue and signals a condition variable. Several consumers are waiting. What does a typical signal operation request?

A.terminate every waiting consumer
B.wake exactly the producer that inserted the item
C.release all threads in the process from every wait
D.wake one thread waiting on that condition variable, if one is waiting ✅
💡 Difficulty: medium | ✅ Correct: D

📖 Explanation: A typical condition-variable signal wakes one waiting thread, if any exist. A broadcast operation is used when all waiters should be notified, but each awakened consumer must still recheck the shared predicate under the mutex.

Q21. A worker writes a shared result and then reports completion. The main thread sometimes sees an older value after receiving a notification. What should be examined?

A.whether the completion mechanism provides the required synchronization and memory visibility ✅
B.whether the worker's variable name is longer than the main thread's variable name
C.whether the computer has enough disk space
D.whether the main thread was created first
💡 Difficulty: hard | ✅ Correct: A

📖 Explanation: A notification must be accompanied by synchronization semantics that make earlier writes visible to the reader. Depending on the interface, a mutex, condition variable protocol, atomic operation, or join can establish the required ordering.

Q22. A thread library provides thread-specific data. A worker needs a temporary error code that must not be overwritten by other workers. Why is this facility suitable?

A.it automatically locks every shared data structure
B.it creates a separate process for the worker
C.it gives each thread its own instance of the specified data ✅
D.it stores the error code permanently in the executable
💡 Difficulty: medium | ✅ Correct: C

📖 Explanation: Thread-specific data provides a distinct instance of a value for each thread. This prevents one worker's temporary error code from overwriting another's, although genuinely shared data still requires appropriate synchronization.

Q23. A library supports a barrier for four worker threads. One worker exits before reaching the barrier, while the other three arrive. What may happen?

A.the barrier always releases after three arrivals
B.the remaining workers may wait indefinitely unless failure or cancellation behavior is defined ✅
C.the exited worker is automatically restarted
D.the barrier converts the remaining workers into detached threads
💡 Difficulty: hard | ✅ Correct: B

📖 Explanation: A barrier normally releases participants only after the required number have arrived. If one worker exits early, the others may remain blocked; robust programs need a defined failure, cancellation, or barrier-abort strategy.

Q24. A developer wants the same thread-management code to work across several operating systems. Which design choice best supports portability?

A.depend on undocumented behavior of one vendor's library
B.assume every platform uses identical function names and semantics
C.use platform-specific extensions throughout the application
D.prefer a standardized supported interface and isolate unavoidable platform-specific code ✅
💡 Difficulty: medium | ✅ Correct: D

📖 Explanation: Thread libraries differ in function names, supported attributes, and detailed semantics. Using a broadly supported standard interface and isolating platform-specific extensions reduces portability problems, while still requiring checks for implementation differences.

Q25. Which statement about the implementation of a thread library is most accurate?

A.it may rely on user-space runtime code, kernel facilities, or a combination of both ✅
B.it must always perform every operation entirely in user space
C.it must always perform every operation entirely inside the kernel
D.its implementation is independent of the operating system and runtime
💡 Difficulty: easy | ✅ Correct: A

📖 Explanation: A thread library defines interfaces, but its implementation can use user-space code, kernel-supported operations, or both. The API abstraction therefore does not by itself reveal where every creation, scheduling, or synchronization operation executes.

Q26. A program creates two workers to sum separate halves of an array containing 20,000 values. Each worker processes 10,000 values, and the main thread needs both partial sums before displaying the total. What should it do?

A.read both partial sums immediately after creation
B.join both workers or use equivalent completion synchronization, then combine their results ✅
C.terminate the workers after they process the first element
D.detach both workers and assume their sums are already complete
💡 Difficulty: easy | ✅ Correct: B

📖 Explanation: Each worker must finish its assigned 10,000 elements before its partial sum is final. The main thread should wait for both completions, then combine the results; joining provides a direct way to establish that completion point.

Q27. A program creates four workers to process equal portions of an array. Each worker requires 5 ms, and the main thread performs an independent 3 ms calculation before joining all workers. Ignoring overhead, what is the earliest completion time?

A.20 ms
B.23 ms
C.8 ms
D.5 ms ✅
💡 Difficulty: hard | ✅ Correct: D

📖 Explanation: The four workers execute concurrently and finish after 5 ms. The main thread's independent 3 ms calculation overlaps their work, so joining completes at 5 ms rather than adding the durations to obtain 8 or 23 ms.

Q28. A library provides an initialization function for a thread-attribute object. What is the main reason to initialize such an object before setting attributes?

A.to create the operating-system kernel itself
B.to make the new thread execute before the caller
C.to establish a valid attribute structure that can be configured and passed to thread creation ✅
D.to guarantee that the thread will be detached
💡 Difficulty: medium | ✅ Correct: C

📖 Explanation: An attribute initialization operation prepares the library's attribute object for use. The program can then set supported properties, such as stack or scheduling attributes, and pass the configured object to thread creation.

Q29. A worker encounters a recoverable task error in a program that processes many independent jobs. What is the strongest library-level design approach?

A.report the task failure through a result or error channel and preserve unrelated work when recovery is safe ✅
B.ignore the error and mark the task successful
C.terminate the entire process for every recoverable error
D.allow the worker to leave shared structures inconsistent so other workers detect the failure
💡 Difficulty: hard | ✅ Correct: A

📖 Explanation: A recoverable task failure should be communicated without corrupting shared state. The worker may continue or be replaced according to the error, allowing unrelated tasks to proceed instead of unnecessarily terminating the entire process.

Q30. A student says that using a thread library makes synchronization unnecessary on a multicore processor. Which correction is most accurate?

A.multiple cores make every shared-memory operation atomic
B.synchronization is needed only when a process has one thread
C.a library prevents all possible interleavings automatically
D.threads can execute concurrently, so shared-state access still requires appropriate synchronization ✅
💡 Difficulty: hard | ✅ Correct: D

📖 Explanation: A thread library supplies synchronization tools but does not automatically apply them to application data. Multiple cores can execute conflicting accesses simultaneously, so programmers must protect shared invariants with suitable locks or atomic operations.

Q31. A thread library supports a stack-size attribute. A worker has deep recursion and large local arrays. What is the risk of choosing a stack that is too small?

A.the thread will automatically receive a larger processor time slice
B.the worker may exhaust its stack and fail during execution ✅
C.the worker will become detached
D.the library will convert the local arrays into global variables
💡 Difficulty: hard | ✅ Correct: B

📖 Explanation: Recursive calls and large local objects consume stack space. If the configured stack cannot accommodate the worker's maximum usage, it may overflow and fail, so the size must reflect realistic call depth and local storage needs.

Q32. A benchmark records worker count and cumulative thread-creation time: (1,0.2 ms) ; (2,0.4 ms) ; (3,0.6 ms) ; (4,0.8 ms) ; (5,1.0 ms). Which conclusion is supported?

A.measured creation time rises by 0.2 ms per worker under these conditions ✅
B.creation time remains constant regardless of worker count
C.the data prove every system has the same creation cost
D.thread creation becomes free when more than one thread exists
💡 Difficulty: easy | ✅ Correct: A

📖 Explanation: The measurements increase by 0.2 ms whenever one worker is added. This describes the benchmark's observed cumulative creation cost, not a universal timing guarantee for every thread library or operating system.

Q33. A developer assumes that a join operation creates a new thread to collect another thread's result. What is the correct interpretation?

A.joining waits for an existing target thread to terminate; it does not create another thread ✅
B.joining duplicates the target thread and its stack
C.joining changes a detached thread into a new process
D.joining guarantees that the target thread starts again
💡 Difficulty: easy | ✅ Correct: A

📖 Explanation: A join operation coordinates with an existing thread and waits for its termination. It does not create a new execution entity, duplicate the target, or restart its computation; it provides a completion synchronization point.

🔗 Related Topics (MCQs)