Need explanation on pri standard format specifier

2020-06-30 05:12发布

问题:

Note on pri from ps man page:

"pri PRI priority of the process. Higher number means lower priority"

Consider PID 26073 here

$ renice +15 26073 


26073: old priority 5, new priority 15 # I am making this process more nice 

$ ps -t 1 -o pid,ppid,%cpu,stat,cmd,bsdstart,time,pri

  PID  PPID %CPU  STAT CMD      START     TIME PRI
 9115 18136  0.0   Ss   bash     17:10 00:00:01  19
26073  9115 12.0   RN+  p4 sync  19:06 00:02:56   4

STAT = RN+ which means : Running , low-prio ( nice to others), foreground. PRI=4 (1)

$ sudo renice -10 26073 

26073: old priority 15, new priority -10 # I am making this process less nice

 $ ps -t 1 -o pid,ppid,%cpu,stat,cmd,bsdstart,time,pri

  PID  PPID %CPU STAT CMD       START     TIME PRI
 9115 18136  0.0  Ss   bash     17:10 00:00:01  19
26073  9115 12.0  S<+  p4 sync  19:06 00:03:15  29

STAT = S<+ which means : Interruptible sleep , high-prio ( not nice to others), foreground. PRI=29 (2)

In case 2 the process priority increased or to say it another way the process became higher priority.

But this contradicts what definition of pri says from man page ( that higher number means lower priority)

回答1:

You are being confused by PRI (immediate priority) vs. NICE (the assigned priority). PRI often gets a boost (i.e. lower value) when a process is being restarted after blocking on I/O, and conversely is lowered (higher value) if it uses up its scheduler-assigned time slot without blocking, at least with the standard scheduler. Many systems have alternative schedulers with different behaviors, but in all cases PRI is the actual current priority that the scheduler has assigned; this value is influenced by, but not defined by, the assigned "niceness".

Reference on Linux's priority management here: http://oreilly.com/catalog/linuxkernel/chapter/ch10.html



回答2:

Although I'm not an expert on the linux scheduler, I do know that it 'punishes' CPU bound processes and rewards I/O bound processes (something most schedulers do to a greater or lesser extent). As explained, this and other adjustments, along with the NICE value, result in an internal priority setting within the scheduler. The fact that they use an inverse NICE value and a non-inverse internal PRI value is somewhat confusing, but makes sense.