https://www.nixtutor.com/linux/changing-priority-on-linux-processes/

Changing Priority on Linux Processes

Wed, Apr 22, 2009

What is Priority and Why Should I Care?

When talking about processes priority is all about managing processor time. The Processor or CPU is like a human juggling multiple tasks at the same time. Sometimes we can have enough room to take on multiple projects. Sometimes we can only focus on one thing at a time. Other times something important pops up and we want to devote all of our energy into solving that problem while putting less important tasks on the back burner.

In Linux we can set guidelines for the CPU to follow when it is looking at all the tasks it has to do. These guidelines are called niceness or nice value. The Linux niceness scale goes from -20 to 19. The lower the number the more priority that task gets. If the niceness value is high number like 19 the task will be set to the lowest priority and the CPU will process it whenever it gets a chance. The default nice value is zero.

By using this scale we can allocate our CPU resources more appropriately. Lower priority programs that are not important can be set to a higher nice value, while high priority programs like daemons and services can be set to receive more of the CPU’s focus. You can even give a specific user a lower nice value for all of his/her processes so you can limit their ability to slow down the computer’s core services.

Checking the Priority of Running Processes

The easiest way to get a quick picture of what the current niceness priority on a process is to open up the top processes with:

top

  1. top
  1. q
  1. h
  1. ps -o pid,comm,nice -p

Setting priority on new processes

At this point you are probably wondering how you can set your own priority levels on processes. To change the priority when issuing a new command you do nice -n [nice value] [command]:

  1. nice -n 10 apt-get upgrade

This will increment the default nice value by a positive 10 for the command, ‘apt-get upgrade’ This is often useful for times when you want to upgrade apps but don’t want the extra process burden at the given time. Remember a positive number is gives less priority for a process.

  1. renice 10 -p 21827

This will increment the priority of the process with an id of 21827 to 10.

Note: Only root can apply negative nice values.

Setting Priority on Existing Processes

Obviously at some point you are going to want to alter the nice value of something that is already running. Just the other day I was doing an upgrade to Ubuntu Jaunty and Firefox started to become unusably slow. A quick renice command rescheduled the upgrade to a lower priority and I was back to surfing in no time.

To change the priority of an existing process just do renice [nice value] -p [process id]:

  1. renice 10 -p 21827

This will increment the priority of the process with an id of 21827 to 10.

Note: Only root can apply negative nice values.

Setting Permanent Priority on all Processes for a Specific User

Sometimes it is helpful to give specific users lower priority than others to keep system resources allocated in the proper places like core services and other programs.

You can set the default nice value of a particular user or group in the /etc/security/limits.conf file.

  1. /etc/security/limits.conf

It uses this syntax: [username] [hard|soft] priority [nice value]

  1. backupuser hard priority 1
  1. Fields Management for window :Def, whose current sort field is %CPU
  2. Navigate with Up/Dn, Right selects for move then <Enter> or Left commits,
  3. 'd' or <Space> toggles display, 's' sets sort. Use 'q' or <Esc> to end!
  4.  
  5. * PID = Process Id
  6. * USER = Effective User Name
  7. * PR = Priority
  8. * NI = Nice Value
  9. * VIRT = Virtual Image (KiB)
  10. * RES = Resident Size (KiB)
  11. * SHR = Shared Memory (KiB)
  12. * S = Process Status
  13. * %CPU = CPU Usage
  14. * %MEM = Memory Usage (RES)
  15. * TIME+ = CPU Time, hundredths
  16. * COMMAND = Command Name/Line
  17. PPID = Parent Process pid
  18. UID = Effective User Id
  19. RUID = Real User Id
  20. RUSER = Real User Name
  21. SUID = Saved User Id
  22. SUSER = Saved User Name
  23. GID = Group Id
  24. GROUP = Group Name
  25. PGRP = Process Group Id
  26. TTY = Controlling Tty
  27. TPGID = Tty Process Grp Id
  28. SID = Session Id
  29. nTH = Number of Threads
  30. P = Last Used Cpu (SMP)
  31. TIME = CPU Time
  32. SWAP = Swapped Size (KiB)
  33. CODE = Code Size (KiB)
  34. DATA = Data+Stack (KiB)
  35. nMaj = Major Page Faults
  36. nMin = Minor Page Faults
  37. nDRT = Dirty Pages Count
  38. WCHAN = Sleeping in Function
  39. Flags = Task Flags <sched.h>
  40. CGROUPS = Control Groups
  41. SUPGIDS = Supp Groups IDs
  42. SUPGRPS = Supp Groups Names
  43. TGID = Thread Group Id
  44. ENVIRON = Environment vars
  45. vMj = Major Faults delta
  46. vMn = Minor Faults delta
  47. USED = Res+Swap Size (KiB)
  48. nsIPC = IPC namespace Inode
  49. nsMNT = MNT namespace Inode
  50. nsNET = NET namespace Inode
  51. nsPID = PID namespace Inode
  52. nsUSER = USER namespace Inode
  53. nsUTS = UTS namespace Inode

A quick renice command rescheduled the upgrade to a lower priority and I was back to surfing in no time.的更多相关文章

  1. Visual Studio 调试技巧[Command Window & Immediate Window ](Tips)

    Visual Studio 调试技巧[Command Window & Immediate Window ](Tips) 1. immediate window 定义的一些 alias (// ...

  2. Rocket - debug - Example: Quick Access

    https://mp.weixin.qq.com/s/SxmX-CY2tqvEqZuAg-EXiQ 介绍riscv-debug的使用实例:配置Quick Access功能. 1. Quick Acce ...

  3. linux 操作

    正在运行的内核和系统信息 # uname -a # 获取内核版本(和BSD版本) # lsb_release -a # 显示任何 LSB 发行版版本信息 # cat /etc/SuSE-release ...

  4. linux分析、诊断及调优的必备“杀器”之一

    下面分别列出linux分析.诊断及调优时用到的工具,并分别进行说明,以方便自己和其他同学参考学习,禁止转载. 1.top top - 02:06:59 up 4 days, 17:14, 2 user ...

  5. Spring Boot Reference Guide

    Spring Boot Reference Guide Authors Phillip Webb, Dave Syer, Josh Long, Stéphane Nicoll, Rob Winch,  ...

  6. yum 工作原理

    MySQL :: A Quick Guide to Using the MySQL Yum Repository https://dev.mysql.com/doc/mysql-yum-repo-qu ...

  7. Performance Tuning Using Linux Process Management Commands

    [root@bigdata-server-02 /]# ps --help all Usage: ps [options] Basic options: -A, -e all processes -a ...

  8. linux应用之mysql数据库指定版本的yum安装(centos)

    A Quick Guide to Using the MySQL Yum Repository Abstract The MySQL Yum repository provides RPM packa ...

  9. MySQL/MariaDB数据库的服务器配置

    MySQL/MariaDB数据库的服务器配置 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.MySQL中的系统数据库 1>.mysql数据库 是mysql的核心数据库,类 ...

随机推荐

  1. 矩阵快速幂 UVA 10870 Recurrences

    题目传送门 题意:f(n) = a1f(n − 1) + a2f(n − 2) + a3f(n − 3) + . . . + adf(n − d), for n > d,求f (n) % m.训 ...

  2. iOS学习17之OC内存管理

    1.内存管理的方式 1> iOS应用程序出现Crash(闪退),90%的原因是因为内存问题. 2> 内存问题 野指针异常:访问没有所有权的内存,如果想要安全的访问,必须确保空间还在 内存泄 ...

  3. 栈式自动编码器(Stacked AutoEncoder)

    起源:自动编码器 单自动编码器,充其量也就是个强化补丁版PCA,只用一次好不过瘾. 于是Bengio等人在2007年的  Greedy Layer-Wise Training of Deep Netw ...

  4. 转载:CSS计数器的趣味时光之css计算数据

    CSS计数器是“啊太好了,竟不知道CSS可以做这啊”这类非常有趣的众多特性之一.简言之,用CSS使你持续某增加某个量,而无需JavaScript. 简单计数器 我们从这个简单的分页示例开始: 你见到的 ...

  5. web安全测试

  6. 移动端开发——javascript

    javascript(简称js)语言在移动前端应用很广.可以说必不可少,许多效果都是和js相关的.包括现在移动端的一些框架.jqmobi.jqtouch.sencha touch.jquerymobi ...

  7. 【COGS】894. 追查坏牛奶

    http://cojs.tk/cogs/problem/problem.php?pid=894 题意:n个点m条边的加权网络,求最少边数的按编号字典序最小的最小割.(n<=32, m<=1 ...

  8. 【POJ】2420 A Star not a Tree?

    http://poj.org/problem?id=2420 题意:给n个点,求一个点使得到这个n个点的距离和最短,输出这个最短距离(n<=100) #include <cstdio> ...

  9. [BZOJ2792][Poi2012]Well

    2792: [Poi2012]Well Time Limit: 40 Sec  Memory Limit: 64 MBSubmit: 137  Solved: 61[Submit][Status][D ...

  10. BZOJ3293: [Cqoi2011]分金币

    Description 圆桌上坐着n个人,每人有一定数量的金币,金币总数能被n整除.每个人可以给他左右相邻的人一些金币,最终使得每个人的金币数目相等.你的任务是求出被转手的金币数量的最小值. Inpu ...