{"id":7015,"date":"2018-10-12T14:19:48","date_gmt":"2018-10-12T11:19:48","guid":{"rendered":"https:\/\/www.howtoforge.com\/linux-taskset-command\/"},"modified":"2018-10-12T14:19:48","modified_gmt":"2018-10-12T11:19:48","slug":"linux-taskset-command-tutorial-for-beginners-with-examples","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/linux-taskset-command-tutorial-for-beginners-with-examples\/","title":{"rendered":"Linux taskset Command Tutorial for Beginners (with Examples)"},"content":{"rendered":"<p>Ever heard of the term processor affinity? It&#8217;s a feature that allows you to bind or unbind processes to a particular central processing unit, or a range of CPUs. Yes, you can tell the system which CPU core(s) should be used to a run a particular process. For theoretical details on why processor affinity exists, head <a href=\"https:\/\/en.wikipedia.org\/wiki\/Processor_affinity\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a>.<\/p>\n<p>Here, in this tutorial, we will discuss a utility &#8211; dubbed <strong>taskset<\/strong> &#8211; that lets you achieve processor affinity. But before we do that, it&#8217;s worth mentioning that all examples in this tutorial have been tested on an Ubuntu 18.04 LTS machine.<\/p>\n<h2 id=\"linux-taskset-command\">Linux taskset command<\/h2>\n<p>The taskset command allows you to set or retrieve a process&#8217;s CPU affinity. Following is its syntax:<\/p>\n<p class=\"command\">taskset [options] mask command [argument&#8230;]<br \/>taskset [options] -p [mask] pid<\/p>\n<p>Here&#8217;s how the tool&#8217;s man page explains it:<\/p>\n<pre readability=\"37\"> taskset\u00a0 is\u00a0 used\u00a0 to\u00a0 set\u00a0 or\u00a0 retrieve\u00a0 the CPU affinity of a running<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 process given its pid, or to launch a new\u00a0 command\u00a0 with\u00a0 a\u00a0 given\u00a0 CPU<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 affinity.\u00a0\u00a0 CPU affinity is a scheduler property that \"bonds\" a process<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 to a given set of CPUs on the system.\u00a0 The Linux scheduler\u00a0 will\u00a0 honor<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 the\u00a0 given CPU affinity and the process will not run on any other CPUs.<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Note that the Linux scheduler also supports natural CPU\u00a0 affinity:\u00a0 the<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 scheduler attempts to keep processes on the same CPU as long as practi?<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 cal for performance reasons.\u00a0 Therefore, forcing a specific CPU\u00a0 affin?<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ity is useful only in certain applications.<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 The CPU affinity is represented as a bitmask, with the lowest order bit<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 corresponding to the first logical CPU and the highest order bit corre?<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 sponding\u00a0 to\u00a0 the\u00a0 last logical CPU.\u00a0 Not all CPUs may exist on a given<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 system but a mask may specify more CPUs than are present.\u00a0 A\u00a0 retrieved<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 mask\u00a0 will\u00a0 reflect only the bits that correspond to CPUs physically on<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 the system.\u00a0 If an invalid mask is given (i.e., one that corresponds to<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 no\u00a0 valid\u00a0 CPUs on the current system) an error is returned.\u00a0 The masks<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 may be specified in hexadecimal (with or without a leading \"0x\"), or as<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 a CPU list with the --cpu-list option.\u00a0 For example,<\/p><p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0x00000001\u00a0 is processor #0,<\/p><p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0x00000003\u00a0 is processors #0 and #1,<\/p><p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0xFFFFFFFF\u00a0 is processors #0 through #31,<\/p><p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 32\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 is processors #1, #4, and #5,<\/p><p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 --cpu-list 0-2,6<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 is processors #0, #1, #2, and #6.<\/p><p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 When\u00a0 taskset returns, it is guaranteed that the given program has been<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 scheduled to a legal CPU.<\/p><\/pre>\n<p>Following are some Q&amp;A-styled examples that should give you a better idea on how the taskset command works.<\/p>\n<h2 id=\"q-how-to-use-taskset-tonbspretrieve-cpu-affinity-of-a-process\">Q1. How to use taskset to\u00a0retrieve CPU affinity of a process?<\/h2>\n<p>If you want taskset to display CPU affinity of an already running process, use the command in the following way:<\/p>\n<p class=\"command\">taskset -p [PID]<\/p>\n<p>Just replace PID with the ID of the process whose CPU affinity you want to fetch. For example:<\/p>\n<p class=\"command\">taskset -p 9726<\/p>\n<p>The above command returned the following output:<\/p>\n<pre>pid 9726's current affinity mask: f<\/pre>\n<p>So hexadecimal value &#8216;f&#8217; here means the process can run on any of the 4 processor cores: 0,1,2,3.<\/p>\n<p>If you want the output to be in terms of CPU range, you can add the -c command line option.<\/p>\n<p class=\"command\">taskset -cp 9726<\/p>\n<p>Following is the output in this case:<\/p>\n<pre>pid 9726's current affinity list: 0-3<\/pre>\n<h2 id=\"q-how-to-change-cpu-affinity-using-taskset\">Q2. How to change CPU affinity using taskset?<\/h2>\n<p>To tweak the CPU affinity of an existing process, you need to specify the process ID (like we did in the previous section) along with a hexadecimal mask that defines the new affinity.<\/p>\n<p>For example, the current CPU affinity of the Gedit process (PID: 9726) is &#8216;f&#8217;.<\/p>\n<p><a class=\"fancybox\" id=\"img-taskset-set-affinity\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/taskset-set-affinity.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/10\/linux-taskset-command-tutorial-for-beginners-with-examples.png\" alt=\"How to change CPU affinity using taskset\" width=\"330\" height=\"38\" title=\"\"><\/a><\/p>\n<p>To change the affinity to 0x11, use the following command:<\/p>\n<p class=\"command\">taskset -p 0x11 9726<\/p>\n<p>And then you can again check the new affinity using the following command:<\/p>\n<p class=\"command\">taskset -p 9726<\/p>\n<p>The following screenshots show the outputs for these commands in my case:<\/p>\n<p><a class=\"fancybox\" id=\"img-taskset-set-affinity-new1\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/taskset-set-affinity-new1.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/10\/linux-taskset-command-tutorial-for-beginners-with-examples-1.png\" alt=\"Using Taskset\" width=\"500\" height=\"87\" title=\"\"><\/a><\/p>\n<p>So you can see that the affinity got changed.<\/p>\n<h2 id=\"q-how-to-assign-a-range-of-cpus-while-changing-affinity\">Q3. How to assign a range of CPUs while changing affinity?<\/h2>\n<p>This is no big deal. All you have to do is to add the -c command line option to the command we&#8217;ve used in the previous section along with the CPU core range as input.<\/p>\n<p>Here is an example:<\/p>\n<p class=\"command\">taskset -cp 0,3 9726<\/p>\n<p>Following is the output produced in this case:<\/p>\n<pre>pid 9726's current affinity list: 0<br\/>pid 9726's new affinity list: 0,3<\/pre>\n<h2 id=\"q-how-to-launch-a-process-with-predefined-cpu-affinity\">Q4. How to launch a process with predefined CPU affinity?<\/h2>\n<p>Yes, you can also launch a process with a set CPU affinity.<\/p>\n<p>For example, I launched the text gedit editor with CPU affinity 0xa.<\/p>\n<p class=\"command\">taskset 0xa gedit<\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>Agreed, the taskset command isn&#8217;t for an average command line user. It&#8217;s mostly used by server side experts for process optimization in a multi core environment. We&#8217;ve discussed the tool&#8217;s bascs here. For more info, head to its <a href=\"https:\/\/linux.die.net\/man\/1\/taskset\" target=\"_blank\" rel=\"noopener noreferrer\">man page<\/a>.<\/p>\n<div class=\"authorbox\" readability=\"15\">\n<img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/10\/linux-taskset-command-tutorial-for-beginners-with-examples.jpg\" alt=\"Himanshu Arora\" title=\"\"> <\/p>\n<p><strong>About Himanshu Arora<\/strong><\/p>\n<p>Himanshu Arora has been working on Linux since 2007. He carries professional experience in system level programming, networking protocols, and command line. In addition to HowtoForge, Himanshu&#8217;s work has also been featured in some of world&#8217;s other leading publications including Computerworld, IBM DeveloperWorks, and Linux Journal.<\/p>\n<\/div>\n<div>\n<p><b>Share this page:<\/b><\/p>\n<p>\n<a href=\"https:\/\/www.facebook.com\/sharer.php?u=https%3A%2F%2Fwww.howtoforge.com%2Flinux-taskset-command%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/10\/linux-taskset-command-tutorial-for-beginners-with-examples-2.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fwww.howtoforge.com%2Flinux-taskset-command%2F&amp;text=Linux+taskset+Command+Tutorial+for+Beginners+%28with+Examples%29&amp;via=howtoforgecom&amp;related=howtoforgecom\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/10\/linux-taskset-command-tutorial-for-beginners-with-examples-3.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/howtoforgecom\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/10\/linux-taskset-command-tutorial-for-beginners-with-examples-4.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/plus.google.com\/share?url=https%3A%2F%2Fwww.howtoforge.com%2Flinux-taskset-command%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/10\/linux-taskset-command-tutorial-for-beginners-with-examples-5.png\" height=\"20\" alt=\"\" title=\"\"><\/a>\n<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Ever heard of the term processor affinity? It&#8217;s a feature that allows you to bind or unbind processes to a particular central processing unit, or a range of CPUs. Yes, you can tell the system which CPU core(s) should be used to a run a particular process. For theoretical details on why processor affinity exists, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-7015","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/7015","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/comments?post=7015"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/7015\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=7015"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=7015"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=7015"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}