Now we're going to take a look now at the process priority.
These are columns that can be seen through our ps command in a long listing,
and we can adjust the allocated CPU time to a process.
We do this by adjusting the process's nice value.
It is least nice with a value of ‑20, and it's most nice with a value of +19.
The nice value adjusts the allocated CPU time.
Where the process is not so nice, it's going to have a higher allocated CPU time.
We can adjust the priorities using the nice command,
and for running processes,
we can readjust the nice value with the renice command.
So let's go through and take a look at this.
To begin with, I'm going to go through and start up one of those sleep commands.
So if I go through and run sleep, we can run the sleep command for 5000 seconds,
and we'll run it in the background.
Now if I go through then and look at my ps ‑lf for the process ID that's
been generated by the output of my pgrep and sleep command,
I should have just the one running now.
Part of the output includes the priority and the nice value.
We're running at the default nice value, which is 0.
Ordinary users can't start our processes with negative nice values.
Our nice value begins at ‑20, where it's least nice, getting the highest CPU
attention, going through to +19, where we're the nicest, and at the nicest
value, we're actually then taking the least CPU attention.
We can see this running that if I then go through and run nice ‑n for
the nice value of, let's say, 10, and start up then sleep with 5000,
run in the background, if I go through now and look at those, we can
see the priority has a higher value.
It is now 90.
Think of this almost as a queuing system.
We are further back in the queue now, and our nice value shows as 10.
The highest value that we can go through and put in is +19.
As I say, only root can start processes with a negative value, and we start
with 0. When I go through and take a look at the output now, we can see the
three processes running. The highest priority, which gives me the lowest
attention, is 99 with a nice value of 9.
Now I can take my running processes, so I can then go through and renice
to a nice value, let's say, of 15 for the process ID, let's say 3286, and
we can see then that its old priority was 0.
It's new priority is 15.
That's as far as the nice value is concerned.
When we go through and take a look at those processes, we can see,
then, it relates to 15 as the nice value, with a 95 priority,
moving towards the back of the queue.
Now the reality is, on a relatively quiet system, this will have little or no
effect, because it doesn't matter how many people are in the queue or what your
queue number is, there's nobody really contesting in front of you for CPU
utilization. But obviously, on a busier system, this can be a great way of
sharing the CPU time, where you don't need your task to be completed too
quickly. Where we do need a task to be completed, though, perhaps a little bit
more quickly, we can go through and do a sudo and renice. If I go through and
put, then, our nice value of ‑20 for our process ID of 3286, so when you're
using renice an ordinary user can't set it to a less nice value than it
currently is.
We certainly can't set negative values. But root can change the
nice value to any of the allowed settings.
When I go through and take a look at this now, we can see that comes in at a
priority of 60, with a nice value of ‑20. But as a standard user, I cannot
even go through and renice something to something that is less nice. So if I
go through and do a ‑n and, let's say, 10 for our process ID 3294, when I go
through and take a look at this, permission is being denied because it's
currently on +19. So I can't change it to something less nice.