Share this post
FaceBook  Twitter  
Contents[Hide]

1. Introduction

User's ability to attach a priority value to its own process upon execution determines whether you are being nice to your fellow users on the same system. Are you being nice or you simply abuse system resources for no apparent reason? In this article you will learn how to manage your processes in terms of how much processing power they consume and how to change a priority value of your processes using nice & renice Linux command.  We will start with some basic theory about what is process, process scheduling, how to fork new process and then we move to nice command and explain how to change process priority value.

2. What is process

In simple words a process is a naming convention used by Linux to assume role of a running program. A process is a collection of rules by which any particular program makes use of assigned processor time, memory and I/O resources. Each process running on a Linux system has its own Process ID ( PID ) by which it can be monitored and administered.

Linux kernel is designed to collect various information about each process. These include, but not limited to:

  • process status ( runnable, sleeping, zombie or stopped )
  • process execution priority ( niceness)
  • information about used resources
  • owner of the process
  • what network ports and files had each particular process opened
  • and more...

Now that we have some idea on what the process is we can go ahead and create some process. To do this simply open your terminal and execute yes command in background and redirecting its output to /dev/null:

$ yes > /dev/null &
[1] 5997

After that use ps -l command we cant extract the information about our yes process:

$ ps -l
F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
0 S  1000  5830  3283  0  80   0 -  6412 wait   pts/0    00:00:00 bash
0 R  1000  5997  5830 99  80   0 -  1757 -      pts/0    00:00:09 yes
0 R  1000  5998  5830  0  80   0 -  2399 -      pts/0    00:00:00 ps

 From the above we can read to following information:

  • F - FLAG: the process did not start with superuser privileges. Otherwise we would see number 4 or sum of 1 and 4. Check man ps for more info.
  • S - STATE: process is currently running
  • UID - User ID of user who initiated the process. UID is actual an alias for EUID ( Effective User ID )
  • PID - Process ID of our yes command is 5997
  • PPID - Parent Process ID. This is a process ID from which our yes command had been forked from. In this case it is bash with PID 5830.
  • C - Integer value of Processor utilization in % .
  • PRI - Process priority. The higher value the lower priority.
  • NI - Nice value with range from -20 to 19.  The higher value the more nice you are to other users in other words the higher value the lower priority.

2.1. Process scheduling

Linux kernel scheduler

This section can be skipped if you do not feel like to go into more details about process priority and scheduling. Here we will attempt to describe Linux process with some easy to understand rudimentary summarization as this topic can span multiple pages and that would be the only introduction.

From our point of view we need to understand that Linux ( Linux kernel version >= 2.6 ) scheduler is preemptive. This is a ability which allows kernel to preemptively choose to execute higher priority task over the ones with a lower priority.  Furthermore, kernel separates priority lists  to real-time tasks and user tasks raging from 1 - 100 and 101 - 140 respectively.

 Furthermore, Linux kernel assigns to higher priority tasks longer quantum time and lower priority tasks shorter quantum time this is approximately 200 ms and 10 ms, respectively. In other words every task is allowed to execute only if it has some remaining time slice. Therefore, shorter time slice to execute causes process to stay shorter time in active runqueue and thus consume less resources. Once the process time slice is empty, process is moved to expired runqueue where it's priority is re-calculate and then moved again to active runqueue. This relationship is illustrated on the diagram on your right. It is important to mention that both  where both active and expired runqueues contain lists of tasks sorted by their priority.

2.2. Process Life cycle

The basic principle of Linux process management contains two distinct operations when creating new process. The operation is where process copies itself by fork() call and thus creates a new process with unique PID. The fork operation is most commonly followed by exec() operation which executes a new program. The first process created during the boot time is init which is always assigned PID 1. All other processes are considered child processes of init process. Under normal circumstances before children process is allows to die this event needs to be acknowledged by a parent process by sending exit value. Successful termination send a parent process value 0. If from some reason child process outlives parent process init will accept this process as orphaned process.

3. Using nice command

Since the processing power was and still is growing exponentially over the years the importance of nice command is diminishing at the same pace. Therefore, as a result it is very today that you will be forced to change process priority manually. Nevertheless, this ability is there and it still may be useful in some situations. By default nice will set a nice level to 10.

$ nice yes > /dev/null &
[1] 5199
$ ps -l
F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
0 S  1000  3383  3380  0  80   0 -  6445 wait   pts/0    00:00:00 bash
0 R  1000  5199  3383 99  90  10 -  1757 -      pts/0    00:00:07 yes
0 R  1000  5200  3383  0  80   0 -  2399 -      pts/0    00:00:00 ps 

To start process with other nice value than 10 we can use -n switch.

$ nice -n 15 yes > /dev/null &
OR
$ nice -15 yes > /dev/null &
[1] 5270
$ ps -l
F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
0 S  1000  3383  3380  0  80   0 -  6447 wait   pts/0    00:00:00 bash
0 R  1000  5270  3383 99  95  15 -  1757 -      pts/0    00:00:02 yes
0 R  1000  5271  3383  0  80   0 -  2399 -      pts/0    00:00:00 ps

To set nice value below 0 root permissions are required. Nice will still start the program, however, the nice value will be set to 0. Here, we try to set nice value to -1 without root permissions:

$ nice -n -1 yes > /dev/null &
[1] 5285
nice: cannot set niceness: Permission denied
$ ps -l
F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
0 S  1000  3383  3380  0  80   0 -  6447 wait   pts/0    00:00:00 bash
0 R  1000  5285  3383 95  80   0 -  1757 -      pts/0    00:00:07 yes
0 R  1000  5295  3383  0  80   0 -  2399 -      pts/0    00:00:00 ps

Therefore, in order to set nice value to lower than 0 we need execute the above program as root or using sudo.

# nice -n -1 yes > /dev/null &
[1] 5537
# ps -l
F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
4 S     0  5428  3383  0  80   0 - 14430 wait   pts/0    00:00:00 su
0 S     0  5436  5428  1  80   0 -  7351 wait   pts/0    00:00:00 bash
4 R     0  5537  5436 87  79  -1 -  1757 -      pts/0    00:00:04 yes
4 R     0  5538  5436  0  80   0 -  2399 -      pts/0    00:00:00 ps

4. Using renice command

In the previous section we have learned how to start a program with pre-set nice value using nice command. Here, we try to change the nice value of a running program using renice command. Here, we have a running yes program with nice value of 10:

$ ps -l
F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
0 S  1000  3383  3380  0  80   0 -  6447 wait   pts/0    00:00:00 bash
0 R  1000  5645  3383 99  90  10 -  1757 -      pts/0    00:00:04 yes
0 R  1000  5646  3383  0  80   0 -  2399 -      pts/0    00:00:00 ps

To change its value we can use renice command and supply PID and nice value. Let's change nice value to 15:

$ renice -n 15 -p 5645
5645 (process ID) old priority 10, new priority 15
$ ps -l
F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
0 S  1000  3383  3380  0  80   0 -  6447 wait   pts/0    00:00:00 bash
0 R  1000  5645  3383 99  95  15 -  1757 -      pts/0    00:00:31 yes
0 R  1000  5656  3383  0  80   0 -  2399 -      pts/0    00:00:00 ps

The rule to follow is that non-super user can only increase the nice value ( give less priority ) to any process. If would would now try change to nice value from 15 back to 10 he following error would appear:

$ renice -n 10 -p 5645
renice: failed to set priority for 5645 (process ID): Permission denied

renice command also gives a root user the ability to change a nice value of any user's processes. This is done by -u switch. The following command will change a priority of all user's processes to -19.

# renice -n -19 -u lubos
1000 (user ID) old priority 0, new priority -19

5. Conclusion

Nice command can be a handy tool and is certainly easy to use. Please note that you can also use top command to renice process values.

Partners

Who are we?

LinuxCareer.com is not affiliated with any local or international company, nor is it a recruitment or employment agency. We specialise in Linux based careers and closely related Information Technology fields by providing careers advice and latest employment opportunities.

JOIN LINUXCAREER

You can also get involved in the LinuxCareer project by participating on our FORUM or SUBMITTING A LINUX ARTICLE. We offer a range of privileges to our authors and good company.