Problem Statement
Explain process priority, nice values, and the renice command. How do you manage process priorities in Linux?
Explanation
Process priority in Linux is controlled by nice values ranging from -20 (highest priority) to 19 (lowest priority). Default nice value is 0. Lower nice values mean higher priority - the process gets more CPU time. The name 'nice' reflects that higher values are 'nicer' to other processes by taking less CPU.
Start processes with specific nice value using nice command: nice -n 10 command starts with nice value 10 (lower priority). Only root can set negative nice values (higher priority): nice -n -5 command. This prevents regular users from prioritizing their processes over system processes.
Change running process priority with renice: renice -n 5 -p PID sets PID's nice value to 5, or renice -n 5 -u username changes all processes for username. In top, press 'r' then enter PID and new nice value. Regular users can only increase nice values (reduce priority) of their processes, while root can set any value.
Use cases: background batch jobs get nice value 10-19 to avoid impacting interactive processes, critical services get nice value -10 to -5 for higher priority, and CPU-intensive tasks like rendering or compilation get higher nice values. Understanding nice values helps optimize resource allocation and prevent low-priority tasks from starving high-priority processes on busy systems.
Practice Sets
This question appears in the following practice sets:
