1

I have a process that runs at about 100% CPU. So I run it with nice:

$ nice /dir/myamd.sh

The top command shows that my process still runs close to 100% (maybe slightly low like 99%). Is the nice command really working?

Thank you all for your responses! Can I use both nice and cpulimit? Because I want my process yield to others AND take only 80% CPU for example.

4
  • You should reread about it since nice is not meant to control the processing speed but the priority. If nothing else is running, then it remains the highest priority running task. Commented Jan 24, 2017 at 3:53
  • Welcome to Stack Overflow! I recommend you take the tour. Commented Jan 24, 2017 at 3:57
  • 2
    To see it actually making a difference, you could run another similarly CPU intensive job with a different priority and you'll see which one gets more CPU. Commented Jan 24, 2017 at 3:57
  • Note: These is a way to tell the cpu-freq controller to not up the cpu frequency for niced processes (but I can't remember how to do it). There is cpulimit (as you know), and you can run processes in a container (cgroups), and according to what I have read limit cpu (this may be better that cpulimit, but harder to use). Commented Jan 25, 2017 at 0:07

5 Answers 5

3

Nice alters the relative priority of the process. If there is nothing else that needs the CPU then there is still nothing else with a higher priority, so it can continue to use 100% of the CPU.

Lots of modern machines have multiple cores, so if you have 4 cores but only 3 processes that are able to run then this is another example where the process could use 100% of a core regardless of the nice level.

1
  • nice alters niceness. This in turn affects the dynamic priority, as does the behaviour of the process. Commented Jan 25, 2017 at 0:11
1

top and ps lax will display the current nice level of all the processes on the system in the NI column.

An analogy for nice levels and CPU load

Think of the CPU as a cashier at a checkout counter at a supermarket. If you have a multi-core CPU, there are several of these.

Each counter has a queue of customers waiting to pay for their groceries. This is the run queue of the CPU.

The length of the queue is the current CPU load. That's the load on the person behind the counter at this moment.

A person in the queue may be nice to the people behind, being totally fine with letting them get past them in the queue if they really need to hurry.

The cashier at the counter still has the same amount to work to do, and still works at the same pace (100%, until the queue is empty), but the people in the queue will arrive at the counter in an order depending on, amongst other things, how nice they are to other people in the queue.

In your case, the supermarket may be quite empty, but there's this one single customer with his really big trolley that needs to check out, and the cashier is working as hard as they can to process all the stuff he's buying. He is very nice though... but that doesn't really matter, the cashier won't slow down to have time for a chat.

On a Unix system, as opposed to at a supermarket, the CPU will only handle a process for a slice of time before sending them back into the queue. This is so that one single process won't block others.

You may even squeeze in context switching into this analogy. This is when the CPU switches from handling one process to handling another, and it can be time consuming to do it too often. This is basically what happens in-between serving customers at the counter; the time taken for the processed groceries to be put into bags etc. and the time taken for the next customer to move forwards, say "hi" etc.

Note: I'm using "CPU" above where in some cases I should say "kernel". From a user experience point-of-view, there's no difference.

0

The nice command is working as designed, just not as you expect it to.

Should you really want to cap the CPU usage and potentially waste available CPU resources, you might use:

cpulimit -l 25 /dir/myamd.sh

cpulimit will monitor myadm.sh CPU usage and throttle it using SIGSTOP/SIGCONT to keep its average usage under the 25% (of one vCPU) limit. On multi-core/threads systems, you can limit the usage to more than 100%.

A more efficient alternative is to use cgroups, see for example this blog for details.

0

You can use the ps command to check that you have correctly niced down your command:

$ nice -n 15 sm WILLY\ WILLIAM\ -\ Ego.m4a &> /dev/null &
[1] 29426
$ ps -lp $!
F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
0 S  1000 29426  2279  1  95  15 - 141822 poll_s pts/7   00:00:00 sm

You can see the niceness level (15, in this case) in the aptly named NI column. Once this is done, you should relax: all that is happening is that the program is running without competition for the use of system resources, if it still attains 100% of CPU.

1
  • Nope :). I'm afraid to tell you Linux made things more complicated. For example, by default, nice level is effectively disregarded between different TTYs - i.e. different terminal windows, or a terminal window v.s. the GUI. unix.stackexchange.com/a/277633/29483
    – sourcejedi
    Commented Jan 25, 2017 at 9:00
0

In your example, it is probably not working.

You're not running this program in the background (&). So this is probably the only program running on this TTY (apart from the shell that started it).

If you run a second program on a different TTY - e.g. a different terminal window - the relative nice levels of the two programs will have no visible effect.

https://unix.stackexchange.com/a/277633/29483

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .