🎓 BookMCQ
← Back to 4. Threads in Operating System

📝 Linux threads implementation (33 MCQs)

Linux Thread Groups and Kernel-Scheduled Tasks

Linux implements threads using kernel-schedulable tasks rather than treating threads as entirely independent user-space objects. Threads in one process typically belong to a thread group and share selected resources such as the address space, file descriptor table, and signal dispositions. Each task nevertheless maintains its own execution context, scheduling state, kernel stack, and task identifier.

Why Linux Uses Shared Process Resources for Threads

Applications need independent execution paths while retaining efficient access to common memory and resources. Linux supports this through the clone-based creation interface, which allows selected resources to be shared between tasks. Sharing avoids duplicating complete process environments, while separate task contexts permit independent scheduling and execution on available processor cores.

clone Flags, Task Structures, and Thread Scheduling

Linux thread libraries use the clone() system-call interface or related mechanisms to create tasks with selected shared resources. Flags such as CLONE_VM and CLONE_FILES specify sharing behaviour, while CLONE_SIGHAND controls sharing of signal dispositions. The kernel maintains task information and schedules each runnable task independently through its scheduler.

Creating Two Threads in a Linux Process

Suppose a Linux application creates two threads using a Pthreads library. The implementation creates corresponding kernel tasks that share the process address space and selected resources. Both threads can execute on separate CPU cores when available. Each retains its own register context and stack, while changes to shared memory can be observed by the other thread.

Kernel Scheduling, Resource Sharing, and Implementation Costs

Linux threading supports multicore execution, responsive applications, and efficient shared-memory communication. The kernel can schedule threads independently, while shared resources reduce the need for separate address spaces. However, shared memory requires synchronisation, and task creation and context switching consume resources. Linux implementation details differ from simplified textbook thread models and have evolved across kernel versions.

Misconceptions About Linux Tasks and Threads

'Linux threads are scheduled only by a user-space library' is incorrect because the kernel schedules their corresponding tasks. 'Threads always have separate address spaces' is incorrect because Linux threads commonly share one through CLONE_VM. 'Every Linux task shares all process resources automatically' is incorrect because clone flags determine which resources are shared.

Exam Focus: Linux clone and Shared Task Resources

Explain how Linux represents threads as kernel-schedulable tasks and identify the purpose of common clone flags. Distinguish task identifiers, thread groups, and process identifiers. Questions may examine shared versus private resources, kernel scheduling, independent execution contexts, and the differences between textbook threading models and Linux implementation details.

Key Terms of Linux Thread Implementation

  • clone() - Linux interface for creating tasks.
  • Task - Kernel-schedulable execution entity.
  • CLONE_VM - Flag sharing the address space.
  • Thread group - Related tasks representing one process.
  • Task identifier - Kernel identifier for a task.
  • Kernel stack - Stack used during kernel execution.
11
Easy
11
Medium
11
Hard

📝 All Linux threads implementation MCQs

Q1. A Linux application creates four POSIX threads. Each can be scheduled independently, shares the process address space, and has its own execution stack. Which implementation model best explains these properties?

A.Linux stores all four threads as one unschedulable user-space object
B.Linux represents each thread as a kernel-schedulable task that shares selected process resources but retains separate execution state ✅
C.Linux creates four unrelated processes with completely separate memory
D.Linux forces all four threads to use one common execution stack
💡 Difficulty: easy | ✅ Correct: B

📖 Explanation: Linux represents each thread as a kernel-schedulable task. Related tasks can share the process address space and selected resources, while each task retains its own execution context, including registers and stack.

Q2. A monitoring tool reports five distinct task identifiers for one Linux application. All five tasks access the same global variable and open-file table. Which conclusion best fits this observation?

A.The tool has combined five unrelated physical machines
B.The application must contain five independent address spaces
C.Only one task is actually scheduled because shared memory prevents concurrency
D.The application has multiple related tasks that share selected resources while remaining separately identifiable and schedulable ✅
💡 Difficulty: easy | ✅ Correct: D

📖 Explanation: Linux can represent threads as separate kernel tasks while grouping related tasks that share selected process resources. A common address space or file table therefore does not erase their distinct execution identities or scheduling states.

Q3. A student concludes that a shared address space proves a Linux process has only one schedulable task. Which reasoning step is incorrect?

A.The student equates shared process resources with one execution entity; Linux can schedule several distinct tasks that share the same address space ✅
B.The student assumes every task must have a different executable file
C.The student assumes each thread needs a separate physical processor
D.The student treats a kernel stack as ordinary shared application data
💡 Difficulty: medium | ✅ Correct: A

📖 Explanation: The flawed step is treating memory sharing as proof of a single task. Linux can maintain separate schedulable execution contexts for related threads even when those tasks access the same process address space.

Q4. A program creates a child task with the Linux clone interface and needs the parent and child to access the same virtual address space. Which flag directly requests that sharing?

A.CLONE_FILES
B.CLONE_SIGHAND
C.CLONE_VM ✅
D.CLONE_THREAD
💡 Difficulty: easy | ✅ Correct: C

📖 Explanation: CLONE_VM requests sharing of the virtual address space between the calling task and the created task. It does not, by itself, describe every other resource-sharing choice, so other clone flags control separate aspects.

Q5. A service creates related tasks using clone with CLONE_FILES. One task opens a file descriptor, and the other then uses that descriptor. What explains why this is possible?

A.CLONE_FILES copies the entire process into a separate address space
B.The tasks share the file descriptor table, so descriptor-table changes are visible to both ✅
C.The kernel automatically broadcasts every file operation to all tasks
D.CLONE_FILES makes every file descriptor a thread-local variable
💡 Difficulty: easy | ✅ Correct: B

📖 Explanation: CLONE_FILES makes the related tasks use the same file descriptor table. A descriptor added or closed through that shared table affects both tasks, unlike merely inheriting a separate copy of descriptor entries.

Q6. A Linux application uses clone flags to share the table that defines how signals are handled. Which flag corresponds to that resource-sharing choice?

A.CLONE_VM
B.CLONE_FILES
C.CLONE_THREAD
D.CLONE_SIGHAND ✅
💡 Difficulty: easy | ✅ Correct: D

📖 Explanation: CLONE_SIGHAND controls sharing of signal dispositions between related tasks. This choice concerns how signals are handled; it should not be confused with address-space sharing, file-descriptor-table sharing, or membership in a thread group.

Q7. A programmer says CLONE_THREAD merely gives a new task access to the parent's memory and therefore does not affect how Linux groups the tasks. What is the correction?

A.It changes only the child's stack size
B.It creates a new process group but no thread relationship
C.It places the new task in the same thread group as the calling task ✅
D.It makes the new task execute only in user space
💡 Difficulty: hard | ✅ Correct: C

📖 Explanation: The mistaken claim overlooks thread-group membership. CLONE_THREAD places the created task in the caller's thread group, allowing Linux to treat the related tasks as threads of one process while retaining separate task identities.

Q8. A Linux service attempts to restrict a worker to logical CPUs 2 and 3, but a monitor later shows it running on CPU 5. What should the developer verify first?

A.Increase the worker's stack size and repeat the request
B.Check whether the affinity call succeeded, used the correct task identifier, and produced an effective mask that permits only the intended CPUs ✅
C.Assume Linux ignores affinity settings for every thread
D.Create another task with CLONE_VM to force migration
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: Affinity applies to a schedulable task and may be limited by permitted CPUs. Verify the call's result, target task identifier, and effective mask before concluding that the scheduler violated the requested placement.

Q9. A Linux service records (time, runnable threads, permitted logical CPUs): (0,3,2); (1,4,2); (2,1,2); (3,5,2); (4,2,2); (5,3,2). At time 3, what is the maximum number of these threads that can execute simultaneously?

A.Five, because all five threads are runnable
B.Two, because execution is limited by the two permitted logical CPUs ✅
C.Three, because the process has three threads at time 5
D.One, because affinity permits only one CPU per process
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: At time 3, five threads are runnable but only two logical CPUs are permitted. Simultaneous execution is therefore bounded by the smaller value: min⁡(5,2)=2\min(5,2)=2, regardless of the larger ready count.

Q10. Two Linux threads run the same function at the same time. Each has its own register context and execution stack. Why is this separation necessary?

A.It prevents the threads from sharing any global variables
B.It converts each thread into a separate process
C.It guarantees that both threads execute on different CPU cores
D.It lets each task preserve its own call sequence and processor state while using shared process resources ✅
💡 Difficulty: medium | ✅ Correct: D

📖 Explanation: Separate stacks and register contexts preserve each thread's independent execution path. Shared global memory can still be accessed by both, but one thread's function calls and register values do not replace the other's.

Q11. A Linux process has three runnable threads. One thread blocks while reading a file, but the other two remain ready. Which statement best describes kernel scheduling?

A.The kernel must block the entire process because all threads share one address space
B.The two ready threads are forced to use the blocked thread's stack
C.The kernel can schedule the other runnable tasks independently while the first task waits ✅
D.The blocked task continues executing because file operations are asynchronous by definition
💡 Difficulty: hard | ✅ Correct: C

📖 Explanation: Linux schedules runnable tasks independently, even when those tasks belong to one process. Blocking one thread does not inherently block its siblings; another ready thread can use an available processor while the first waits.

Q12. A server creates 500 threads, each reserving an 8 MiB stack region. Ignoring guard pages and other overhead, what total virtual stack reservation do these threads represent?

A.4000 MiB, or about 3.91 GiB ✅
B.508 MiB
C.400 MiB
D.8000 MiB, or about 7.81 GiB
💡 Difficulty: medium | ✅ Correct: A

📖 Explanation: Multiply the per-thread reservation by the thread count: 500×8=4000500 \times 8=4000 MiB. This is about 4000/1024=3.914000/1024=3.91 GiB of reserved virtual address space, excluding guard pages and other overhead for all threads combined.

Q13. A Linux program calls pthread_create to start a worker function. Which implementation description best matches the usual Linux threading model?

A.The function is executed only when the main thread explicitly calls it
B.The threading library requests creation of a kernel-schedulable task for the new thread ✅
C.The kernel copies the entire process into an unrelated executable
D.The worker is represented only by an entry in a user-space array and cannot be scheduled by the kernel
💡 Difficulty: easy | ✅ Correct: B

📖 Explanation: The POSIX threading interface creates a thread in the calling process, and Linux implementations represent it with a kernel-schedulable task. The new thread begins at its supplied start routine while sharing selected process resources.

Q14. A joinable worker returns a pointer to a result after computing it. The creator must wait for completion and collect that return value. Which operation is appropriate?

A.pthread_detach
B.pthread_cancel
C.pthread_join with the worker's thread identifier and a return-value location ✅
D.fork followed by wait
💡 Difficulty: medium | ✅ Correct: C

📖 Explanation: pthread_join waits for the specified joinable thread to terminate and can retrieve its return value. It also allows the implementation to reclaim resources associated with that terminated thread after the join completes.

Q15. A joinable worker has finished, and its creator no longer needs to collect its return value. Which operation marks the worker so its remaining thread resources can be reclaimed automatically after termination?

A.pthread_join
B.pthread_cancel
C.pthread_once
D.pthread_detach ✅
💡 Difficulty: easy | ✅ Correct: D

📖 Explanation: pthread_detach marks a joinable thread as detached, so no thread needs to join it and its implementation resources can be reclaimed after termination. A detached thread's exit value cannot later be collected with pthread_join.

Q16. A student writes a program with one main thread and three workers, then assumes returning from main waits for all workers automatically. Which correction is accurate?

A.Returning from main normally terminates the process, including unfinished worker threads; explicit lifecycle coordination is required when they must finish ✅
B.Returning from main is identical to calling pthread_join on every worker
C.Linux converts each unfinished worker into an independent process
D.The workers continue indefinitely because they share the process address space
💡 Difficulty: hard | ✅ Correct: A

📖 Explanation: The flawed step is treating return from main as an implicit join. Normal process termination ends its remaining threads, so a program needing worker results or completion must coordinate them explicitly before exiting.

Q17. A profiling table reports (active threads, reserved stack space in MiB): (10,80); (20,160); (30,240); (40,320); (50,400). Assuming the same reservation per thread, what is projected for 60 threads?

A.420 MiB
B.480 MiB ✅
C.600 MiB
D.800 MiB
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: The table shows 8 MiB reserved per thread because 80/10=880/10=8. For 60 threads, the projected reservation is 60×8=48060 \times 8=480 MiB, assuming the same stack setting and excluding additional overhead under these assumptions.

Q18. Several Linux workers update a shared queue protected by a mutex. One worker exits a critical section without unlocking the mutex. Why do other workers stop progressing?

A.The queue is automatically copied into each worker's private address space
B.Linux changes the mutex into a counting semaphore
C.The mutex remains locked, so other workers cannot enter the protected section ✅
D.The kernel removes the other workers from the process thread group
💡 Difficulty: easy | ✅ Correct: C

📖 Explanation: A mutex grants exclusive access until its owner releases it. If the worker leaves it locked, other workers attempting to acquire it remain blocked, so every control path must release the mutex appropriately.

Q19. Four Linux threads build separate record buffers, then append their records to one shared log file. The records must never interleave and must follow a defined order. What additional mechanism is needed?

A.Separate buffers alone guarantee serialized file output
B.One file descriptor per thread automatically enforces a common record order
C.A shared address space prevents concurrent writes
D.A coordinated writer or suitable synchronization around output is needed to enforce record boundaries and ordering ✅
💡 Difficulty: medium | ✅ Correct: D

📖 Explanation: Private buffers isolate record construction, not writes to the shared file. A logging thread, mutex, or equivalent protocol must coordinate output when complete records and a defined ordering are required.

Q20. A Linux program lets one thread traverse a linked list while another removes nodes and frees them without synchronization. What is the main correctness risk?

A.The traversal may dereference freed memory or follow links changed during its operation ✅
B.The freed nodes automatically become private to the removing thread
C.The scheduler guarantees that readers finish before memory is released
D.The list is copied to a new address space whenever a node is removed
💡 Difficulty: hard | ✅ Correct: A

📖 Explanation: Concurrent removal can invalidate a reader's pointers or change links while traversal is in progress. If memory is freed too early, the reader may access invalid storage; locking or safe reclamation is required.

Q21. A Linux program replaces a mutex-protected counter increment with an atomic increment. Which statement describes the protection accurately?

A.The atomic counter also makes every unrelated shared structure safe
B.The increment can be indivisible for that counter, but multi-object invariants still need an appropriate synchronization design ✅
C.The atomic operation stores the counter separately in every thread
D.Atomic operations eliminate all cache-coherence and scheduling costs
💡 Difficulty: easy | ✅ Correct: B

📖 Explanation: An atomic increment prevents lost updates to the particular atomic object when used correctly. It does not automatically protect other variables or a multi-step invariant spanning several operations, which may still require coordination.

Q22. Several Linux threads call the same initialization routine concurrently, but a shared configuration should be initialized only once. Which POSIX mechanism is designed for this purpose?

A.pthread_join
B.pthread_detach
C.pthread_once ✅
D.pthread_mutex_destroy
💡 Difficulty: medium | ✅ Correct: C

📖 Explanation: pthread_once coordinates concurrent callers so the designated initialization routine is performed once. It is suitable for one-time setup of shared state, although later concurrent modifications to that state may require separate synchronization.

Q23. A Linux application uses a reader-writer lock. Readers arrive continuously, and a writer repeatedly waits while new readers are admitted. What problem can occur?

A.Every reader must execute alone
B.The lock automatically converts the writer into a reader
C.Writers always receive priority in every implementation
D.The writer may experience prolonged waiting or starvation under a reader-favoring policy ✅
💡 Difficulty: hard | ✅ Correct: D

📖 Explanation: Reader-writer locks permit concurrent readers while reserving exclusive access for writers. If the policy keeps admitting new readers, a waiting writer may be delayed indefinitely, so fairness and workload characteristics matter.

Q24. A producer-consumer queue has capacity 8, but the producer blocks even when inspection shows free slots. Which debugging action is most useful?

A.Check semaphore initialization and every wait/post operation against the queue's actual state transitions ✅
B.Increase the number of consumers without inspecting synchronization
C.Replace the queue's item count with task identifiers
D.Assume the Linux scheduler no longer schedules producer threads
💡 Difficulty: easy | ✅ Correct: A

📖 Explanation: Semaphore counts must represent available slots and queued items consistently. Incorrect initialization, a missing post, or an extra wait can make the synchronization state disagree with the queue even when physical space remains.

Q25. A Linux server protects an entire request-processing path with one global mutex. As worker count increases, waiting grows and throughput falls. Which redesign best addresses the likely bottleneck without sacrificing correctness?

A.Extend the critical section so each worker completes more work while holding the lock
B.Remove the mutex even though all workers still modify shared state
C.Shorten the serialized region or partition the shared resource so independent work can proceed concurrently ✅
D.Create more workers while leaving the shared critical section unchanged
💡 Difficulty: medium | ✅ Correct: C

📖 Explanation: A global mutex serializes requests that might otherwise proceed independently. Shortening its critical section or partitioning shared state can reduce contention while retaining protection for operations that genuinely require mutual exclusion.

Q26. A shared configuration contains four related fields. A writer updates them one at a time while readers inspect the structure, and readers sometimes observe combinations that never formed a valid configuration. Which design best preserves the invariant?

A.Make each field a separate global variable and assume aligned writes form one transaction
B.Use a consistent locking or safe-publication strategy covering the complete configuration state ✅
C.Increase the number of readers so each sees a different field
D.Move only the pointer into a separate task while leaving the object unprotected
💡 Difficulty: hard | ✅ Correct: B

📖 Explanation: The invariant spans multiple fields, so individual aligned writes do not make the whole update atomic. Readers and writers need a coordinated protocol, such as a mutex or immutable replacement with safe publication.

Q27. A Linux server measures throughput as worker count rises. The observations are (threads, requests per second): (1,100); (2,190); (4,350); (8,380); (16,390); (32,392). Which conclusion is best supported by the plotted data?

A.Throughput doubles at every increase in worker count
B.More than eight threads reduce throughput to zero
C.The scheduler stops running threads after the fourth worker
D.Throughput initially improves but then approaches a plateau, suggesting a shared bottleneck or overhead ✅
💡 Difficulty: medium | ✅ Correct: D

📖 Explanation: Throughput rises rapidly from 100 to 350 requests per second, then changes only from 380 to 392 as workers increase. The flattening indicates diminishing returns, consistent with a bottleneck or added coordination overhead.

Q28. A Linux server runs thousands of short jobs, many of which spend time waiting for I/O. Unbounded thread creation causes high resource use. Which starting design is generally more controlled?

A.Use a bounded worker pool and assess asynchronous I/O where it fits the workload ✅
B.Create one permanent operating-system process for every job
C.Reserve a larger stack for every newly created thread
D.Disable all concurrency and process every job in the main thread
💡 Difficulty: easy | ✅ Correct: A

📖 Explanation: A bounded worker pool limits simultaneous thread resources and reuses workers across jobs. For workloads dominated by waiting, asynchronous I/O may further reduce the number of threads needed, depending on the available interface.

Q29. A worker receives a pointer to a local variable in another thread's function. The owner returns, and the worker later dereferences the pointer. What is the central problem?

A.Linux redirects the pointer to an equivalent location on the worker's stack
B.The stack variable is automatically promoted to process-wide storage
C.The referenced object may have ended its lifetime, making the later access invalid ✅
D.A thread pointer always extends the owner's stack frame until process termination
💡 Difficulty: hard | ✅ Correct: C

📖 Explanation: A pointer does not extend the lifetime of the object it references. After the owning function returns, its automatic variable is no longer valid, so another thread must not dereference that stale pointer.

Q30. A Linux process has 20 runnable threads and an affinity mask allowing four logical CPUs. A developer gives all threads equal priority and concludes that every thread receives exactly one-fifth of every CPU interval. Why is this reasoning unsound?

A.Equal priority fixes each thread to a different CPU
B.Equal priority does not guarantee equal instantaneous service; runnable timing, blocking, affinity, workload, and scheduling policy affect actual execution ✅
C.The shared address space forces equal instruction counts
D.Linux ignores affinity whenever priorities are equal
💡 Difficulty: hard | ✅ Correct: B

📖 Explanation: Equal priority is not a promise of identical time slices or progress over every interval. Threads can block or become runnable at different times, and affinity and scheduling policy also influence their actual processor service.

Q31. A Linux task is created with CLONE_VM and CLONE_FILES. Two related tasks now share memory and the file descriptor table, but both update a shared counter without coordination. Which conclusion is correct?

A.Sharing these resources does not synchronize concurrent updates; the counter still needs an appropriate concurrency-control method ✅
B.CLONE_VM automatically makes every memory update atomic
C.CLONE_FILES prevents the tasks from executing simultaneously
D.The two tasks must use separate copies of the counter
💡 Difficulty: hard | ✅ Correct: A

📖 Explanation: CLONE_VM and CLONE_FILES specify resource sharing, not mutual exclusion. Because both tasks can access the same counter concurrently, updates can race; a mutex or suitable atomic protocol is still necessary.

Q32. An engineer wants to create a Linux thread-like task that shares the caller's address space, file descriptor table, and signal dispositions, while retaining independent execution state. Which interpretation of the requested design is most accurate?

A.The tasks must share one stack and one register context because their memory is shared
B.The tasks become separate machines because their execution state differs
C.Sharing selected process resources is compatible with distinct task contexts; the clone configuration must request the intended sharing explicitly ✅
D.The kernel schedules the tasks only through a user-space library
💡 Difficulty: hard | ✅ Correct: C

📖 Explanation: Linux task creation can combine selected shared resources with distinct execution contexts. The requested model does not imply a common stack or register set; the chosen clone flags determine which process resources are shared.

Q33. A Linux application protects a critical section with a normal, non-robust POSIX mutex. Thread B calls pthread_mutex_unlock even though Thread A owns it. Which assessment is correct?

A.The unlock succeeds and automatically transfers ownership to Thread B
B.The kernel schedules Thread B and releases the mutex on Thread A's behalf
C.The operation violates mutex ownership requirements and is not a valid general-purpose release; behavior may be undefined ✅
D.The mutex automatically becomes a counting semaphore with value one
💡 Difficulty: hard | ✅ Correct: C

📖 Explanation: The flawed step is treating unlock as permissionless. A normal POSIX mutex has ownership requirements, so a non-owner cannot safely release it; the operation may have undefined behavior rather than transferring ownership.

🔗 Related Topics (MCQs)