Tech

Guides
 

Manipulate process priority with nice

By Vincent Danen, Special to ZDNet Asia
Monday, January 07, 2008 01:40 PM
Modern operating systems are multi-user and multitasking, which means that multiple users and multiple tasks can be using the computer at any given time.
Modern operating systems are multi-user and multitasking, which means that multiple users and multiple tasks can be using the computer at any given time. Typically you'll have one person using a desktop system running any number of applications or many users using many applications on a server.

The amount of time devoted to tasks largely depends on how intensive the task is. Some tasks require higher priority than others; for instance, if you were compiling a large software package you didn't need immediately, that priority should probably be lower than your Web browser or e-mail client.

Each process has a niceness  value associated with it, which is what the kernel uses to determine which processes require more processor time than others. The higher the nice value, the lower the priority of the process. In other words, the "nicer" the program, the less CPU it will try to take from other processes; programs that are less nice tend to demand more CPU time than other programs that are nicer.

The priority is noted by a range of -20 (the highest) to 20 (the lowest). Using ps, you can see the current nice value of all programs:

$ ps axl
F UID PID PPID PRI  NI  VSZ RSS WCHAN  STAT TTY TIME COMMAND
4   0   1    0  16   0 2648 112 -      S    ?   0:01 init [3]
1   0   2    1  34  19    0   0 ksofti SN   ?   0:02 [ksoftirqd/0]
5   0   3    1  10  -5    0   0 worker S<   ?   0:00 [events/0]
...

You can see that init  has a nice value of 0, while other kernel tasks associated with PID 2 and 3 have a nice value of 19 and -5 respectively.

Typically, a program inherits its nice value from its parent; this prevents low priority processes from spawning high priority children. Having said that, you can use the nice command (as root or via sudo) with the command you wish to execute in order to alter its nice value. Here is a short illustration:

# ps axl | grep axl | grep -v grep
4   0 30819 30623  15   0   4660   772 -  R+   pts/0 0:00 ps axl
# nice -10 ps axl | grep axl | grep -v grep
4   0 30822 30623  30  10   4660   772 -  RN+  pts/0 0:00 ps axl

You can see there that the nice value, represented by column six, has been altered. You can also use the renice  command to alter running processes. In the following example, vim was started to edit the file foo  and began with a default nice value of 0. Using renice, we can change its priority:

# ps axl | grep vim | grep -v grep
0   0 30832 30623  16   0  15840  3140 - S+  pts/0  0:00 vim foo
# renice -5 30832
30832: old priority 0, new priority -5
# ps axl | grep vim | grep -v grep
0   0 30832 30623  15  -5  15840  3140 - S<+  pts/  0:00 vim foo

Here, we have adjusted the priority of vim, giving it a slightly higher priority. Renice operates on the process ID, so using grep, we determined that vim is process ID 30832 and saw that the nice value was 0. After executing renice, the nice value is now -5.

Standard caveats apply: Only root can alter the nice priority of programs. So if you find that your compilation is taking too much CPU from other activities, consider renicing the parent process via root. Subsequent children should have a better nice value, or you can even start the compilation (or any other activity) with nice, specifying an appropriate nice value. You can also use renice to renice all programs belonging to a process group or user name/ID.



WORTHWHILE?

0

0 votes
Blog

Talkback 0 comments

There are currently no comments for this post.


Guest user

Guest user

Level: 
Joined: —
Already a member? Log in »



 

Loading...

Whitepapers/Case Studies

Downloads

Open Source News



Tech Jobs Now!

Tags

  1. canada
  2. e - mail
  3. fedora project
  4. gnome
  5. linux
  6. linux distribution
  7. mandriva
  8. mysql
  9. open source
  10. operating systems
  11. password
  12. red hat inc.
  13. repository
  14. security
  15. server
  16. ssh
  17. tool
  18. ubuntu
  19. vincent danen
  20. web