In Linux, process priority determines how the kernel schedules processes for CPU time. Processes with higher priority are given more CPU time, while those with lower priority may have to wait longer to be executed. The Linux kernel uses two types of priority: nice value and real-time priority.
1. Nice Value (User-space priority)
- Range: The nice value ranges from
-20
(highest priority) to19
(lowest priority). By default, a process starts with a nice value of0
. - Effect: A lower nice value means higher priority. For example, a process with a nice value of
-10
has a higher priority than a process with a nice value of10
. - Usage:
Set nice value: You can set the nice value when starting a process using the nice
command.
nice -n 10 command_name
nice -n 10 process_id
Change nice value: After a process has started, you can change its nice value using the renice
command.
renice -n 5 -p process_id
2. Real-time Priority
- Range: Real-time priorities range from
0
(lowest) to99
(highest). Only processes with real-time scheduling policies can have real-time priority. - Scheduling Policies:
- SCHED_FIFO (First In, First Out): Processes are scheduled in the order they become runnable. Once a process starts running, it won’t stop until it finishes, blocks, or is preempted by a higher-priority process.
- SCHED_RR (Round Robin): Similar to
SCHED_FIFO
, but with time slices. Processes share the CPU time fairly. - SCHED_OTHER: The default Linux scheduling policy, used for normal tasks.
- Usage: You can set real-time priority using the
chrt
command.bashCopy codechrt -f -p 50 process_id
3. Checking Process Priority
- To view the priority of running processes, you can use commands like
top
orps
.bashCopy codetop ps -eo pid,ni,pri,cmd
4. Impact of Priority
- Higher Priority: A process with a higher priority (lower nice value or higher real-time priority) will get more CPU time.
- Lower Priority: A process with a lower priority (higher nice value or lower real-time priority) may have to wait longer for CPU time, especially if there are higher-priority processes running.
Example Commands:
- Start a process with a lower priority:bashCopy code
nice -n 10 ./your_script.sh
- Increase the priority of an existing process:bashCopy code
renice -n -5 -p 1234
- Set real-time scheduling policy for a process:bashCopy code
chrt -f -p 75 1234
Key Points:
- Nice values affect the relative priority of processes in the system.
- Real-time priorities are used for time-critical tasks.
- Use nice and renice for user-space priorities and chrt for real-time scheduling.
In this lesson, we will learn how to manage process priority.
Viewing Process Priority with nice
The nice
command is used to set or adjust the priority of a process.
nice -n priority command
- Replace
priority
with a value from -20 to 19.
Modifying Process Priority with renice
The renice
command allows you to change the priority of a running process.
renice priority -p PID
- Replace
priority
with a value from -20 to 19. - Replace
PID
with the process ID.
Remember, managing processes is a crucial aspect of Linux system administration. Understanding how to monitor, terminate, and adjust process priorities will help maintain a stable and efficient system.