06. Changing process priorities

As mentioned before, each process takes up a certain amount of CPU time. For every moment the CPU is busy running one process, another process loses valuable running time.

As you can imagine, there will be some instances when you are under time constraints and want to prioritize one job over others. Or other times you'll want to push a process back so that it can run quietly in the background while you tend to other things. To control such priorities, we may launch a program with the nice command, or modify a currently running process with renice.

Niceness

The term niceness comes from how "nice" the process is, in terms of how much CPU time it hogs. A nice process (as you would imagine) doesn't take too much CPU time and shares the CPU liberally with other processes.

This range of values goes from -20 to 20, where the lower the niceness, the more prioritized the process is. Thus, a "mean" process (one that hogs system resources) would have values close to -20, while a nicer process is closer to 20. The default niceness of any program is 10.

Syntax for nice

There are several ways you can call on nice:

$ nice -12 python longProgram.py
$ nice -n 12 python longProgram.py
$ nice --adjustment=12 python longProgram.py

As you can tell, we call nice, then specify any arguments before we input a command.

Syntax for renice

We can use the renice command to alter the priority of an already running process without disrupting its operation.

$ renice priority [[-p] pids] [[-g] pgrps] [[-u] users]

You may specify a process via its PID with the -p option. If you want to change the priorities of all processes under a certain group or user, use the -g and -u options.

$ renice 5 -p 21649
# Prioritizes the process with pid 21649
$ renice 19 -u SarahMarsh
# Deprioritizes all processes belonging to user SarahMarsh

Take your Linux skills to the next level!

The Linux Command Line

Take your Linux skills to the next level! Try Linux & UNIX

The Linux Command Line takes you from your very first terminal keystrokes to writing full programs in Bash, the most popular Linux shell. Along the way you'll learn the timeless skills handed down by generations of gray-bearded, mouse-shunning gurus: file navigation, environment configuration, command chaining, pattern matching with regular expressions, and more.

$ Check price
39.9539.95Amazon 4.5 logo(274+ reviews)

More Linux & UNIX resources

Aching back from coding all day?

Self-Massage Tool

Aching back from coding all day? Try Back Problems

Relieve spasms, tight muscles, trigger points and pressure points with the Body Back Buddy! This trigger point massage is designed to help you self-message any area of your body - especially those that are hard to reach. Keeping your muscles relaxes and out of contraction is importan in helping to reduce pain and prevent muscle injury.

$ Check price
29.9529.95Amazon 4.5 logo(3,443+ reviews)

More Back Problems resources

Ad