Processes and signals

INPUT · Slides

Give up your place in the queue (`nice`)

01 / 07

There is a queue

This Linux has one CPU. Which means only one program can truly run at a time.

But last lesson you ran three sleeps at once. How did that work?

it hands the turn round bit by bit (switching thousands of times a second)

It is only too fast to see. Inside the kernel there is a queue, and something decides who moves next. That something is called the scheduler.

Normally everyone queues equally. But you will think this sometimes.

> this job is in no hurry, it can come later

The tool for saying so is nice. It makes something stand back in the queue.

02 / 07

The bigger it is, the more it stands back

The nice value is a number from -20 to 19. Watch the direction.

-20  <- the strongest (me first)  0  <- ordinary (the default) 19  <- standing furthest back (after you)

Bigger means weaker. That is the part that goes against instinct.

Remember it by the name. nice means kind, good-natured, so

> a big nice value = very nice = letting others go first

It runs the opposite way round from saying "high priority", so take care not to mix the two.

nice is big    ->  the priority is lownice is small  ->  the priority is high

Type just nice to see the current value. Your shell should be 0.

03 / 07

Start it with nice -n

To settle the value as you start, put it in front of the command.

~ $ nice -n 10 sh heavy.sh &

Read it as "stand back by 10 and run sh heavy.sh". The -n is the n of number.

The shape is always this.

nice -n value the-command-you-want

nice is built in an unusual way: once its own job is done it turns into that command. So no name nice is left when you look with ps.

Checking the value is a little odd in this busybox. There is no nice column in ps, so you read it from /proc.

~ $ awk '{print $19}' /proc/84/stat10

The 19th field of /proc/number/stat is the nice value. The same place where you read the parent number (the 4th) last time.

You can see it in top too: an N goes up in the STAT column (like SN).

04 / 07

Change something already running with renice

To change the value of something already going, there is renice.

~ $ sleep 300 &[1] 84~ $ renice 19 84

nice is for "what you are about to start", renice for "what is running now".

renice points with the PID. Job numbers (%1) do not work, so you get the number with $! or pidof.

~ $ sh heavy.sh &~ $ renice 19 $!

You can hand it several at once.

~ $ renice 19 $(pidof sleep)

The number means something different in nice and renice, so watch just that.

nice -n 10 command    add 10 to the current valuerenice 19 PID         make it 19 (not adding)

nice is addition, renice is overwriting.

05 / 07

Standing back is free, coming forward is root only

Here is the important bit. You can only move in the standing-back direction.

~ $ nice -n -5 sleep 30nice: setpriority(-5): Permission denied

Try to go negative and you are refused.

Nor can you undo a value you have raised.

~ $ renice 19 84     goes through~ $ renice 5 84      renice: setpriority: Permission denied

One way only.

The reason is that if everyone could prefer themselves, the whole idea of priority would mean nothing. The rule is that only root gets to say "me first".

This joins up with chapter 7. root passes through everything, and in exchange an ordinary user cannot make themselves stronger.

So in practice it comes to this.

make your own heavy work stand back    -> any time you likemake your own work go first            -> ask an administrator

Learning the first is enough.

06 / 07

It only works on the CPU queue

What nice changes is only the turn at the CPU.

Let us line up what it does not change.

ChangesDoes not change
the turn at the CPUhow much memory is available
(speed when it is busy)how fast the disk reads and writes
how fast the network is

So depending on the trouble, nice does nothing.

the CPU is full (idle at 0%)   -> nice worksthere is not enough memory     -> it does notwaiting on disk (io is high)   -> hardly at all

Remember looking at idle and io in top last lesson. Look with top first at what is jammed, and if it is the CPU then nice has its turn.

One more thing. When nothing else is going on, even nice 19 runs flat out. It stands back only when it is busy. Which is why "putting it on work that is in no hurry, just in case" costs you nothing.

07 / 07

Now have a go

This is everything for the lesson.

nice                     see the current valuenice -n 10 command       start it standing backrenice 19 PID            overwrite something runningawk '{print $19}' /proc/PID/stat   read the valuetop -b -n1               look for the N in STAT

The direction of the value, once more.

0 -> towards 19 = stand back (anyone can)0 -> towards -20 = go first (root only)

Here are some times you would use it.

  • converting video for hours, running between other work
  • running a backup in the daytime
  • packing up piles of files (tar, in chapter 9)

All of them jobs where finishing a bit later is fine. Let work that can wait stand back, and work that cannot gets lighter.