{"id":581,"date":"2012-07-10T11:28:58","date_gmt":"2012-07-10T03:28:58","guid":{"rendered":"http:\/\/rmohan.com\/?p=581"},"modified":"2012-07-19T11:11:16","modified_gmt":"2012-07-19T03:11:16","slug":"process-priority-with-nice","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=581","title":{"rendered":"Process priority with nice"},"content":{"rendered":"<p><strong>Process priority with nice<\/strong><\/p>\n<p>Modern operating systems are multi-user and multitasking, which means that multiple users and multiple tasks can be using the computer at any given time. Typically you\u2019ll have one person using a desktop system running any number of applications or many users using many applications on a server.<\/p>\n<p>The amount of time devoted to tasks largely depends on how intensive the task is. Some tasks require higher priority than others; for instance, if you were compiling a large software package you didn\u2019t need immediately, that priority should probably be lower than your Web browser or e-mail client.<\/p>\n<p>Each process has a <em>niceness<\/em> value associated with it, which is what the kernel uses to determine which processes require more processor time than others. The higher the <a href=\"http:\/\/learnlinux.tsf.org.za\/courses\/build\/internals\/ch07s02.html\" target=\"_blank\">nice value<\/a>, the lower the priority of the process. In other words, the \u201cnicer\u201d the program, the less CPU it will try to take from other processes; programs that are less nice tend to demand more CPU time than other programs that are nicer.<\/p>\n<p>The priority is noted by a range of -20 (the highest) to 20 (the lowest). Using ps, you can see the current nice value of all programs:<\/p>\n<pre>$ ps axl<\/pre>\n<pre>F   UID   PID  PPID PRI  NI    VSZ   RSS WCHAN  STAT TTY        TIME COMMAND<\/pre>\n<pre>4     0     1     0  16   0   2648   112 -      S    ?          0:01 init [3]<\/pre>\n<pre>1     0     2     1  34  19      0     0 ksofti SN   ?          0:02 [ksoftirqd\/0]<\/pre>\n<pre>5     0     3     1  10  -5      0     0 worker S&lt;   ?          0:00 [events\/0]<\/pre>\n<pre>...<\/pre>\n<p>You can see that <em>init<\/em> has a nice value of 0, while other kernel tasks associated with PID 2 and 3 have a nice value of 19 and -5 respectively.<\/p>\n<p>Typically, a program inherits its nice value from its parent; this prevents low priority processes from spawning high priority children. Having said that, you can use the nice command (as root or via sudo) with the command you wish to execute in order to alter its nice value. Here is a short illustration:<\/p>\n<pre># ps axl | grep axl | grep -v grep<\/pre>\n<pre>4     0 30819 30623  15   0   4660   772 -      R+   pts\/0      0:00 ps axl<\/pre>\n<pre># nice -10 ps axl | grep axl | grep -v grep<\/pre>\n<pre>4     0 30822 30623  30  10   4660   772 -      RN+  pts\/0      0:00 ps axl<\/pre>\n<p>You can see there that the nice value, represented by column six, has been altered. You can also use the <em>renice<\/em> command to alter running processes. In the following example, vim was started to edit the file <em>foo<\/em> and began with a default nice value of 0. Using renice, we can change its priority:<\/p>\n<pre># ps axl | grep vim | grep -v grep<\/pre>\n<pre>0     0 30832 30623  16   0  15840  3140 -      S+   pts\/0      0:00 vim foo<\/pre>\n<pre># renice -5 30832<\/pre>\n<pre>30832: old priority 0, new priority -5<\/pre>\n<pre># ps axl | grep vim | grep -v grep<\/pre>\n<pre>0     0 30832 30623  15  -5  15840  3140 -      S&lt;+  pts\/0      0:00 vim foo<\/pre>\n<p>Here, we have adjusted the priority of vim, giving it a slightly higher priority. Renice operates on the process ID, so using grep, we determined that vim is process ID 30832 and saw that the nice value was 0. After executing renice, the nice value is now -5.<\/p>\n<p>Standard caveats apply: Only root can alter the nice priority of programs. So if you find that your compilation is taking too much CPU from other activities, consider renicing the parent process via root. Subsequent children should have a better nice value, or you can even start the compilation (or any other activity) with nice, specifying an appropriate nice value. You can also use renice to renice all programs belonging to a process group or user name\/ID.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>If you wish to run a command which typically uses a lot of CPU (for example, running tar on a large file), then you probably don\u2019t want to bog down your whole system with it. Linux systems provide the <em>nice<\/em> command to control your process priority at runtime, or <em>renice<\/em> to change the priority of an already running process. The full manpage has help, but the command if very easy to use:<\/p>\n<div>\n<table cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>1<\/div>\n<\/td>\n<td>\n<div>$ nice -n prioritylevel \/command\/to\/run<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>The priority level runs from -20 (top priority) to 19 (lowest). For example, to run tar and gzip at a the lowest priority level:<\/p>\n<div>\n<table cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>1<\/div>\n<\/td>\n<td>\n<div>$ nice -n 19 tar -czvf file.tar.gz bigfiletocompress<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>similarly, if you have a process running, use ps to find the process ID, and then use renice to change it\u2019s priority level:<\/p>\n<table cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>1<\/div>\n<\/td>\n<td>\n<div>$ renice -n 19 -p 987 32<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>Process priority with nice<\/p>\n<p>Modern operating systems are multi-user and multitasking, which means that multiple users and multiple tasks can be using the computer at any given time. Typically you\u2019ll have one person using a desktop system running any number of applications or many users using many applications on a server.<\/p>\n<p>The amount of time [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/581"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=581"}],"version-history":[{"count":3,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/581\/revisions"}],"predecessor-version":[{"id":733,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/581\/revisions\/733"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=581"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=581"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=581"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}