Manage application’s priority in Linux February 2, 2011Posted by Tournas Dimitrios in Linux.
Linux is a multitasking operating system , it can handle multiple tasks …… not at one’s but in sequential mode . And you may say : I have many applications simultaneously running ( news-reader , my cd play’s music and I’m resizing images with Gimp ) and they seems to run simultaneously . Exactly they ” seems” to run simultaneously … but actually they run in a sequential order , and that is the key point of this article . This sequential order is looping very fast and precisely controlled by the kernel , the final result is that the user receives the perception that all processes are running simultaneously .
One of the fundamental tasks of the Linux kernel is ensure that processes share system resources effectively. How the kernel decides which process gets to execute on the CPU at which time is known as scheduling.
Every process has two values which influence its scheduling. The first is a dynamic value which is constantly being changed by the kernel. The second is a fixed (static) value, which is only occasionally (if ever) explicitly changed by a user. The technical jargon for the first (dynamic) value is “process’s priority ” and the second (fixed) value as
” process’s niceness”.
Process Priorities :
When scheduling processes, the kernel effectively gives every process a handful of counters. Every time a process gets scheduled onto the CPU, it gives up one of its counters. When deciding which process to schedule onto the CPU next, the kernel chooses the runnable process with the most counters. Eventually, the kernel will reach a state where all of the runnable processes have used up their counters. This is referred to as the end of a scheduling epoch, and at this point, the kernel starts all of the processes over again with a new handful of counters.
Notice that processes which are not in the runnable state never give up their counters. If, however, a sleeping process were to suddenly awaken (because something interesting happened) and be kicked into the runnable state, it would most likely have more counters than processes which had been running for a while, and would be quickly scheduled onto the CPU.
Process Niceness :
As mentioned above, every process also has a static value referred to as its niceness value. This value may range from -20 to 19 for any process, starting at 0 by default. How does a process’s niceness influence its scheduling? At the beginning of a scheduling epoch, you can think of the kernel subtracting a process’s niceness value from the number of counters the process is allocated. As a result, “nicer” processes (those with a higher niceness value) get less counters, and thus less time on the CPU, while “greedy” processes (those with a niceness value less than 0) get more counters and more time on the CPU. If a “nice” process is the only one running on the machine, however, it would get full access to the CPU.
Altering the process niceness :
Say that you are rendering a video that is going to take a long time and can consume a good amount of your CPU cycles. You can set the nice value to give that process a lower priority so it won’t consume that much of your CPU. The job will take longer, but you won’t notice the huge hit on your processor. But how is this done? Let’s take a look at how you can manage the priority of an application.
- From the terminal with the “nice” and “renice” commands
- From the terminal using the “top” utility .
- With a gnome-based gui application : gnome-system-monitor .
From the terminal with the “nice” and “renice” commands
- The nice command is used to set a process’s niceness as the process is started. For example :
nice -5 cat /dev/zero > /dev/null &
Notice that the syntax can be misleading. The token -19 should not be considered negative 19, but instead the numeric command line switch 19.Since most shells run with a niceness of 0, this is seldom noticed. But if a shell were running with a niceness value of 10, the following command line would result in the simulation running with a niceness value of 15.
- The renice command can be used to change the niceness of an already running process. Processes can be specified by process id, username, or group name, depending on which of the following command line switches are used.
Switch Effect -p interpret remaining arguments as process ID’s (the default) -u interpret remaining arguments as usernames. -g interpret remaining arguments as group ID’s.
[root@Homeserver ~]# cat /dev/zero > /dev/null &
[root@Homeserver ~]# cat /dev/zero > /dev/null &
[root@Homeserver ~]# renice -5 5203
5203: old priority 0, new priority -5
[root@Homeserver ~]# renice 8 5202
5202: old priority 0, new priority 8
Notice the inconsistency in syntax. The renice command, unlike the nice command, does not expect the niceness value to be specified as a command line switch, but as an argument. In fact, renice -19 7347 would be interpreted as a niceness value of -19. Further muddying the waters, the output of the renice command refers to the value as a priority, instead of a niceness. (as some of the related documentation does, as well).
If you are wanting to renice a service or an application that was started with administrative privileges you must have administrative privileges to run renice, so you’ll need to either su to root or use sudo
From the terminal using the “top” utility
top has two main sections. The first displays general information such as the load averages, number of running and sleeping tasks, and overall CPU and memory usage. The second main section displays a sorted list of processes (usually by CPU usage) and showing their PIDs (Process ID number), user who owns the process, running time, and CPU and memory used by that process.As such top is useful even without any further configuration. Just starting it up provides a plethora of information about your system’s resource usage. If you just need to know what may be slowing your system down, for example, it might be enough to run top and see if there’s any process running at close to 100% of CPU time or using a little too much memory.
From inside the top application call the help screen with the “h” key , this reveals a lot of hot-keys (included nice , renice ) to manipulate your systems recourses .
top deserves a separate article by its own ,so that is a scheduled article for next posts .
With a gnome-based gui application : gnome-system-monitor
This graphical utility can be called through the terminal :
If you open up the System Monitor you will notice a Nice column (see Figure ). If you select an application and right-click it you will notice the Change Priority entry in the menu. Select that and a new window will appear with a slider allowing you to change the nice value from -20 to 20. Once you have changed that value click the Change Priority button and the new nice value is set.
You will probably notice that the majority of applications nice value is set to 0. That is normal priority. You will only want to change this value for certain circumstances – such as a run-away application or when an application will consume a good amount of CPU cycles. Other than that, Linux does a fairly good job managing the priority of the applications both the system and the users run. But it’s always nice to know you can step in when necessary.