📝 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.
📝 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?
📖 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?
📖 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?
📖 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?
📖 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?
📖 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?
📖 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?
📖 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?
📖 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?
📖 Explanation: At time 3, five threads are runnable but only two logical CPUs are permitted. Simultaneous execution is therefore bounded by the smaller value: , 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?
📖 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?
📖 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?
📖 Explanation: Multiply the per-thread reservation by the thread count: MiB. This is about 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?
📖 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?
📖 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?
📖 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?
📖 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?
📖 Explanation: The table shows 8 MiB reserved per thread because . For 60 threads, the projected reservation is 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?
📖 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?
📖 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?
📖 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?
📖 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?
📖 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?
📖 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?
📖 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?
📖 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?
📖 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?
📖 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?
📖 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?
📖 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?
📖 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?
📖 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?
📖 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?
📖 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.