How to set the priority of a process
Introduction
Sometimes is important to set the priority of a process to be sure that this one has the highest one and other process can't affect it. In the following page, you can find 2 tools that can help us to do this. The first one is the nice command that tweaks the priority level of a process so it can run less frequently. The other one is cpulimit tool that sets the maximum ARM usage that an application should have.
Nice
The nice tool tweaks the priority level of a process. It is useful when you want to run a CPU task in background. The niceness level ranges from -20 (highest priority) to 19 (lowest priority). The nice tool is part of busybox, so you have to enable it. To do it, run the following commands:
cd $DEVDIR make config -> File System Configuration -> Select target's file system software [*] Busybox -> Busybox Configuration -> Coreutils [*] nice
Busybox has a tool named ps that you can use to check the current priorities of each process. To enable it, run the following commands:
cd $DEVDIR make config -> File System Configuration -> Select target's file system software [*] Busybox -> Busybox Configuration -> Process utilities [*] ps [*] Enable time and elapsed time output [*] Enable additional ps columns
Once you have the nice and ps tools enabled, you can check the current priorities of the processes and change them. All the process that starts without nice, will have a priority of 0. So for example, if you have an application named test that you are running in the background and you want to know the current priority for this process, you have to run the following command:
ps -o pid,nice,user,args
To set the priority of this application to the highest level, you have to run the following command:
nice -n -20 test
Now, you can check again the priority of test, running the ps command. You will see that the nice level (NI) change to -20.
To avoid another processes to affect the one with the highest level, it is important to set them to the lowest level.
There is an important thing to mention about nice, if you set the priority of a process which could start other ones, for example inetd, all the processes that inetd starts will have the same priority level of inetd. This is important because there are processes that consumes a lot of CPU (for example the FTP transfers) and each time that you start an FTP transfer, one process will be created. So, if you use nice to set the priority of the process that launches the FTP transfers, you would be setting the priority of each of the next FTP transfers as well.
The advantage of nice is that if the most important process is not doing anything the other processes can take advantage of that and use the processor as needed.
CPULimit
CPUlimit is an application that helps you to set the maximum ARM usage that an application should have. It will pause the process at different intervals to keep it under the defined limit. It is useful when you want to be sure that a process doesn't use more than a certain CPU usage.
So for example, if you have the test application and you want to be sure that it will not consume more than 10% of the CPU, you have to run the following command:
cpulimit -i -e test -l 10
Using the -l parameter, you define the maximum CPU limit for the application. You can check the CPU usage of a process using the top tool:
top -d1
The disadvantage of CPUlimit over nice is that it limits the CPU for the process, but if this process starts other processes, it will not limit the CPU for them.