How does process scheduling work in Linux and what is the nice value?
Learn how Linux process scheduling works with the CFS, how virtual runtime ensures fairness, and how the nice value from -20 to +19 tunes CPU priority.
Expected Interview Answer
Linux process scheduling is how the kernel decides which runnable process gets the CPU next and for how long, and the nice value is a per-process hint (from -20 to +19) that biases how much CPU time a process receives.
The default scheduler, the Completely Fair Scheduler (CFS), aims to give every runnable task a fair share of CPU time by tracking each task's virtual runtime and always running the one that has received the least. The nice value weights this fairness: a lower nice value (down to -20) increases a task's share, while a higher value (up to +19) makes it 'nicer' and yields CPU to others. Real-time scheduling policies such as SCHED_FIFO and SCHED_RR sit above CFS for latency-sensitive tasks and always preempt normal tasks.
- CFS distributes CPU time fairly among tasks
- nice values let admins prioritize workloads
- Real-time policies serve latency-critical processes
- Preemption keeps the system responsive
- renice adjusts priority of running processes
AI Mentor Explanation
The scheduler is like a captain rotating the strike so every batter gets fair time at the crease, always sending in whoever has faced the fewest balls. The nice value is like a pre-agreed instruction to give a key batter extra deliveries (low nice) or ask a tail-ender to yield strike (high nice) without breaking the fairness idea.
Step-by-Step Explanation
Step 1
Identify runnable tasks
The kernel maintains a run queue of processes ready to use the CPU.
Step 2
Pick by virtual runtime
CFS selects the runnable task with the smallest accumulated virtual runtime to keep CPU sharing fair.
Step 3
Apply nice weighting
A task's nice value scales how fast its virtual runtime grows, so low-nice tasks accumulate slower and run more.
Step 4
Honor scheduling classes
Real-time classes (SCHED_FIFO, SCHED_RR) preempt normal CFS tasks whenever they are runnable.
Step 5
Preempt and repeat
When a slice ends or a higher-priority task wakes, the current task is preempted and selection repeats.
What Interviewer Expects
- Explanation of CFS and fairness via virtual runtime
- Understanding of the nice range -20 to +19
- How lower nice means higher priority
- Awareness of real-time scheduling policies
- Knowing nice sets initial priority while renice changes a running process
Common Mistakes
- Saying a higher nice value means higher priority
- Confusing nice value with the raw kernel priority number
- Thinking any user can lower nice below zero without privileges
- Ignoring real-time policies that preempt normal tasks
- Believing the scheduler runs tasks strictly round-robin regardless of weight
Best Answer (HR Friendly)
“Linux constantly decides which program gets to use the processor next, trying to share time fairly among everything that wants it. The nice value is a knob that says how greedy a program is allowed to be: a low value gives it more CPU time, and a high value makes it politely let other programs go first.”
Code Example
# Start a job with a lower priority (nicer, yields CPU)
nice -n 10 ./backup.sh
# Start a job with higher priority (needs root for negative nice)
sudo nice -n -5 ./encode.sh
# Change the nice value of a running process by PID
renice -n 5 -p 4821
# View nice (NI) and priority (PRI) columns
top -o %CPUFollow-up Questions
- How does CFS use virtual runtime to decide which task runs next?
- What is the difference between SCHED_FIFO and SCHED_RR?
- Why does lowering the nice value below zero require privileges?
- How does renice differ from nice?
- What is priority inversion and how can it be mitigated?
MCQ Practice
1. Which nice value gives a process the MOST CPU priority?
The nice range is -20 (highest priority, greediest) to +19 (lowest priority, nicest), so -20 gives the most CPU share.
2. What does the Completely Fair Scheduler primarily track to choose the next task?
CFS runs the task with the smallest virtual runtime so every runnable task converges toward a fair share of CPU time.
Flash Cards
What scheduler does Linux use by default? — The Completely Fair Scheduler (CFS), which shares CPU by picking the task with the lowest virtual runtime.
What is the nice value range? — -20 (highest priority) to +19 (lowest priority); lower means more CPU time.
Difference between nice and renice? — nice sets priority when launching a process; renice changes the priority of an already running process.
What are real-time scheduling policies? — SCHED_FIFO and SCHED_RR give latency-sensitive tasks priority that preempts normal CFS tasks.
Continue Learning
Related Interview Questions
What is a zombie process and an orphan process in Linux?
medium
What is the difference between a process and a thread in Linux?
medium
Why does free -m show almost no free memory, and what does 'available' really mean?
hard
How does the Linux OOM killer choose a victim, and how do you investigate an OOM kill?
hard