8

The niceness of a process decreases with increasing process priority.

Extract from Beginning Linux Programming 4th Edition, Pg 169 :

The default priority is 0. Positive priorities are used for background tasks that run when no other higher priority task is ready to run. Negative priorities cause a program to run more frequently, taking a larger share of the available CPU time. The range of valid priorities is -20 to +20. This is often confusing because the higher the numerical value, the lower the execution precedence.

Is there any special reason for negative values corresponding to higher process priority (as opposed to increasing priority for higher niceness valued processes) ?

NobodyNada
  • 7,529
  • 6
  • 44
  • 51
asheeshr
  • 4,088
  • 6
  • 31
  • 50
  • related: http://superuser.com/questions/391980/what-is-the-logic-behind-linux-nice-values – Thilo Dec 28 '12 at 09:12
  • I'd address this question to serverfault as it's not about programming, sorry. – Roman Newaza Dec 28 '12 at 09:13
  • Although it does speak to how programmers might think - just goes to show, not everything we do is clear :) – Ewald Dec 28 '12 at 09:14
  • It could be a bug: " POSIX does not define any semantics for the values that these functions get and set. As you will see, the Linux implementation is quite the inverse of what the authors of the POSIX syntax had in mind. " http://www.gnu.org/software/libc/manual/html_node/Priority.html – Thilo Dec 28 '12 at 09:14
  • @RomanNewaza I really dont see why this should be on ServerFault at all. Unix or SuperUser, maybe. – asheeshr Dec 28 '12 at 09:16
  • @AshRj, yes, you are right. – Roman Newaza Dec 28 '12 at 09:18
  • @AshRj as u have explaned.. The range of valid priorities is -20 to +20 but i think there is no +20 but it's +19. – akp Dec 28 '12 at 10:00
  • @akp My book states -20 to +20 while wiki says -20 to +19 so i dont know for sure. It probably will be uptill +19 – asheeshr Dec 28 '12 at 10:38
  • 1
    @AshRJ yes it's +19 sure...there are 40 priority from -20 to +19 including 0. and for more info read my answer. – akp Dec 28 '12 at 10:51

4 Answers4

3

@Ewald's answer is correct, as is confirmed by Jerry Peek et al. in Unix Power Tools (O'Reilly, 2007, p. 507):

This is why the nice number is usually called niceness: a job with a high niceness is very kind to the users of your system (i.e., it runs at low priority), while a job with little niceness hogs the CPU. The term "niceness" is awkward, like the priority system itself. Unfortunately, it's the only term that is both accurate (nice numbers are used to compute the priorities but are not the priorities themselves) and avoids horrible circumlocutions ("increasing the priority means lowering the priority...").

Nice has had this meaning since at least V6 Unix, but the V6 manual never explains this explicitly. The range of allowed values was -220 through +20, with negative numbers reserved for the superuser. The range was changed to -20 through +20 in V7.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
2

Yes - it gets NICER as the number goes up and MEANER as the number goes down. So the process is seen as "friendlier" when it's not taking up all the resources and "nasty" as it gets greedier with resources.

Think of it as "nice" points - the nicer you are to others, the more points you have.

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
Ewald
  • 5,691
  • 2
  • 27
  • 31
  • I find it hard to believe that, that is the only reason :) – asheeshr Dec 28 '12 at 09:19
  • @AshRj: still, this is it. The [Wikipedia page on nice](https://en.wikipedia.org/wiki/Nice_%28Unix%29#Etymology) cites Kernighan and Pike (1984) for the etymology, which can be regarded as an authorative source. – Fred Foo Dec 28 '12 at 09:29
  • 1
    @larsmans I just checked the source cited on the Wikipedia page,`The Unix Programming Environment`, page 35 as well as other instances, nowhere do the authors state this reasoning or for that matter any reasoning or explanation as to why the niceness value decreases. They only refer to the `nice utility` in two instances within the book. – asheeshr Dec 28 '12 at 10:23
  • @AshRj Yup - that's true - like the story about why the cursor is called the cursor... Because it prompts you to curse :) – Ewald Dec 28 '12 at 10:42
  • 1
    @AshRj: interesting -- even when Wikipedia does cite sources, you can't fully trust it. I just grepped the V6 Unix docset and nowhere does it explain the name "nice". – Fred Foo Dec 28 '12 at 13:48
2

Hysterical reasons - I mean historical... I'm pretty sure it started with numbers going up from 0 .. 20, and the lowest available was taken first. Then someone came to the conclusion that "Hmm, what if we need to make some MORE important" - well we have to go negative.

You want priority to be a sortable value, so if you start with "default is zero", you have to either make higher priority a higher number (but "priority 1" in daily speak is higher then "priority 2" - when your boss says "Make this your number 1 priority", it does mean it's important, right?). Being a computer, clearly priority 0 is higher than priority 1, and priority -1 is higher than priority 0.

In the end, it's an arbitrary choice. Maybe Ken Thomson, Dennis Ritchie or one of those guys will be able to say for sure why they choose just that sequence, and not 0..255, for example.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • Ritchie died over a year ago, but the Unix V6 manual describes the original `nice(1)` and `nice(2)`: the range of niceness values was -220..20, with negative values only available to the superuser. – Fred Foo Dec 28 '12 at 09:26
  • Yes, I'm aware that DR is no longer with us. I may simply be that it was an easy way to determine where the "special" level started - if it's less than zero, you have to be superuser. – Mats Petersson Dec 28 '12 at 09:39
  • Also, it makes the calculation simple -- just add the niceness to the priority (or subtract it if low numbers mean high priority). – Fred Foo Dec 28 '12 at 13:47
  • 1
    Right, so there's more to the scheduler than niceness - the niceness value is added into the priority calculation, it's not THE WHOLE priority of the process. It also takes into account IO activity and processor usage. Processes with high IO and low processor usage will get "boost" vs. those processes that use up a whole lot of CPU. This helps the system get good IO throughput, where they would otherwise have been blocked by cpu-intensive work that can be done later. – Mats Petersson Dec 28 '12 at 13:51
  • I like the comparison with how people speak about priorities in English. No way to prove it, but the explanation makes a lot of sense. – elifiner Feb 02 '15 at 18:17
2

First of all the answer is a little bit long but it is only for clarification.

As in the linux kernel every conventional process may have the priorities which are called static priority are from 100(highest) to 139(lowest). so there are basically 40 priorities which could be assigned to the process.

so when any process is created it gets the priority of it's parent but if the user wants to change it's priority then it could be done with the help of nice(nice_value) system call.

& the reason for your question is that every process wants base time quantum which is used as how much time the process will get the CPU for its execution in milliseconds and this is calculated as

time={
     if static_priority<120

       (140-static_priority)*20 

     if static_priority>=120

       (140-static_priority)*5

so The sys_nice( )service routine handles the nice( )system call. Although the nice_value may have any value, absolute values larger than 40 are trimmed down to 40. Traditionally, negative values correspond to requests for priority increments and require superuser privileges, while positive ones correspond to requests for priority decreases. In the case of a negative nice_value, the function invokes the capable( ) function to verify whether the process has a CAP_SYS_NICE capability. Moreover, the function invokes the security_task_setnice( )security hook. so in the end the nice_value is used to calculate the static priority & then this static priority is used for calculation of base time quantum.

so it's clear that -ve values are used for increment the priority so needs super user access & +ve values are used for decrease the priority so no need of super user access.

akp
  • 1,753
  • 3
  • 18
  • 26