Advanced Process Scheduling
Process scheduling determines how the operating system allocates CPU time among runnable processes. Scheduling policies and priorities help balance responsiveness, fairness, and efficient CPU utilization.
Learning Objectives
- Review process scheduling concepts.
- Understand process priorities.
- Learn the
nice()interface. - Study
getpriority(). - Study
setpriority(). - Understand scheduling behavior.
1. What is Process Scheduling?
Process scheduling is the mechanism used by the operating system to select which runnable process receives CPU time. The scheduler continually chooses the next process based on the system's scheduling policy and process priorities.
2. Scheduling Objectives
| Objective | Description |
|---|---|
| Fairness | Provide reasonable CPU access to runnable processes. |
| Responsiveness | Reduce waiting time for interactive applications. |
| CPU Utilization | Keep the processor busy whenever possible. |
| Throughput | Complete as many processes as possible over time. |
| Low Latency | Reduce delays before execution. |
3. Scheduling Workflow
4. Process Priority
A priority represents the relative importance of a runnable process. The scheduler uses priority information, together with its scheduling policy, to determine CPU allocation.
5. nice()
The nice() function changes the nice value associated
with the calling process.
The nice value influences scheduling priority according to the
operating system's scheduling policy.
6. Return Value
| Return | Description |
|---|---|
| New Nice Value | Operation completed successfully. |
| -1 | Error (check errno to distinguish from a valid return value). |
7. Example Using nice()
8. Nice Values
| Nice Value | General Effect |
|---|---|
| Lower Value | Higher scheduling priority. |
| Higher Value | Lower scheduling priority. |
The exact supported range is implementation-dependent.
9. Scheduling Example
10. Interview Questions
- What is process scheduling?
- What is the purpose of
nice()? - How does the scheduler use priorities?
- Does a higher nice value increase or decrease scheduling priority?
- Why should
errnobe checked afternice()returns-1? - Can scheduling behavior vary between UNIX implementations?
- What is the primary goal of CPU scheduling?
- What is meant by fairness in scheduling?
- Why is responsiveness important?
- What is throughput?
11. getpriority()
The getpriority() function returns the scheduling
priority (nice value) associated with a process, process group,
or user.
12. Function Parameters
| Parameter | Description |
|---|---|
which |
Selects the object type. |
who |
Identifies the process, process group, or user. |
13. PRIO Constants
| Constant | Description |
|---|---|
PRIO_PROCESS |
Operate on a single process. |
PRIO_PGRP |
Operate on a process group. |
PRIO_USER |
Operate on all processes owned by a user. |
14. Example Using getpriority()
15. setpriority()
The setpriority() function changes the scheduling
priority (nice value) for a process, process group, or user.
16. Example Using setpriority()
17. Priority Selection
18. Linux Scheduling Commands
| Command | Purpose |
|---|---|
nice |
Start a program with a specified nice value. |
renice |
Modify the nice value of an existing process. |
ps -el |
Display scheduling-related process information. |
top |
Monitor running processes and priorities. |
19. Best Practices
- Always verify the return values of
getpriority()andsetpriority(). - Check
errnowhengetpriority()returns-1. - Avoid assigning unnecessarily high scheduling priority.
- Use priority changes only when required by the application.
- Follow the operating system's scheduling policies.
20. Interview Questions
- What is the purpose of
getpriority()? - What does
setpriority()modify? - What does
PRIO_PROCESSrepresent? - How does
PRIO_PGRPdiffer fromPRIO_PROCESS? - When should
errnobe checked aftergetpriority()? - What Linux command changes the priority of a running process?
- Can scheduling priority affect CPU allocation?
- Which header file declares
getpriority()? - Why should priority changes be used carefully?
- What is the purpose of the
renicecommand?
21. Scheduling Policies
A scheduling policy defines the rules used by the operating system to select the next runnable process for execution. Different scheduling policies are designed for different workloads, such as interactive applications or real-time systems.
22. Common POSIX Scheduling Policies
| Policy | Description |
|---|---|
SCHED_OTHER |
Default time-sharing scheduling policy. |
SCHED_FIFO |
First-In, First-Out real-time scheduling policy. |
SCHED_RR |
Round-Robin real-time scheduling policy. |
The availability and behavior of scheduling policies depend on the operating system implementation.
23. Scheduling Class Selection
24. sched_yield()
The sched_yield() function voluntarily relinquishes the
processor, allowing the scheduler to select another runnable process.
25. Example Using sched_yield()
26. CPU Time Allocation
27. Linux Scheduling Commands
| Command | Purpose |
|---|---|
chrt |
Display or modify scheduling policy and priority. |
ps -eo pid,pri,ni,comm |
Display process priorities and nice values. |
top |
Monitor process priorities in real time. |
htop |
Interactive process monitoring (if installed). |
28. Best Practices
- Use the default scheduling policy unless specialized scheduling is required.
- Use real-time scheduling policies only when justified by application requirements.
- Call
sched_yield()only when voluntarily giving other runnable processes an opportunity to execute is appropriate. - Avoid excessive priority adjustments that could reduce overall system responsiveness.
- Test scheduling behavior on the target operating system.
29. Common Programming Mistakes
- Assuming all UNIX systems implement scheduling policies identically.
- Using real-time policies without understanding their impact.
- Calling
sched_yield()unnecessarily inside tight loops. - Ignoring the return value of scheduling-related functions.
- Confusing process priority with scheduling policy.
30. Interview Questions
- What is a scheduling policy?
- What is the purpose of
SCHED_OTHER? - How does
SCHED_FIFOdiffer fromSCHED_RR? - What does
sched_yield()do? - Which header file declares
sched_yield()? - When should a process voluntarily yield the CPU?
- Why are scheduling policies implementation-dependent?
- Can scheduling priority and scheduling policy both affect CPU allocation?
- Which Linux command displays scheduling policies?
- Why should real-time scheduling be used carefully?
31. Scheduling API Comparison
| Function | Purpose |
|---|---|
nice() |
Adjust the nice value of the calling process. |
getpriority() |
Retrieve the scheduling priority (nice value). |
setpriority() |
Modify the scheduling priority. |
sched_yield() |
Voluntarily relinquish the processor. |
32. Scheduling Policy Summary
| Policy | Typical Purpose |
|---|---|
SCHED_OTHER |
General-purpose time-sharing scheduling. |
SCHED_FIFO |
First-In, First-Out real-time scheduling. |
SCHED_RR |
Round-Robin real-time scheduling. |
33. Linux Scheduling Commands
| Command | Purpose |
|---|---|
nice |
Start a process with a specified nice value. |
renice |
Modify the nice value of an existing process. |
chrt |
View or change scheduling policy and priority. |
ps -el |
Display process scheduling information. |
top |
Monitor running processes. |
htop |
Interactive process monitor (if installed). |
34. Complete Scheduling Workflow
35. Best Practices Review
- Use portable POSIX scheduling interfaces whenever possible.
- Always verify return values from scheduling-related functions.
- Check
errnowhen required. - Use priority changes only when application requirements justify them.
- Avoid unnecessary use of real-time scheduling policies.
- Test scheduling behavior on the target operating system.
36. Common Programming Mistakes
- Ignoring return values from scheduling APIs.
- Confusing nice values with scheduler implementation details.
- Assuming scheduling behavior is identical across UNIX systems.
- Using real-time policies without appropriate privileges.
- Calling
sched_yield()unnecessarily.
37. Advanced Interview Questions
- What is the relationship between scheduling policy and process priority?
- Why should
errnobe checked aftergetpriority()? - How does
setpriority()differ fromnice()? - When should
sched_yield()be used? - What is the purpose of
PRIO_PROCESS? - Which scheduling policy is typically used for normal applications?
- Why are scheduler implementations operating-system dependent?
- What Linux utility modifies the priority of a running process?
- Why should real-time scheduling policies be used carefully?
- How do scheduling priorities affect CPU allocation?
38. Module Summary
This module explained the standardized UNIX scheduling interfaces used
to influence process execution order.
It covered scheduling objectives, process priorities, the
nice(), getpriority(),
setpriority(), and sched_yield()
functions, POSIX scheduling policies, Linux scheduling utilities,
recommended programming practices, and common implementation
considerations.
These concepts provide the foundation for understanding how operating
systems allocate CPU resources among runnable processes.
Concept Mastery Assessment — Module 36
Verify your conceptual understanding with 12 targeted technical assessment questions. Instant feedback and explanations are provided for every choice.