|Home » Learning Curve
Be Nice With Nice
About Unix process priorities.
Several early upgraders to OS X 10.4 Tiger have complained about their systems coming to a standstill with Spotlight running and have considered using command line tools and AppleScript wrappers to change Spotlight's priority to ease the crunch.
Unix priorities range from +20 (CPU starvation) to -20 ('whoosh') with 0 as the default. Most of the time you won't need to interfere with process priorities, and this is of course by design. Running the ACP Lightman you can see that most Cocoa applications in fact have a zero priority and this is good.
There are several ways to set a process priority programmatically; there are two ways to set it and affect it from a command line interfacing with the Unix shell.
- nice - starts a process at a specified priority.
- renice - adjusts the priority of a running process.
Both tools have manpages.
The syntax for nice is straightforward.
nice [-n increment] utility [argument ...]
The '[argument ...]' bit is what gets passed to your program ('utility') as its command line. The '[-n increment]' bit is how you want to set the priority of the target. The '-n' is necessary because if no arguments are given the process gets bumped back ten notches automatically.
You could use this to start a superspeed TextEdit.
% sudo nice -n-10 /Applications/TextEdit.app/Contents/MacOS/TextEdit &
But it wouldn't do you much good as everything would get royally screwed up and you'd need 'sudo' again to kill it.
% sudo kill 978
+ Terminated sudo nice -n-10 /Applications/TextEdit.app/Contents/MacOS/TextEdit
Why It's Called Nice
The nice tool is used most often to reduce CPU consumption - and thereof its name: by relegating a process more to the background, the rest of the 'time sharing system' gets better access to resources and will run more smoothly.
The nice tool is in other words most applicable with long running 'batch' processes; for example, the first version of Unix at Bell Labs had a process calculating 'π'.
Any user can at any time start a process with a lower priority; starting a process with a higher priority is however the domain of the superuser (see above). Setting a priority below the default generally results in impaired system performance.
The Unix tool 'renice' will change the priority of running processes and generally works the same way. The simplest syntax is the following.
% renice <new priority> <process ID>
Again, lowering the priority of a process one owns requires no authentication, but raising it does.
% renice 1 978
978: old priority 0, new priority 1
% renice 0 978
renice: 978: setpriority: Permission denied
% sudo renice 0 978
978: old priority 1, new priority 0