Thread Scheduling
Thread scheduling is the mechanism used by the operating system to determine which thread executes on the CPU and for how long. POSIX provides scheduling policies and priorities that influence thread execution behavior.
Learning Objectives
- Understand thread scheduling.
- Learn POSIX scheduling policies.
- Understand thread priorities.
- Learn real-time scheduling concepts.
- Understand scheduler behavior.
1. What is Thread Scheduling?
Thread scheduling is the process by which the operating system selects one runnable thread from multiple eligible threads and assigns CPU time for execution. Scheduling improves CPU utilization and enables multiple threads to share processor resources efficiently.
2. Thread Scheduling Workflow
3. POSIX Scheduling Policies
| Policy | Description |
|---|---|
SCHED_OTHER |
Default time-sharing scheduling policy. |
SCHED_FIFO |
Real-time First-In, First-Out scheduling. |
SCHED_RR |
Real-time Round-Robin scheduling. |
4. SCHED_OTHER
SCHED_OTHER is the default scheduling policy used
for normal user applications.
The operating system determines processor allocation according
to its standard time-sharing scheduler.
5. SCHED_FIFO
SCHED_FIFO is a real-time scheduling policy.
Runnable threads with the same priority execute in the order
they become ready.
Execution continues until the thread blocks, voluntarily yields,
or a higher-priority thread becomes runnable.
6. SCHED_RR
SCHED_RR is a real-time Round-Robin scheduling
policy.
Threads with equal priority receive processor time in rotating
time slices.
7. Scheduling Policy Comparison
| Feature | SCHED_OTHER | SCHED_FIFO | SCHED_RR |
|---|---|---|---|
| Application Type | General Purpose | Real-Time | Real-Time |
| Time Slice | Operating System Defined | No | Yes |
| Equal Priority Rotation | Operating System Defined | No | Yes |
| Priority Based | Yes | Yes | Yes |
8. Thread Priority
Thread priority is a scheduling attribute that influences the order in which runnable threads are selected for execution. Higher-priority runnable threads are generally selected before lower-priority runnable threads according to the active scheduling policy.
9. Scheduler Decision Process
10. Interview Questions
- What is thread scheduling?
- Why is thread scheduling required?
- What is the purpose of scheduling policies?
- What is
SCHED_OTHER? - What is
SCHED_FIFO? - What is
SCHED_RR? - Which scheduling policy is the default on POSIX systems?
- What is the difference between
SCHED_FIFOandSCHED_RR? - What is thread priority?
- How does the scheduler choose the next runnable thread?
11. sched_get_priority_max()
The sched_get_priority_max() function returns the
highest scheduling priority supported for a specified scheduling
policy.
12. sched_get_priority_min()
The sched_get_priority_min() function returns the
lowest scheduling priority supported for a specified scheduling
policy.
13. Priority Range Example
14. sched_setscheduler()
The sched_setscheduler() function changes the
scheduling policy and scheduling parameters of a process.
15. sched_getscheduler()
The sched_getscheduler() function returns the
current scheduling policy associated with a process.
16. Real-Time Scheduling
Real-time scheduling is designed for applications that require
predictable execution behavior.
POSIX defines the SCHED_FIFO and
SCHED_RR policies for real-time scheduling.
17. Scheduling Function Workflow
18. Linux Commands
| Command | Description |
|---|---|
man sched_get_priority_max |
Maximum priority documentation. |
man sched_get_priority_min |
Minimum priority documentation. |
man sched_setscheduler |
Configure scheduling policy. |
man sched_getscheduler |
Display scheduler information. |
chrt -p PID |
View scheduling policy and priority. |
19. Interview Questions
- What does
sched_get_priority_max()return? - What does
sched_get_priority_min()return? - Why do different scheduling policies have different priority ranges?
- What is the purpose of
sched_setscheduler()? - What does
sched_getscheduler()return? - What is a real-time scheduling policy?
- Which POSIX scheduling policies support real-time execution?
- Why might changing the scheduler require elevated privileges?
- How do scheduling priorities affect execution order?
- How can Linux display the current scheduling policy of a process?
20. pthread_setschedparam()
The pthread_setschedparam() function changes the
scheduling policy and scheduling parameters of an existing
thread.
21. pthread_getschedparam()
The pthread_getschedparam() function retrieves the
current scheduling policy and scheduling parameters associated
with a thread.
22. Scheduling Example
23. CPU Affinity (Linux Extension)
CPU affinity specifies the processor or processors on which a thread or process is permitted to execute. Assigning affinity can improve cache utilization and reduce CPU migration overhead in some workloads.
24. CPU Affinity Workflow
25. Scheduling Best Practices
- Use the default scheduling policy unless specialized behavior is required.
- Choose real-time scheduling only for applications with deterministic timing requirements.
- Verify supported priority ranges before assigning priorities.
- Check return values from all scheduling functions.
- Document any scheduling assumptions used by the application.
- Use CPU affinity only when workload analysis indicates a measurable benefit.
26. Linux Commands
| Command | Description |
|---|---|
man pthread_setschedparam |
Thread scheduling API documentation. |
man pthread_getschedparam |
Retrieve thread scheduling information. |
taskset -p PID |
Display CPU affinity (Linux). |
chrt -p PID |
Display scheduling policy and priority. |
ps -Lo pid,tid,cls,rtprio,pri,psr,comm |
Display thread scheduling information. |
27. Advanced Interview Questions
- What is the purpose of
pthread_setschedparam()? - How does
pthread_setschedparam()differ fromsched_setscheduler()? - What information is returned by
pthread_getschedparam()? - Why are scheduling policies separated from scheduling parameters?
- What is CPU affinity?
- Is CPU affinity defined by POSIX?
- What advantages can CPU affinity provide?
- When should real-time scheduling be considered?
- Why do scheduling operations often require elevated privileges?
- How can Linux display the CPU currently executing a thread?
28. Priority Inversion
Priority inversion occurs when a high-priority thread must wait for a resource currently held by a lower-priority thread. If intermediate-priority threads continue executing, the high-priority thread may experience unexpected delays.
29. Priority Starvation
Priority starvation occurs when lower-priority threads receive little or no processor time because higher-priority runnable threads continue to execute. Proper scheduling policies help reduce the likelihood of starvation.
30. Scheduling Limitations
| Limitation | Description |
|---|---|
| Operating System Support | Scheduling behavior depends on the operating system implementation. |
| Privileges | Changing scheduling policy or priority may require elevated privileges. |
| Real-Time Availability | Not every system supports all real-time scheduling features. |
| Policy Differences | Behavior can vary between POSIX-compliant operating systems. |
31. Best Practices
- Use the default scheduler unless specialized scheduling is required.
- Select real-time scheduling only for applications with deterministic timing requirements.
- Keep critical sections as short as possible.
- Verify priority ranges before assigning priorities.
- Check the return values of all scheduling functions.
- Test scheduling behavior on the target operating system.
- Document scheduling assumptions within the application.
32. Common Programming Mistakes
- Assuming scheduling behavior is identical on every POSIX system.
- Ignoring errors returned by scheduling APIs.
- Assigning real-time priorities without understanding their impact.
- Using real-time scheduling when normal scheduling is sufficient.
- Ignoring priority inversion scenarios in synchronized applications.
- Assuming CPU affinity is portable across all POSIX systems.
- Assigning priorities outside the supported range.
33. Advanced Interview Questions
- What is priority inversion?
- How does priority starvation occur?
- Why should critical sections remain short?
- What operating system factors influence scheduling behavior?
- Why do scheduling APIs often require elevated privileges?
- What problems can result from assigning incorrect priorities?
- Why is CPU affinity considered non-portable?
- When should real-time scheduling be avoided?
- Why should scheduling be tested on the deployment platform?
- How do scheduling policies and priorities work together?
34. Module Summary
In this module, we studied POSIX thread scheduling, including standard scheduling policies, thread priorities, scheduling functions, and real-time scheduling concepts. We also examined scheduling limitations, priority inversion, priority starvation, Linux-specific CPU affinity, and best practices for developing portable multithreaded applications.
Concept Mastery Assessment — Module 56
Verify your conceptual understanding with 12 targeted technical assessment questions. Instant feedback and explanations are provided for every choice.