Adjusting Process Priority with nice  

When Linux processes are started, they are started with a specific priority. By default, all regular processes are equal and are started with the same priority, which is the priority number 20. In some cases, it is useful to change the default priority that was assigned to the process when it was started. You can do that using the  nice  and  renice  commands. Use  nice  if you want to start a process with an adjusted priority. Use  renice  to change the priority for a currently active process. Alternatively, you can use the  r  command from the  top  utility to change the priority of a currently running  process.

Changing process priority may make sense in two different scenarios. Suppose, for example, that you are about to start a backup job that does not necessarily have to finish fast. Typically, backup jobs are rather resource intensive, so you might want to start it in a way that it is not annoying other users too much, by lowering its priority.

Another example is where you are about to start a very important calculation job. To ensure that it is handled as fast as possible, you might want to give it an increased priority, taking away CPU time from other processes.

On earlier Linux versions, it could be dangerous to increase the priority of one job too much, because other processes (including vital kernel processes) might risk being blocked out completely. On current Linux kernels, the situation is not that urgent anymore:

■ Modern Linux kernels differentiate between essential kernel threads that are started as real-time processes and normal user processes. Increasing the priority of a user process will never be able to block out kernel threads or other processes that were started as real-time processes.

■ Modern computers often have multiple CPU cores. A single threaded process that is running with the highest priority will never be able to get beyond the boundaries of the CPU it is running on.

When using nice or renice to adjust process priority, you can select from values ranging from -20 to 19. The default niceness of a process is set to 0 (which results in the priority value of 20). By applying a negative niceness, you increase the priority. Use a positive niceness to decrease the priority. It is a good idea not to use the ultimate values immediately. Instead, use increments of 5 and see how it affects the application.

TIP Do not set process priority to -20; it risks blocking other processes from getting served.

EXAMPLES:

Let’s take a look at examples of how to use nice and renice . The command nice -n 5 dd if=/dev/zero of=/dev/null & starts an infinite I/O-intensive job, but with an adjusted niceness so that some place remains for other processes as well. To adjust the niceness of a currently running process, you need the PID of that process. The following two commands show how ps aux is used to find the PID of the dd job from the previous example. Next, you see how the renice command is used to change the niceness of that command:

[root@rhel7 tmp]# nice -n  dd if=/dev/zero of=/dev/null &
[]
[root@rhel7 tmp]# jobs
[]+ Running nice -n dd if=/dev/zero of=/dev/null &

1. Use ps aux | grep dd to find the PID of the dd command that you just started. The PID is in the second column of the command output.

[root@rhel7 tmp]# ps aux | grep dd
root 0.0 0.0 ? S Jun27 : [kthreadd]
root 0.0 0.0 ? S< Jun27 : [ipv6_addrconf]
dbus 0.0 0.0 ? Ssl Jun27 : /bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation
root 99.9 0.0 pts/ RN : : dd if=/dev/zero of=/dev/null
root 0.0 0.0 pts/ R+ : : grep --color=auto dd

2. Use renice -n 10 -p 1234 (assuming that 1234 is the PID you just found). 
Note that regular users can only decrease the priority of a running process. You must be root to give processes increased priority.

[root@rhel7 tmp]# renice -n  -p
(process ID) old priority , new priority

使用nice命令调整进程优先级的更多相关文章

  1. centos7 的防火墙命令调整了

    CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙.firewall:systemctl start firewalld.service#启动firewalls ...

  2. Ubuntu—终端命令调整窗口的大小

    1,查看窗口大小 current 1280x768 是我当前电脑的窗口大小,下面提供的是可以修改的窗口大小. $ xrandr 2.修改窗口大小 示例: $ xrandr -s 1024x768

  3. Linux改变进程优先级的nice命令

    前言: VPS普遍性能不高,很多人可能有这样一个感受,在执行du.tar等命令时,会造成系统负载飙升,Apache响应缓慢.这时nice命令改变进程优先级可能能缓解这种状况.nice命令用于调整Lin ...

  4. Linux的进程优先级

    Linux的进程优先级 为什么要有进程优先级?这似乎不用过多的解释,毕竟自从多任务操作系统诞生以来,进程执行占用cpu的能力就是一个必须要可以人为控制的事情.因为有的进程相对重要,而有的进程则没那么重 ...

  5. Linux进程优先级系统——设置实时进程优先级

    前言 最近研发的产品出了点小bug,最后查到根本原因是,其中一个进程A使用基于FIFO的实时进程优先级,而另一个进程B是使用普通调度的进程优先级,而A和B两个进程是互相通信的,进程B会被饿死,而进程A ...

  6. CentOS命令行性能检测工具

    一.uptime Uptime命令的显示结果包括服务器已经运行了多长时间,有多少登陆用户和对服务器性能的总体评估(load average).load average值分别记录了上个1分钟,5分钟和1 ...

  7. Linux就业技术指导(五):Linux运维核心管理命令详解

    一,Linux核心进程管理命令 1.1 ps:查看进程 1.1.1 命令解释 功能说明 ps命令用于列出执行ps命令的那个时刻的进程快照,就像用手机给进程照了一张照片.如果想要动态地显示进程,就需要使 ...

  8. Linux的进程优先级NI和PR

    为什么要有进程优先级? 这似乎不用过多的解释,毕竟自从多任务操作系统诞生以来,进程执行占用cpu的能力就是一个必须要可以人为控制的事情.因为有的进程相对重要,而有的进程则没那么重要. 进程优先级起作用 ...

  9. (转)Linux常用性能检测命令

    一.uptime     Uptime命令的显示结果包括服务器已经运行了多长时间,有多少登陆用户和对服务器性能的总体评估(load average).load average值分别记录了上个1分钟,5 ...

随机推荐

  1. C++ 数组作为函数参数时,传递数组大小的方法

    废话不多说,先上错误示范: void fun(int arr[arr_num]) { // ... } int main() { // ... int *arr = new int[10]; fun( ...

  2. 【USACO 2.1.1】城堡

    [题目描述] 我们憨厚的USACO主人公农夫约翰(Farmer John)以无法想象的运气,在他生日那天收到了一份特别的礼物:一张“幸运爱尔兰”(一种彩票).结果这张彩票让他获得了这次比赛唯一的奖品— ...

  3. mktime性能问题

    #include <time.h> int main() { for (int i = 0; i < 100000; ++i) { struct tm tm = {}; tm.tm_ ...

  4. ch01.深入理解C#委托及原理(转)

    ch01..深入理解C#委托及原理_<没有控件的ASPDONET> 一.委托 设想,如果我们写了一个厨师做菜方法用来做菜,里面有 拿菜.切菜.配菜.炒菜 四个环节,但编写此方法代码的人想让 ...

  5. UI基础 - UILabel

    //根据文字获取size - (CGSize)getSizeWithstring:(NSString *)string { CGSize maxSize = CGSizeMake(320, 320); ...

  6. 移动app测试浅析

    移动App测试浅析 1. 移动App测试的现状及其挑战 移动互联网走到今天,App寡头化的趋势已经越来越明显,同时用户的口味越来越高,这对移动App开发者提出了更高的要求.几年前可能你有一个创意,随便 ...

  7. COS访谈第十八期:陈天奇

    COS访谈第十八期:陈天奇 [COS编辑部按] 受访者:陈天奇      采访者:何通   编辑:王小宁 简介:陈天奇,华盛顿大学计算机系博士生,研究方向为大规模机器学习.他曾获得KDD CUP 20 ...

  8. 【用PS3手柄在安卓设备上玩游戏系列】谈安卓游戏对手柄的支持

    不同的游戏对于手柄的支持程度是不一样的,对应所需要进行的手柄设置也不尽相同.我没有这样的时间和精力,针对每一款游戏去写博客,但找出不同游戏中的共同点,针对同一类的游戏去写博客,应该是可行的.我把安卓上 ...

  9. C++11 可变参数模板

    在C++11之前, 有两个典型的受制于模板功能不强而导致代码重复难看的问题, 那就 function object 和 tuple. 拿 function objects 来说, 需要一个返回类型参数 ...

  10. iOS进阶读物

    不知不觉作为 iOS 开发也有两年多的时间了,记得当初看到 OC 的语法时,愣是被吓了回去,隔了好久才重新耐下心去啃一啃.啃了一阵,觉得大概有了点概念,看到 Cocoa 那么多的 Class,又懵了, ...