Linux Scheduling Priorities Explained

Posted: 11 May 2005

Scheduling Priorities aren't something most people think much of when using a computer; but once in a while, they can come in very handy when trying to figure out why your system is running abnormally slow while multitasking.

the problem

Running a process that takes a long time to complete, and uses a large amount of system resources, while you try to use the computer for other things will most likely cause your computer to behave very slowly.

Example: I want to run a script that will take 2000 music files and convert them to oggs. When I run the program, it will undoubtedly take 100% of the CPU cycles, and will probably take quite a long time to complete the task. I still want to use my computer in the meantime, but running other programs will undoubtedly be a very slow process. What can I do to make things run faster?

the solution

First off, Linux isn't magic. It can't make your computer do everything faster at the same time, but it does have quite a powerful scheduling system that tells the computer what program has first priority on the CPU.

What this means, is when you are running two programs at the same time, and your CPU is being asked to do more than it can, it has to switch between the two programs while working 100% of the time.

With Linux, you can declare which programs have higher priority than others, and if done right, this could significantly improve the over all performance of your machine.

Let's go back to my example:

I want to convert all my music to oggs, but I want to be able to work on my computer at the same time, and have it run just as fast as it normally does. Is it possible? Yes.

In this scenario, I would simply tell Linux that I want the music converter to have the lowest priority possible. This means that anything else I run in the meantime will have a higher priority, and can push the converter aside, get done what needs to be done, and then give the CPU back to the converter.

This means it'll take a little bit longer to convert your music, but it also means your computer will do other tasks quickly.

It's kind of like being the CEO of some business, and having 20 people want to talk to you at the same time. Is it possible to understand them all at once? No. But you can tell certain people that they'll just have to wait while you get the more important issues resolved. Once they're resolved, you can go back to working on the lesser issues.

actually doing it

The Linux scheduler system is usually referred to as the "nice" system or "nice levels" and the way you use it is actually quite easy.

*note* - Under normal circumstances, most people shouldn't have to change the nice settings at all. Only set the nice level manually if you need to.

The nice levels go from -20 to 19.

-20 is the lowest nice level, which gives it the highest priority. 19 is the highest nice level, which gives it the lowest priority. Just think of the nice level as "The ability of the program to play nice with other programs." The higher the nice level, the more the program will get out of the way of other programs. The lower the nice level, the more it will stop other programs from using system resources.

If I were running a script named "song_converter.pl" which I wanted just to use spare CPU cycles, or have the lowest priority of system resources, I would put the nice level high by issuing the following command:

jason@Predator jason $ nice -n 19 song_converter.pl

That will set the priority of the song_converter.pl program to the absolute lowest.

If you try to run a program with a very high priority, you will have to be the root user, or you'll get a message saying permission has been denied:

jason@Predator jason $ nice -n -20 song_converter.pl
nice: cannot set priority: Permission denied

Setting programs to high nice levels is a pretty good way to let an application run harmlessly in the background for a long period of time without slowing down your computer when you want it to do other things.



Informacja z serwisu http://www.djack.com.pl