00:00:01 - Hello and welcome to this nugget, where we're going to be talking about
00:00:05 - Process Priorities as far as it relates to other priorities
00:00:09 - in the Linux System. Now this topic has a weight of
00:00:15 - two. So what that means is that in the LPIC Test you're probably
00:00:19 - only going to have a couple questions relating to to these
00:00:22 - topics but they're really key and integral when you're a system
00:00:26 - administrator to understand what's going on in your system.
00:00:28 - So we're gonna go, we're gonna go over this very thoroughly.
00:00:31 - Now before we start actually talking about how to manipulate
00:00:34 - things like Process Priorities and nice levels I should explain
00:00:38 - what a nice level even means. So I'm going to explain what niceness means.
00:00:42 - I'm going to show you some tools so that you can manipulate
00:00:45 - those, so you can identify those nice levels and then
00:00:48 - afterwards, I'll show you some tools so that you can manipulate those
00:00:51 - levels,so that the computer will bend to your every whim.
00:00:55 - But first of all what does it mean for a process to be
00:00:58 - nice? Well, just like people, if a process is nice, has a
00:01:03 - higher nice level, it's going to allow the other processes
00:01:07 - on the running system to have more of the processors' time and
00:01:12 - something with a lower nice level or not as nice is going
00:01:16 - to hog that CPU time, all to itself. Now it does get a little
00:01:20 - bit confusing. For example, there are numbers assigned
00:01:23 - to different nice levels and the numbers go all the way from
00:01:26 - a negative 20
00:01:28 - all the way to a positive 19.
00:01:33 - So now, if you remember back to school where you learned absolute
00:01:36 - values, this range gives you a range in the negative and
00:01:40 - the positive with zero being right around in the middle. Now what
00:01:44 - does that mean? Unfortunately it gets a little confusing because
00:01:47 - these negative numbers, so a negative nice level again means
00:01:52 - it has a higher priority and then a higher nice level means
00:01:56 - that it has a lower priority. And that confuses me and I have
00:02:00 - a little trick that sadly, I have to use more often than I'd
00:02:03 - like to admit. Basically let's think about a checkbook. If
00:02:07 - you have a checkbook and there is money in it. Well that's
00:02:10 - a good thing, but let's say your checkbook goes in the negative.
00:02:13 - Well if you have a negative number in your checkbook that is
00:02:17 - going to be like the highest priority in your life. So the
00:02:21 - same kind of thing is true with nice levels on the computer.
00:02:25 - So if you want to use my trick, if you have a checkbook in the
00:02:27 - negative it's a high priority. Well feel free to do that. Otherwise,
00:02:31 - just know that higher nice levels mean that it's nicer to the
00:02:35 - other processes running. So anyway, you have a grasp of basically
00:02:38 - how the Linux Kernel looks at different values for nice.
00:02:42 - So what is nice or how do you identify how nice programs are
00:02:46 - behaving.?Well, let's take a look.
00:02:49 - Okay, so the first command we're going to look at is the ps
00:02:53 - command. Now ps will just show you this the processes that are
00:02:56 - running. Now if run with no flags you'll just see the processes running
00:02:59 - in this current terminal window which is basically just bash
00:03:03 - which is my shell and ps, the command I just ran which isn't terribly
00:03:08 - useful as you can imagine. So what there are, are a bunch
00:03:11 - of different flags and you can look in the man page for ps
00:03:16 - and it will show you all these different flags and it has a
00:03:19 - bunch of different suggestions. And I'll just tell you a few that
00:03:22 - people commonly use. A lot of people like ps a-u-x.
00:03:28 - What that does it gives you -- oh, my goodness-- the screen full
00:03:32 - of information. But basically if you scroll up it will give
00:03:35 - you a column by column of what you have here. You have what
00:03:38 - user is running the program, the process ID of that particular
00:03:41 - program. As you can see it shows all the processes because it
00:03:44 - started you know, the very first processes in it; process two, process three, process four
00:03:48 - process five. How much CPU it's
00:03:51 - using. How much memory it's using. Some things I'm not even
00:03:54 - sure what all of these are to be quite honest but it gives
00:03:57 - you a whole bunch of information about the running processes.
00:04:01 - Some people like ps a-l-x which gives you
00:04:06 - the long view of all the running processes. The nice thing
00:04:10 - about this is it will also show you the, the nice level which
00:04:15 - the other one didn't do so again the user ID, process ID,
00:04:19 - the nice levels here. All of these different things
00:04:24 - along with the command way over here. I'll be quite honest. A lot
00:04:28 - of these have more information, again, you could tell when I
00:04:30 - would say things like I don't really even know this column
00:04:33 - is. So what I like to do is either do, you know, something really
00:04:37 - quick like ps a-u-x and then you know,
00:04:42 - grep, look for g-d-m, the known display manager and see, oh they're
00:04:46 - here, you know I have some information on that display manager that
00:04:50 - I was looking for. You can go through that stuff. But you can also make
00:04:54 - ps bend to your will -- wa ha ha ha. Basically you're in ps
00:05:01 - minus e for everything. It will show all the processes, and o for ouptut
00:05:05 - format. And that's where you can just go through and like say
00:05:08 - I want to see the user ID, the process ID. I want to
00:05:13 - see how much CPU it's using, the nice level, the
00:05:18 - command itself and if you do that --oh, see now it's
00:05:23 - just going to show you all of this stuff --
00:05:27 - oh, CPU actually.
00:05:30 - CPU was the wrong flag there that shows which CPU here;
00:05:37 - pcpu.
00:05:38 - There that's a little better.
00:05:41 - So what it's going to show us now is, it's going to show
00:05:44 - us the user name who is running the command, the process ID number, the
00:05:47 - percent of the CPU, the nice level that's been assigned
00:05:51 - to that process and the command itself. So again, ps is a
00:05:55 - very powerful command and you can have it do all kinds of things.
00:05:59 - You can limit it by you know the user that you see.
00:06:03 - You can limit it by certain type of processes, but ps is a tool that
00:06:06 - you're going to use a lot especially in combination
00:06:10 - with again piping it through that grep command.
00:06:15 - So for example, looking at that g-d-m process again, you see it's
00:06:18 - running two different processes. These are the process ID's started
00:06:21 - by root. So you can, you can find out a lot of information
00:06:26 - about running processes using ps and that's a tool that you're going to use
00:06:30 - a lot -- just to see what's running. If something is pegged as a CPU
00:06:33 - you'll be able to identify that. There are
00:06:36 - a couple better ways to identify that and we'll talk about that
00:06:39 - in just a minute. But before we we stop with the, with the ps
00:06:44 - thing here, I do want to show you -- now this is a command this
00:06:46 - is in the curriculum for for LPIC-1. So this is something
00:06:49 - you have to know but I've been a system admin for 15 years
00:06:53 - now and never have I used this command. So do with that what you will.
00:06:57 - The command is pstree. Now what pstree does it shows you
00:07:02 - a very visually appealing display of all the processes that
00:07:06 - are running in this parent child tree. So for example, everything
00:07:11 - is below init because that's the very first process that
00:07:14 - runs everything else. NetworkManager then has these child
00:07:17 - processes, g-d-m has these child processes and what, what child
00:07:22 - and parent means is that
00:07:24 - x-session manager actually started all of these so if the x-session
00:07:28 - manager dies all of these programs will die too. So again,
00:07:32 - it's nice visual representation of what's running on your system.
00:07:35 - But, apart from you know, maybe saying, wow, look at all that stuff running,
00:07:39 - I don't know that it's extremely valuable from a system administrator
00:07:43 - standpoint. But you need to know what it does because like
00:07:45 - I said it could very well be on the test. So ps and pstree --
00:07:51 - ps is something you're going to use a lot, pstree maybe not so much. But you know it
00:07:54 - looks nice, the output anyway and you need to know it.
00:07:59 - Okay, so we've explained the ps command. You're familiar with
00:08:02 - what ps does and especially and I know I've said it over and over but just
00:08:05 - really drive the point home, the two you're going to use with that
00:08:08 - a lot is grep. So, ps plus grep, again the one two punch
00:08:12 - combination for finding system programs running. We went
00:08:16 - over briefly what pstree does. Again, it shows that visual representation
00:08:21 - of the hierarchy of commands running in the Linux Kernel,
00:08:26 - which is just a fancy way of of showing you, you know who started
00:08:29 - what. And then what we have next are tools that -- one more
00:08:34 - tool to show things that are running. And top is is a really powerful
00:08:37 - tool. It's something that you're going to use it in different scenarios
00:08:42 - than you use ps, but it's something that's just as powerful and
00:08:45 - just as often used. So let's go take a look at that right
00:08:48 - right now. Here we are back in our Linux box. Let's open up a terminal
00:08:53 - and just type the command top t-o-p. Now what top does it shows
00:08:58 - you all the running commands and its a
00:09:02 - very convenient default that it has; is that it sorts the, the different
00:09:07 - rows here by the percent CPU column. So if you have an application
00:09:12 - like for example Firefox is known like if you're in a heavy
00:09:14 - duty Flashlight or something. A lot of times Firefox will either
00:09:18 - lockup or just use like 100% CPU and behave
00:09:22 - really really slowly and that's just, you know again, we'll
00:09:27 - see in another section that, you know, back when we talked about
00:09:30 - managing processes, you can actually kill it. But one of the things
00:09:33 - I want to point out in this nugget, is that the nice level,
00:09:38 - again the nice level here is displayed as well. So we can
00:09:42 - if you press H you can see that you can change the sort field
00:09:45 - by doing greater than or less than. So what we can do is if
00:09:49 - you really want to see, we'll sort by na, na, na, na, there sorted by the nice
00:09:56 - level. Okay, so all of these things have a zero nice level.
00:10:01 - So the nicest commands are going to be at the top, command of zero.
00:10:05 - Now let's do --I'm going to start a process of running
00:10:09 - in the background with a different nice level just so you
00:10:12 - can see it. I'll show you how to do that in the next section,
00:10:15 - but I'm going to get one going, so we can see it happening
00:10:21 - Okay so here you see now there's this command by user spowers
00:10:25 - which is me and I'm running a sleep command which doesn't
00:10:28 - do anything but sleep. But it's very useful for our purposes
00:10:31 - right now and it has a nice level of 10. Now what that means
00:10:35 - is, now again you remember the checkbook example, this
00:10:39 - means that it's nicer. It's using less priority than all
00:10:43 - of these other ones that are at a lower nice level, which means
00:10:46 - a higher priority. So top is a way that you can see what the
00:10:50 - nice level is and you can also -- you know we talked before about
00:10:54 - this kind of stuff. This is where nice is most often used,
00:10:56 - looking for the CPU stuff but for this nugget again, the nice level,
00:11:00 - the priority that it's, it's running at is key. And why I wanted
00:11:04 - to show you the CPU and the nice level thing. There
00:11:07 - are some scripts that will look for processes taking up a bunch
00:11:10 - of CPU time and instead of killing them off; what they'll
00:11:14 - do, is they'll assign them a nice level of 19. So what
00:11:19 - that effectively does it says, okay you can use a 100%
00:11:22 - of the CPU as long as no other application wants to do it.
00:11:26 - So if you're one of those people that run like Sedi at home or
00:11:29 - or one of those distributed computing programs, if
00:11:32 - it runs at a really high nice level like nice level 19,
00:11:37 - your system won't be affected by running slower pokey, but
00:11:41 - you can still use your CPU's idle time, if you will.
00:11:45 - So again this nice levels displayed in top and that's how you
00:11:48 - can see what, what nice level priority has.
00:11:53 - So that means that we now understand different ways that you
00:11:57 - can identify a computer's or a processes' nice level. So
00:12:02 - we've gone over top, we've gone over ps, and again I didn't just show you
00:12:06 - identifying the nice level. What that means as
00:12:09 - far as the CPU usage means too. So anyway, ps and top and now
00:12:15 - I'm going to take you to the next step. What if you want to
00:12:19 - manage what those levels are. You can actually start
00:12:23 - a process with a certain nice level which makes sense if you
00:12:26 - know that you want that to have a certain priority level. And
00:12:29 - if there's something already running you can renice that
00:12:32 - program. So change the level of that program's priority, the Kernel.
00:12:37 - So let's look at those right now.
00:12:40 - Okay so just a minute ago I ran that sleep command in the background,
00:12:43 - so that I can show you how it looked in top. Well, I'm going to do the
00:12:46 - same thing now. We're going to run sleep and we'll have it run for
00:12:51 - 5,000 seconds because if this nugget is going on in
00:12:54 - 5,000 seconds, I hope that you would have stopped it
00:12:56 - and gone and taken a nap. So we'll let that run, sleep for 5,000. We're
00:13:00 - just going to minimize this window, okay.
00:13:03 - So let's open up a new terminal window and what we're going
00:13:06 - to do -- if you start a process, it has a default level of
00:13:10 - zero. Okay, so if you start a process it gives it a level
00:13:13 - of zero. So let's look at that -- ps minus eo.
00:13:17 - Let's just look for the user name,
00:13:20 - the nice level and the command, okay.
00:13:24 - And let's look for; we know it's going to be sleep. Oop, not
00:13:29 - sleep; grep. We're going to grep the results for sleep. So here's what we have,
00:13:34 - all right. We have the user, me, spowers, the nice level of zero
00:13:39 - and the command is sleep, okay. So that's exactly what we would
00:13:44 - expect to see, because again, the default level; that command that you
00:13:46 - get when you start them is zero. So let's come back here.
00:13:50 - So we have that, that's the default level. What if we want
00:13:55 - the computer to run a command, no it doesn't have to be sleep,
00:13:57 - It can be like Firefox, it can be any program or you know a compiler,
00:14:02 - if you don't want to let you -- have your compiler hog
00:14:05 - up all of your computer resources, if you're compiling a big
00:14:08 - program. It can be any program you want to give a different
00:14:11 - priority level. But let's just keep using sleep because it's
00:14:14 - a very nice command. It just sits there and waits for us to
00:14:17 - jabber our jaws. So if you use the command nice is actually command
00:14:22 - as well, command
00:14:26 - nice and then what command you want to run.
00:14:29 - We're going to tell it to sleep 5,000 but again the
00:14:32 - nice command is starting it. So it's going to give it a different
00:14:37 - default level. If you don't specify what nice level
00:14:40 - it defaults to -- let's minimize this window again. Let's run that same exact command;
00:14:46 - again ps. We're looking for the sleep commands. So we're, you know,
00:14:50 - this format and then we're piping it through the grep
00:14:53 - command to look for sleep and you'll see that again I own it but the
00:14:58 - default nice level it gives it is 10 so which is about halfway
00:15:01 - from normal to super-duper nice lowest possible priority.
00:15:06 - It's kinda right there in the middle between zero and 19
00:15:09 - the highest level. So again, that's what happens if you just
00:15:13 - tell -- you know, just start a command with the nice program. So
00:15:17 - nice sleep 5,000 sets it at a level of --
00:15:21 - It sets it at 10. Let's say that 10 is not exactly what we want to use, okay.
00:15:29 - Let's say that we want to start; like say you want to play solitaire
00:15:35 - and you don't want anybody else to use the computer. You want
00:15:37 - to dominate the entire CPU or some other thing that makes
00:15:40 - a little more sense than playing solitaire. But what you can
00:15:42 - do is set the nice numbers. So nice minus n and and you can pick
00:15:45 - a number. Now remember the range is negative 20 for the
00:15:50 - highest priority all the way to a positive 19 for the
00:15:54 - least priority. So, if we want to make the computer
00:15:59 - sleep as hard as it can, or use the highest priority for sleeping,
00:16:05 - we'll do this -- uh, permission denied. Oh, no, what happened
00:16:11 - there? Let's try it some other way. Let's say nice minus n and
00:16:16 - we'll give it the lowest priority --
00:16:19 - sleep 5,000.
00:16:22 - Oh now it's not giving us a problem there, okay. So what happened? Well,
00:16:27 - if we do this command again we'll see sure enough it assigned that
00:16:29 - level of 19 which is what you'd expect. But how come
00:16:32 - it wouldn't let us do a negative number? Negative numbers are
00:16:35 - valid but you'll see it says Permission denied. Well, there's
00:16:40 - reasoning there and it's a good reason because what if you had
00:16:42 - somebody that really did want to play solitaire and give it
00:16:46 - that ridiculous nice level or somebody who is compiling
00:16:49 - a program in a shared environment and they said you know what,
00:16:51 - my program is more important than anybody else here. So I want
00:16:55 - to give it the highest priority. Well you have to be root to
00:16:58 - do that so either su as root or sudo and then
00:17:04 - the root user has the ability to set any level that makes
00:17:10 - sense again, negative 20 is the most possible or the highest
00:17:13 - possible priority. And sleep 5,000, it's going to ask
00:17:17 - me for my password.
00:17:20 - But it's not nic, it's nice.
00:17:23 - Computers are smart but you still have to actually know how
00:17:25 - to spell your commands. So nice and now since I did it as
00:17:29 - root -- if we look over here,
00:17:33 - oh, see sleep has a super-duper high priority. So anyway,
00:17:37 - that's how you can set something from the get-go with a certain
00:17:42 - level or a certain priority level, alright.
00:17:47 - Okay,so let's stop our ridiculously high processed
00:17:52 - nice command there alright and
00:17:56 - back here you'll see that it's not running anymore. There's no sleep command
00:18:01 - running. Let's say it was still running.
00:18:11 - See there it's running, but now it's running but we don't want it
00:18:15 - to be that high of a priority anymore. So what's a person to do?
00:18:19 - Well, let me give a real world example. Let's say you're in a
00:18:22 - lab and Johnny is running a game program and he gave himself
00:18:26 - somehow, well he's running at the default level of zero which
00:18:30 - you think is a little bit unfair for Johnny to be running, you
00:18:32 - know, his Halo demo at that level while the rest of
00:18:35 - the lab is trying to compile stuff on the server or some such
00:18:38 - thing like that. What you can do is renice things. Now if
00:18:42 - I am here. Now root owns that process now. You know I
00:18:47 - don't own that process now. See how when I owned it, it was
00:18:50 - at 19. Well now root owns it because it's below that, so I
00:18:54 - as a user here can't change that nice level. But,so sudo
00:18:58 - and we're going to renice, okay. And the process ID -- so
00:19:02 - minus p and then the process ID and you'll see that I didn't
00:19:05 - search for the process ID. So I don't know what it is.
00:19:09 - All right, well what we're going to have to do then is
00:19:12 - redo my command here. We have the user. And we're going to do the
00:19:17 - process ID.
00:19:20 - Aha, so now we have the user, the process ID and now we can
00:19:24 - renice it. So sudo,the root user, can do it again because
00:19:27 - it has that, that root is the owner -- sudo renice.
00:19:34 - Let's give it the nice level of 15. Okay, we'll pretend that
00:19:40 - Johnny's playing that game and we want to give him a low priority. So a renice
00:19:43 - to level 15, the process 10274,alright. Now it's going to ask me for my password.
00:19:51 - Old priority was negative 20. New priority is 15.
00:19:56 - So if we run our ps command again, it's going to show us
00:19:59 - that nice level has been changed. Now you can also use renice
00:20:03 - for a whole person. Let's say you're really upset at Johnny because he's
00:20:05 - running 12 copies of Halo and students are trying to get
00:20:08 - stuff done. Well what you can do is sudo, renice, we'll
00:20:15 - give him a level of 19, again the lowest priority level.
00:20:19 - And instead of minus p for the process we'll say minus u
00:20:23 - for Johnny. We'll say spowers because there is no
00:20:27 - Johnny but that's Sean Powers, I don't trust either, alright. So renice
00:20:31 - all those things and all of my processes used to have
00:20:36 - that the default level of zero. Now they have that level of 19, yikes!
00:20:41 - So now my computer system is going to be sluggish for
00:20:44 - me. I shouldn't have played Halo like that. So anyway that's
00:20:47 - the way that you can renice running applications.
00:20:51 - Okay so now we are professionals in the world of Process Priorities.
00:20:56 - We've learned how to start processes with nice at different
00:20:59 - levels, whether it's a higher nice level which means a lower
00:21:03 - priority or a lower nice level which means a higher priority.
00:21:06 - Again, remember my old checkbook scenario, if that confuses you.
00:21:10 - Then we also learned how to renice applications,
00:21:14 - so that we can change them. Now again it's really important
00:21:16 - to remember that if you're trying to lower the nice level or
00:21:20 - gives something a higher priority than zero you have to be
00:21:25 - root to do that. I guess the only exception would be like
00:21:28 - if you have an application running and you're a user and somehow
00:21:31 - you're applications running at negative 15. Well you do
00:21:34 - have the right to give it a lower priority of like negative
00:21:38 - five. It's just you can't raise a priority of a process to
00:21:43 - something less than zero unless you're root. That's just
00:21:47 - the way to protect people from, you know, taking over their
00:21:51 - CPU of their computer without root access. So anyway
00:21:54 - we've gone through the whole process. You understand nice.
00:21:57 - You know how -- what tools to use to identify what levels
00:22:02 - of priorities different processes have. And you know how to set
00:22:05 - and change those different priority levels. So I hope that
00:22:09 - this has been informative for you and I would like to thank
00:22:12 - you for viewing..