In this video, I will tell you about process priority.
So how does priority management work?
Well, in a generic point of view,
C-groups are the main way to manage priority,
because C-groups provide a framework
to apply resource restrictions to Linux systems.
C-groups can limit the amount of CPU cycles,
available memory, and more.
And if processes are equal from a perspective of C-groups,
the Linux nice and re-nice commands
can be used to manage priority.
Nice and re-nice is what you need to focus on
for your RHCSA exam.
But in the real world,
nice and re-nice are not that nice anymore,
and the focus is really on C-groups.
And that's why I want to tell you
a little bit about C-groups.
So in C-groups, the Linux system is divided
in three different slices.
These are three different areas.
The system slice, the user slice, and the machine slice.
So system slice is for system D processes,
user slice is for user processes,
and the machine slice is for virtual machines
and containers.
And by default, all slices are equal.
And that means that if one or more processes
within a slice request a maximum amount of CPU cycles,
each slice will get an equal amount of CPU shares.
It also means that if you have 20 processes
in the system slice that all require a lot of CPU cycles,
and one process in the user slice
that requires a maximum amount of CPU cycles,
there will be a 50-50 division.
And that's important to realize.
In system D, the CPU weight parameter
can be set on individual system units
if you want to change that behavior.
But again, this is too much for RHCSA.
And be happy about it, because on RHCSA,
you only need to know about the NICE command.
I needed you to know about system D C-groups,
because if you use NICE and suddenly something
hits in because of the C-groups, you're
not going to understand what NICE is doing.
But NICE is what you can use to specify CPU priority.
And as I mentioned, it works for processes
that are equal from a C-groups perspective.
And if suddenly you see that the process is getting
more priority, but you can't see that back in the NICE value,
well, think about the C-groups that
are probably messing it up.
Now, how do you work with NICE?
Well, to change priorities of non-real-time processes,
the NICE and re-NICE commands can be used.
The values to be used are in a range from minus 20
to positive 19.
Negative NICE values indicate an increased priority.
Positive NICE value indicates decreased priority.
And users can set their processes to a lower priority,
only to a lower priority.
If you want to increase priorities,
you need a root access.
Now, how does it work?
Well, you use a command like NICE minus N19,
followed by your command, which is running the command
with the positive priority of 19.
And that means that it is getting the lowest priority
that is available.
And priority is always relative to other processes.
So let me show you how this works.
So I'm getting back to DD.
And I'm using NICE minus N19, DD, IFS, dev0, and OFS, dev0,
running it as a background job.
And then I'm running another one.
And that is NICE minus N minus 5,
a slightly increased priority.
So the negative numbers means that the process
will claim more resource.
The positive number means that the process
claims less resources.
So how do we see that in top?
Well, that's surprising.
We see two different processes.
And both of them are running at full capacity.
Why is that?
Well, that is because this is a two-CPU system.
Honestly, if we want this to be a pure demo,
we temporarily need to switch off one of the CPUs.
And that's actually something that I can do by using echo 0
greater than slash sys slash bus slash CPU slash devices
slash CPU 1 slash online.
Whoa, what is that?
Well, the sys interface is a low-level interface
presented in the sys directory that you can use
to manage hardware properties.
And you can change the runtime behavior of your hardware.
And that's what I'm doing in this case.
I just offlined my CPU 1.
And that means that from the kernel scheduling perspective,
I am now on a one-CPU system.
And now we can clearly see the difference
between the nice process with the increased nice value
and with the decreased nice value.
So the nice value of minus 5 is giving my process 29662,
the highest priority.
And the other one is fading back to the background.
Interesting to know, you can also change nice values
from the top interface.
Just press R. So I'm going to re-nice process 29661.
And I'm giving it minus 20.
That's the most aggressive nice value that you can have.
And we wait for one polling loop.
And there you can see what is going on
and is really getting very many CPU cycles.
Now I'm using Q to quit top and kill all DD
because that is the smartest and fastest way to get
rid of these nice processes.
And let's not forget that I need to offline my CPU again.
Control-A to move the cursor to the start of the command line
and Echo 1 into this file.
And now my CPU 1 will be online again.