🎓 BookMCQ
← Back to 6. CPU Scheduling

📝 Pthread Scheduling in Thread Scheduling (44 MCQs)

📖 From Operating System • 6. CPU Scheduling • 44 questions available

What is Pthread Scheduling in Thread Scheduling?

Definition:
Pthread scheduling refers to POSIX thread scheduling attributes including contention scope, inheritance policy, and priority π\pi configured via `pthread_attr_setscope` and related APIs.

Example:
A developer sets `PTHREAD_SCOPE_SYSTEM` for a compute-intensive thread to ensure direct kernel scheduling, while setting `PTHREAD_SCOPE_PROCESS` for lightweight coordination threads.

Reason:
Explicit pthread scheduling control allows applications to optimize thread placement and responsiveness according to workload characteristics, bridging portable threading standards with platform-specific scheduling capabilities.

12
Easy
15
Medium
17
Hard

📝 All Pthread Scheduling in Thread Scheduling MCQs

Q1. What does PTHREAD_SCOPE_PROCESS specify in Pthread scheduling?

A.System Contention Scope scheduling
B.Process Contention Scope scheduling ✅
C.Global scheduling
D.Kernel-level scheduling
💡 Difficulty: easy | ✅ Correct: B

📖 Explanation: PTHREAD_SCOPE_PROCESS specifies Process Contention Scope (PCS) scheduling. In this policy, the thread library schedules user-level threads onto available LWPs within the same process, with competition limited to threads within that process.

Q2. What does PTHREAD_SCOPE_SYSTEM specify in Pthread scheduling?

A.Process Contention Scope scheduling
B.System Contention Scope scheduling ✅
C.User-level scheduling
D.Local scheduling
💡 Difficulty: easy | ✅ Correct: B

📖 Explanation: PTHREAD_SCOPE_SYSTEM specifies System Contention Scope (SCS) scheduling. In this policy, threads compete with all threads in the system, and the kernel schedules kernel-level threads onto physical CPUs.

Q3. On many-to-many systems, how does PTHREAD_SCOPE_PROCESS schedule threads?

A.Creates kernel threads for each user thread
B.Schedules user-level threads onto available LWPs ✅
C.Uses only one LWP for all threads
D.Bypasses the thread library
💡 Difficulty: easy | ✅ Correct: B

📖 Explanation: On many-to-many systems, PTHREAD_SCOPE_PROCESS schedules user-level threads onto available LWPs. The thread library maintains the number of LWPs and manages which user thread runs on each LWP.

Q4. On many-to-many systems, what does PTHREAD_SCOPE_SYSTEM do?

A.Schedules threads using PCS
B.Creates and binds an LWP for each user-level thread ✅
C.Uses a single LWP for all threads
D.Disables user-level threads
💡 Difficulty: easy | ✅ Correct: B

📖 Explanation: On many-to-many systems, PTHREAD_SCOPE_SYSTEM creates and binds an LWP for each user-level thread. This effectively maps threads using the one-to-one policy, where each user thread has its own kernel thread.

Q5. Which Pthread function is used to set the contention scope policy?

A.pthread_attr_getscope()
B.pthread_attr_setscope() ✅
C.pthread_setscope()
D.pthread_getscope()
💡 Difficulty: easy | ✅ Correct: B

📖 Explanation: pthread_attr_setscope() is the function used to set the contention scope policy for a thread. It takes a pointer to the thread's attribute set and a scope value (PTHREAD_SCOPE_PROCESS or PTHREAD_SCOPE_SYSTEM).

Q6. Which Pthread function is used to get the current contention scope policy?

A.pthread_attr_setscope()
B.pthread_attr_getscope() ✅
C.pthread_getscope()
D.pthread_scopeget()
💡 Difficulty: easy | ✅ Correct: B

📖 Explanation: pthread_attr_getscope() is the function used to get the current contention scope policy. It takes a pointer to the thread's attribute set and a pointer to an integer where the current scope value will be stored.

Q7. What is the first parameter in pthread_attr_setscope()?

A.The thread ID
B.A pointer to the attribute set for the thread ✅
C.The scope value
D.The priority value
💡 Difficulty: easy | ✅ Correct: B

📖 Explanation: The first parameter in pthread_attr_setscope() is a pointer to the attribute set (pthread_attr_t) for the thread. This attribute set contains the thread's configuration settings, including the contention scope policy.

Q8. What is the second parameter in pthread_attr_setscope()?

A.A pointer to the attribute set
B.The thread ID
C.The scope value (PTHREAD_SCOPE_PROCESS or PTHREAD_SCOPE_SYSTEM) ✅
D.The priority value
💡 Difficulty: easy | ✅ Correct: C

📖 Explanation: The second parameter in pthread_attr_setscope() is the scope value, which is either PTHREAD_SCOPE_PROCESS or PTHREAD_SCOPE_SYSTEM, indicating how the contention scope should be set for the thread.

Q9. What does pthread_attr_getscope() return in its second parameter?

A.The thread ID
B.A pointer to the current scope value ✅
C.The priority value
D.The stack size
💡 Difficulty: easy | ✅ Correct: B

📖 Explanation: pthread_attr_getscope() returns the current contention scope value through its second parameter, which is a pointer to an integer. This pointer is set to the current scope value (PTHREAD_SCOPE_PROCESS or PTHREAD_SCOPE_SYSTEM).

Q10. What value does pthread_attr_setscope() return if an error occurs?

A.0
B.A nonzero value ✅
C.A negative value
D.The scope value
💡 Difficulty: easy | ✅ Correct: B

📖 Explanation: pthread_attr_setscope() returns a nonzero value if an error occurs. A return value of 0 indicates success. This follows the standard Pthread error handling convention where nonzero values indicate errors.

Q11. What value does pthread_attr_getscope() return if an error occurs?

A.0
B.A nonzero value ✅
C.The scope value
D.A pointer to the scope
💡 Difficulty: easy | ✅ Correct: B

📖 Explanation: pthread_attr_getscope() returns a nonzero value if an error occurs. This allows error checking in programs to ensure the scope retrieval was successful before using the returned scope value.

Q12. On which systems is only PTHREAD_SCOPE_SYSTEM allowed?

A.Windows and Solaris
B.Linux and Mac OS X ✅
C.FreeBSD and AIX
D.HP-UX and IRIX
💡 Difficulty: easy | ✅ Correct: B

📖 Explanation: Linux and Mac OS X systems allow only PTHREAD_SCOPE_SYSTEM. These systems use the one-to-one thread model where each user thread maps to a kernel thread, making PCS scheduling unnecessary and unavailable.

Q13. Which contention scope policy uses the one-to-one mapping on many-to-many systems?

A.PTHREAD_SCOPE_PROCESS
B.PTHREAD_SCOPE_SYSTEM ✅
C.PTHREAD_SCOPE_BOTH
D.PTHREAD_SCOPE_NONE
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: PTHREAD_SCOPE_SYSTEM creates and binds an LWP for each user-level thread on many-to-many systems, effectively mapping threads using the one-to-one policy. This ensures each user thread has a dedicated kernel thread.

Q14. What maintains the number of LWPs when using PTHREAD_SCOPE_PROCESS?

A.The operating system kernel
B.The thread library ✅
C.The CPU scheduler
D.The memory manager
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: The thread library maintains the number of LWPs when using PTHREAD_SCOPE_PROCESS. It may use scheduler activations to manage the LWP pool, ensuring there are enough LWPs for user threads to run.

Q15. What is the effect of PTHREAD_SCOPE_SYSTEM on a many-to-many system?

A.It reduces the number of LWPs
B.It creates one LWP per user thread ✅
C.It uses a single LWP for all threads
D.It disables user-level threads
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: PTHREAD_SCOPE_SYSTEM creates one LWP per user thread on many-to-many systems. This effectively converts the many-to-many model to a one-to-one model, where each user thread has its own dedicated kernel thread.

Q16. What is the purpose of the pthread_attr_t structure in Pthread scheduling?

A.To store thread execution results
B.To hold thread attributes including contention scope ✅
C.To manage memory allocation
D.To handle I/O operations
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: The pthread_attr_t structure holds thread attributes including the contention scope policy. It is used as a parameter in functions like pthread_attr_setscope() and pthread_attr_getscope() to manage these settings.

Q17. In the Pthread scheduling API example, what does the program first determine?

A.The thread priority
B.The existing contention scope ✅
C.The stack size
D.The CPU affinity
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: In the Pthread scheduling API example, the program first determines the existing contention scope using pthread_attr_getscope(). It then sets the scope to PTHREAD_SCOPE_SYSTEM before creating threads.

Q18. What happens when PTHREAD_SCOPE_SYSTEM is set in the Pthread example?

A.Threads use PCS scheduling
B.Threads run using SCS scheduling ✅
C.Threads are not created
D.Threads use FCFS scheduling
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: When PTHREAD_SCOPE_SYSTEM is set, threads run using SCS scheduling. The program then creates five separate threads that will compete with all threads in the system for CPU time.

Q19. Why might a system only allow PTHREAD_SCOPE_SYSTEM?

A.It uses the many-to-one model
B.It uses the one-to-one model ✅
C.It uses the many-to-many model
D.It uses no thread model
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: Systems that use the one-to-one model (like Linux and Mac OS X) only allow PTHREAD_SCOPE_SYSTEM because each user thread maps directly to a kernel thread. PCS scheduling is not needed or supported in such systems.

Q20. What is the relationship between PTHREAD_SCOPE_SYSTEM and the one-to-one model on many-to-many systems?

A.It converts many-to-many to one-to-one ✅
B.It converts one-to-one to many-to-many
C.It has no effect
D.It disables threading
💡 Difficulty: medium | ✅ Correct: A

📖 Explanation: PTHREAD_SCOPE_SYSTEM creates and binds an LWP for each user-level thread on many-to-many systems, effectively converting the many-to-many model to a one-to-one model for those threads.

Q21. What is the primary benefit of using PTHREAD_SCOPE_PROCESS?

A.Faster kernel scheduling
B.Reduced context switching between user threads ✅
C.Better system-wide CPU allocation
D.Higher thread priority
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: PTHREAD_SCOPE_PROCESS allows user-level scheduling among threads within a process, which can reduce context switching overhead compared to kernel-level scheduling. Threads can be scheduled without kernel intervention, improving performance for fine-grained threading.

Q22. What is the primary benefit of using PTHREAD_SCOPE_SYSTEM?

A.Reduced context switching
B.Better system-wide CPU allocation ✅
C.Lower memory usage
D.Simpler thread creation
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: PTHREAD_SCOPE_SYSTEM allows the kernel to make system-wide CPU allocation decisions. Threads compete with all threads in the system, enabling better global resource utilization and fairer CPU distribution across processes.

Q23. If pthread_attr_setscope() returns 0, what does this indicate?

A.An error occurred
B.The operation succeeded ✅
C.The scope was not changed
D.The thread was terminated
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: A return value of 0 from pthread_attr_setscope() indicates success. The contention scope was successfully set to the specified value. This follows the standard Pthread convention where zero indicates successful execution.

Q24. What is the significance of scheduler activations in the context of PTHREAD_SCOPE_PROCESS?

A.They are not used
B.The thread library may use them to manage LWPs ✅
C.They replace PTHREAD_SCOPE_PROCESS
D.They are only used in one-to-one models
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: Scheduler activations may be used by the thread library to manage the number of LWPs. This mechanism allows the thread library to adjust the LWP pool based on system conditions, improving scheduling efficiency for PTHREAD_SCOPE_PROCESS.

Q25. In the Pthread API, what does the first parameter of pthread_attr_getscope() contain?

A.The thread ID
B.A pointer to the attribute set ✅
C.The scope value
D.The priority value
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: The first parameter of pthread_attr_getscope() is a pointer to the attribute set (pthread_attr_t) for the thread. This attribute set is queried to determine the current contention scope policy.

Q26. What is the scope of competition in PTHREAD_SCOPE_PROCESS?

A.All threads in the system
B.Threads within the same process ✅
C.Threads on the same CPU
D.Threads with the same priority
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: PTHREAD_SCOPE_PROCESS limits competition to threads within the same process. The thread library schedules user-level threads onto available LWPs, with contention only among threads of that process.

Q27. What is the scope of competition in PTHREAD_SCOPE_SYSTEM?

A.Threads within the same process
B.All threads in the system ✅
C.Threads on the same CPU
D.Threads with the same priority
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: PTHREAD_SCOPE_SYSTEM involves competition among all threads in the system. The kernel schedules threads globally, considering every thread regardless of which process it belongs to.

Q28. What happens when pthread_attr_getscope() is called with an invalid attribute pointer?

A.It returns success
B.It returns a nonzero value ✅
C.It sets the scope to default
D.It crashes the program
💡 Difficulty: hard | ✅ Correct: B

📖 Explanation: When pthread_attr_getscope() is called with an invalid attribute pointer, it returns a nonzero value indicating an error. This allows programs to detect and handle invalid attribute set usage.

Q29. How does PTHREAD_SCOPE_SYSTEM affect LWP creation on many-to-many systems?

A.It creates LWPs only when needed
B.It creates one LWP per user thread ✅
C.It reuses existing LWPs
D.It does not affect LWPs
💡 Difficulty: hard | ✅ Correct: B

📖 Explanation: PTHREAD_SCOPE_SYSTEM creates one LWP for each user-level thread on many-to-many systems. This effectively implements the one-to-one mapping, ensuring each user thread has its own kernel schedulable entity.

Q30. Why might a programmer choose PTHREAD_SCOPE_PROCESS over PTHREAD_SCOPE_SYSTEM?

A.To get kernel-level scheduling
B.To reduce context switch overhead ✅
C.To increase system-wide competition
D.To simplify kernel interaction
💡 Difficulty: hard | ✅ Correct: B

📖 Explanation: Programmers might choose PTHREAD_SCOPE_PROCESS to reduce context switch overhead. User-level scheduling among threads within a process avoids kernel involvement for thread switches, which can improve performance for applications with many threads.

Q31. Why might a programmer choose PTHREAD_SCOPE_SYSTEM over PTHREAD_SCOPE_PROCESS?

A.To reduce context switching
B.To ensure fair CPU allocation across all threads ✅
C.To use user-level scheduling
D.To avoid kernel scheduling
💡 Difficulty: hard | ✅ Correct: B

📖 Explanation: Programmers might choose PTHREAD_SCOPE_SYSTEM to ensure fair CPU allocation across all threads in the system. The kernel can make global scheduling decisions, preventing a single process from monopolizing CPU time at the expense of others.

Q32. In the Pthread scheduling example, how many threads are created after setting the scope to PTHREAD_SCOPE_SYSTEM?

A.3
B.4
C.5 ✅
D.6
💡 Difficulty: hard | ✅ Correct: C

📖 Explanation: In the Pthread scheduling example, the program creates five separate threads after setting the contention scope to PTHREAD_SCOPE_SYSTEM. These threads will run using SCS scheduling, competing with all threads in the system.

Q33. What is the effect of using PTHREAD_SCOPE_SYSTEM on a system that only supports one-to-one mapping?

A.It creates user-level threads
B.It maps each user thread to a kernel thread ✅
C.It uses a single kernel thread for all
D.It disables threading
💡 Difficulty: hard | ✅ Correct: B

📖 Explanation: On systems that only support one-to-one mapping (like Linux and Mac OS X), PTHREAD_SCOPE_SYSTEM maps each user thread to a kernel thread. This is the default behavior where every user thread has a corresponding kernel thread.

Q34. What is the relationship between the pthread_attr_t structure and thread creation in Pthreads?

A.Attributes are set before thread creation ✅
B.Attributes are set after thread creation
C.Attributes cannot be changed
D.Attributes are set at thread termination
💡 Difficulty: hard | ✅ Correct: A

📖 Explanation: The pthread_attr_t structure contains attributes that are set before thread creation. The contention scope is set using pthread_attr_setscope() before calling pthread_create(), and the thread inherits these attributes when created.

Q35. What is the significance of the nonzero return value in Pthread scheduling functions?

A.It indicates success
B.It indicates an error occurred ✅
C.It indicates the thread was created
D.It indicates the scope was changed
💡 Difficulty: hard | ✅ Correct: B

📖 Explanation: A nonzero return value in Pthread scheduling functions (pthread_attr_setscope() and pthread_attr_getscope()) indicates an error occurred. This is a standard error-handling convention where zero indicates success and nonzero indicates an error condition.

Q36. How does PTHREAD_SCOPE_PROCESS scheduling differ from PTHREAD_SCOPE_SYSTEM in terms of kernel involvement?

A.PCS requires more kernel involvement
B.SCS requires less kernel involvement
C.PCS has no kernel involvement, SCS has full kernel involvement ✅
D.Both have the same kernel involvement
💡 Difficulty: hard | ✅ Correct: C

📖 Explanation: PTHREAD_SCOPE_PROCESS has no kernel involvement in user-level thread scheduling; the thread library handles all scheduling decisions. PTHREAD_SCOPE_SYSTEM involves the kernel in scheduling decisions, as the kernel schedules kernel threads onto physical CPUs.

Q37. What is the primary purpose of pthread_attr_setscope() in multi-threaded programs?

A.To set the thread's stack size
B.To specify how threads should compete for CPU time ✅
C.To set the thread's priority
D.To allocate memory for the thread
💡 Difficulty: hard | ✅ Correct: B

📖 Explanation: pthread_attr_setscope() specifies how threads should compete for CPU time. It determines whether thread scheduling uses Process Contention Scope (within the process) or System Contention Scope (across all system threads), affecting scheduling behavior.

Q38. If a system only supports PTHREAD_SCOPE_SYSTEM, what happens if you try to set PTHREAD_SCOPE_PROCESS?

A.The call succeeds
B.The call returns a nonzero error ✅
C.The system automatically converts it
D.The thread is not created
💡 Difficulty: hard | ✅ Correct: B

📖 Explanation: If a system only supports PTHREAD_SCOPE_SYSTEM (like Linux and Mac OS X), attempting to set PTHREAD_SCOPE_PROCESS with pthread_attr_setscope() will return a nonzero error value. The operation fails because the requested scope is not supported.

Q39. What is the effect of PTHREAD_SCOPE_PROCESS on thread scheduling performance?

A.It increases kernel involvement
B.It reduces scheduling overhead ✅
C.It requires more memory
D.It increases context switching
💡 Difficulty: hard | ✅ Correct: B

📖 Explanation: PTHREAD_SCOPE_PROCESS reduces scheduling overhead because thread switches within the process are handled by the thread library without kernel intervention. This can significantly improve performance for applications with many short-lived threads.

Q40. What is the effect of PTHREAD_SCOPE_SYSTEM on thread scheduling performance?

A.It reduces scheduling overhead
B.It increases kernel involvement and scheduling overhead ✅
C.It eliminates context switching
D.It improves user-level scheduling
💡 Difficulty: hard | ✅ Correct: B

📖 Explanation: PTHREAD_SCOPE_SYSTEM increases scheduling overhead because every thread scheduling decision involves the kernel. However, this also enables better system-wide CPU allocation, as the kernel can make informed decisions considering all system threads.

Q41. What information does pthread_attr_getscope() provide to the programmer?

A.The thread's current priority
B.The thread's current contention scope ✅
C.The thread's stack size
D.The thread's CPU affinity
💡 Difficulty: hard | ✅ Correct: B

📖 Explanation: pthread_attr_getscope() provides the thread's current contention scope value. This allows programmers to determine whether threads are using PCS or SCS scheduling, helping in debugging and optimization of multi-threaded applications.

Q42. What is the relationship between PTHREAD_SCOPE_SYSTEM and SCS scheduling?

A.They are unrelated
B.PTHREAD_SCOPE_SYSTEM implements SCS scheduling ✅
C.PTHREAD_SCOPE_SYSTEM implements PCS scheduling
D.PTHREAD_SCOPE_SYSTEM disables scheduling
💡 Difficulty: hard | ✅ Correct: B

📖 Explanation: PTHREAD_SCOPE_SYSTEM implements System Contention Scope (SCS) scheduling. This means threads scheduled with this scope compete with all threads in the system, and the kernel is responsible for scheduling decisions.

Q43. What is the relationship between PTHREAD_SCOPE_PROCESS and PCS scheduling?

A.They are unrelated
B.PTHREAD_SCOPE_PROCESS implements PCS scheduling ✅
C.PTHREAD_SCOPE_PROCESS implements SCS scheduling
D.PTHREAD_SCOPE_PROCESS disables scheduling
💡 Difficulty: hard | ✅ Correct: B

📖 Explanation: PTHREAD_SCOPE_PROCESS implements Process Contention Scope (PCS) scheduling. This means threads scheduled with this scope compete only with threads within the same process, and the thread library handles scheduling decisions.

Q44. What happens if pthread_attr_setscope() is called with an invalid scope value?

A.The scope is set to default
B.A nonzero error value is returned ✅
C.The scope is set to PTHREAD_SCOPE_PROCESS
D.The scope is set to PTHREAD_SCOPE_SYSTEM
💡 Difficulty: hard | ✅ Correct: B

📖 Explanation: If pthread_attr_setscope() is called with an invalid scope value (not PTHREAD_SCOPE_PROCESS or PTHREAD_SCOPE_SYSTEM), the function returns a nonzero error value. This prevents the thread from being created with an invalid scheduling scope.

🔗 Related Topics (MCQs)