1 00:00:06,600 --> 00:00:08,730 - In this video, we are going to explain what you 2 00:00:08,730 --> 00:00:11,460 can do to manage process priority. 3 00:00:11,460 --> 00:00:14,370 The thing is that every user process by default 4 00:00:14,370 --> 00:00:16,620 is started with the same priority 5 00:00:16,620 --> 00:00:21,270 And to adjust priorities, user can use the nice command 6 00:00:21,270 --> 00:00:24,390 to start processes with an adjusted priority. 7 00:00:24,390 --> 00:00:26,850 Non-privileged users can only run processes 8 00:00:26,850 --> 00:00:28,800 with a lower priority. 9 00:00:28,800 --> 00:00:30,840 Privileged users can increase 10 00:00:30,840 --> 00:00:33,240 and decrease process priority. 11 00:00:33,240 --> 00:00:35,730 And if you are going to use the nice command, 12 00:00:35,730 --> 00:00:39,150 the priority that you will use reaches from -20, 13 00:00:39,150 --> 00:00:42,630 which is the highest priority to positive 19, 14 00:00:42,630 --> 00:00:44,940 which is the lowest priority. 15 00:00:44,940 --> 00:00:47,760 If you want to adjust priorities for learning processes, 16 00:00:47,760 --> 00:00:49,230 you can use renice, 17 00:00:49,230 --> 00:00:53,100 but you can also use top to nice your processes. 18 00:00:53,100 --> 00:00:54,243 Let me demonstrate. 19 00:00:56,580 --> 00:00:59,730 All right, let's start with this nice command. 20 00:00:59,730 --> 00:01:04,470 Nice - - help just to show you how it is used. 21 00:01:04,470 --> 00:01:06,630 So we can see that nice is worked 22 00:01:06,630 --> 00:01:09,480 with options and command and argument. 23 00:01:09,480 --> 00:01:13,440 And the command is - n for the adjustment. 24 00:01:13,440 --> 00:01:17,160 So - n adds the integer n to the niceness. 25 00:01:17,160 --> 00:01:18,630 And if you don't do anything, 26 00:01:18,630 --> 00:01:21,420 the integer that is added is the 10. 27 00:01:21,420 --> 00:01:26,420 10 means that the process is becoming the least favorable, 28 00:01:29,490 --> 00:01:33,180 in terms of priority, because it's a positive value. 29 00:01:33,180 --> 00:01:34,830 You want it to have more attention, 30 00:01:34,830 --> 00:01:36,990 give it an a negative value. 31 00:01:36,990 --> 00:01:37,980 So if I use 32 00:01:37,980 --> 00:01:42,980 nice - n - 10 dd if=/dev/zero of=/dev/null 33 00:01:46,110 --> 00:01:50,460 So if I use nice - n - 10 on the dd command, 34 00:01:50,460 --> 00:01:52,320 I am getting permission denied. 35 00:01:52,320 --> 00:01:53,430 Does that make sense? 36 00:01:53,430 --> 00:01:54,810 Yes, it does. 37 00:01:54,810 --> 00:01:59,810 Because only user route can increase the priority. 38 00:02:00,150 --> 00:02:02,043 So I need to do in order to do that. 39 00:02:03,270 --> 00:02:04,290 So there we go. 40 00:02:04,290 --> 00:02:07,890 And oops, I'm running it as a foreground process now. 41 00:02:07,890 --> 00:02:08,910 I don't want to do that. 42 00:02:08,910 --> 00:02:11,730 So control z and bg to continue running it 43 00:02:11,730 --> 00:02:13,530 as a background process. 44 00:02:13,530 --> 00:02:16,710 Now I'm going to run it once more this time 45 00:02:16,710 --> 00:02:20,130 without the nice command. 46 00:02:20,130 --> 00:02:22,260 And then we are going to monitor and see 47 00:02:22,260 --> 00:02:24,033 if we see any differences. 48 00:02:25,290 --> 00:02:28,710 And this one needs to be a background process as well. 49 00:02:28,710 --> 00:02:31,020 Now, how can we best observe 50 00:02:31,020 --> 00:02:33,900 the differences and similarities between processes? 51 00:02:33,900 --> 00:02:34,950 By using top. 52 00:02:34,950 --> 00:02:36,660 And what do we see? 53 00:02:36,660 --> 00:02:38,430 We see something interesting. 54 00:02:38,430 --> 00:02:41,820 And the interesting thing is that we have two processes, 55 00:02:41,820 --> 00:02:45,120 and these two processes, these two nice processes, 56 00:02:45,120 --> 00:02:47,520 are running with the same priority. 57 00:02:47,520 --> 00:02:51,090 That's not what I expected, but it totally makes sense, 58 00:02:51,090 --> 00:02:53,520 because if you press one in top, we can see 59 00:02:53,520 --> 00:02:58,520 that this is a two CPU system and the dd process can 60 00:02:58,620 --> 00:03:00,990 run on one CPU at the same time. 61 00:03:00,990 --> 00:03:02,250 So it doesn't really matter 62 00:03:02,250 --> 00:03:05,130 that one of these processes is a higher priority. 63 00:03:05,130 --> 00:03:07,410 One of them is occupying one CPU, 64 00:03:07,410 --> 00:03:10,680 and the other one is occupying the other CPU. 65 00:03:10,680 --> 00:03:11,520 And you will notice 66 00:03:11,520 --> 00:03:13,530 that all the other processes will be running 67 00:03:13,530 --> 00:03:15,240 on the other CPU as well. 68 00:03:15,240 --> 00:03:16,710 And that's the result 69 00:03:16,710 --> 00:03:20,283 of this increased priority of this one dd process. 70 00:03:21,510 --> 00:03:23,100 Now you will see the difference, 71 00:03:23,100 --> 00:03:27,210 if we start one more dd process, there we go. 72 00:03:27,210 --> 00:03:30,960 And let's get back to top once more 73 00:03:30,960 --> 00:03:32,610 because what do we see now? 74 00:03:32,610 --> 00:03:36,870 We see process with PID 6525, 75 00:03:36,870 --> 00:03:39,690 which has a nice value of minus 10, 76 00:03:39,690 --> 00:03:44,490 and which has a CPU priority of 95%. 77 00:03:44,490 --> 00:03:47,190 And then we see the two other processes, 78 00:03:47,190 --> 00:03:51,180 the dd processes with the normal nice value 79 00:03:51,180 --> 00:03:55,530 and they are running with 47% CPU usage. 80 00:03:55,530 --> 00:03:57,210 Here you see the CPU usage 81 00:03:57,210 --> 00:03:59,190 and here we see the nice values. 82 00:03:59,190 --> 00:04:01,020 So zero is not adjusted. 83 00:04:01,020 --> 00:04:04,650 Minus 10 means we do have an adjusted nice value. 84 00:04:04,650 --> 00:04:08,250 And all this adjusted nice value influences a priority. 85 00:04:08,250 --> 00:04:11,880 By default user processes will learn with priority 20. 86 00:04:11,880 --> 00:04:16,773 If you tweak the nice value, then this priority is changing. 87 00:04:17,610 --> 00:04:22,230 Let me use r, r for renice, because from within top 88 00:04:22,230 --> 00:04:24,930 you can change priorities as well. 89 00:04:24,930 --> 00:04:28,290 I want to renice 6548. 90 00:04:28,290 --> 00:04:29,640 Notice that I'm running top 91 00:04:29,640 --> 00:04:32,040 at the moment as an ordinary user, 92 00:04:32,040 --> 00:04:36,390 and I am allowed to renice my own processes 93 00:04:36,390 --> 00:04:40,260 and I'm going to give it a priority 15. 94 00:04:40,260 --> 00:04:41,610 And what do we see? 95 00:04:41,610 --> 00:04:43,176 We see that process. 96 00:04:43,176 --> 00:04:46,200 Oh, it's descending in the list. 97 00:04:46,200 --> 00:04:50,370 We see that 6548 now as a priority 35, 98 00:04:50,370 --> 00:04:55,370 nice value of 15, and it almost gets no CPU cycles anymore. 99 00:04:56,310 --> 00:04:57,870 And that is how you can use nice 100 00:04:57,870 --> 00:05:00,363 to adjust process priority in Linux.