<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="http://wiki.jackslab.org/skins/common/feed.css?303"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="zh-cn">
		<id>http://wiki.jackslab.org/index.php?action=history&amp;feed=atom&amp;title=Linux_%E4%B8%8B%E7%BB%91%E5%AE%9A%E8%BF%9B%E7%A8%8B%E5%88%B0_CPU</id>
		<title>Linux 下绑定进程到 CPU - 版本历史</title>
		<link rel="self" type="application/atom+xml" href="http://wiki.jackslab.org/index.php?action=history&amp;feed=atom&amp;title=Linux_%E4%B8%8B%E7%BB%91%E5%AE%9A%E8%BF%9B%E7%A8%8B%E5%88%B0_CPU"/>
		<link rel="alternate" type="text/html" href="http://wiki.jackslab.org/index.php?title=Linux_%E4%B8%8B%E7%BB%91%E5%AE%9A%E8%BF%9B%E7%A8%8B%E5%88%B0_CPU&amp;action=history"/>
		<updated>2026-09-11T06:16:42Z</updated>
		<subtitle>本wiki的该页面的版本历史</subtitle>
		<generator>MediaWiki 1.19.2</generator>

	<entry>
		<id>http://wiki.jackslab.org/index.php?title=Linux_%E4%B8%8B%E7%BB%91%E5%AE%9A%E8%BF%9B%E7%A8%8B%E5%88%B0_CPU&amp;diff=18597&amp;oldid=prev</id>
		<title>Comcat：/* Reference */</title>
		<link rel="alternate" type="text/html" href="http://wiki.jackslab.org/index.php?title=Linux_%E4%B8%8B%E7%BB%91%E5%AE%9A%E8%BF%9B%E7%A8%8B%E5%88%B0_CPU&amp;diff=18597&amp;oldid=prev"/>
				<updated>2025-12-04T06:37:23Z</updated>
		
		<summary type="html">&lt;p&gt;‎&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Reference&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
			&lt;tr valign='top'&gt;
			&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;←上一版本&lt;/td&gt;
			&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;2025年12月4日 (四) 06:37的版本&lt;/td&gt;
			&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;第114行：&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;第114行：&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;* http://www.mjmwired.net/kernel/Documentation/cpu-hotplug.txt&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;* http://www.mjmwired.net/kernel/Documentation/cpu-hotplug.txt&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&lt;del style=&quot;color: red; font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Comcat</name></author>	</entry>

	<entry>
		<id>http://wiki.jackslab.org/index.php?title=Linux_%E4%B8%8B%E7%BB%91%E5%AE%9A%E8%BF%9B%E7%A8%8B%E5%88%B0_CPU&amp;diff=9230&amp;oldid=prev</id>
		<title>Comcat：/* GLibc API */</title>
		<link rel="alternate" type="text/html" href="http://wiki.jackslab.org/index.php?title=Linux_%E4%B8%8B%E7%BB%91%E5%AE%9A%E8%BF%9B%E7%A8%8B%E5%88%B0_CPU&amp;diff=9230&amp;oldid=prev"/>
				<updated>2017-10-30T03:23:43Z</updated>
		
		<summary type="html">&lt;p&gt;‎&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;GLibc API&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
			&lt;tr valign='top'&gt;
			&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;←上一版本&lt;/td&gt;
			&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;2017年10月30日 (一) 03:23的版本&lt;/td&gt;
			&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;第37行：&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;第37行：&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Other macros to manipulate the bitset (set, reset bits):&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;Other macros to manipulate the bitset (set, reset bits):&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;lt;source lang=&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;bash&lt;/del&gt;&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;lt;source lang=&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;cpp&lt;/ins&gt;&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160; &amp;#160;  void CPU_CLR(int cpu, cpu_set_t *set);&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160; &amp;#160;  void CPU_CLR(int cpu, cpu_set_t *set);&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160; &amp;#160;  int CPU_ISSET(int cpu, cpu_set_t *set);&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160; &amp;#160;  int CPU_ISSET(int cpu, cpu_set_t *set);&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Comcat</name></author>	</entry>

	<entry>
		<id>http://wiki.jackslab.org/index.php?title=Linux_%E4%B8%8B%E7%BB%91%E5%AE%9A%E8%BF%9B%E7%A8%8B%E5%88%B0_CPU&amp;diff=9229&amp;oldid=prev</id>
		<title>Comcat：以“== GLibc API ==  &lt;source lang=cpp&gt;        #include &lt;sched.h&gt;         int sched_setaffinity(pid_t pid, unsigned int cpusetsize,                              cpu_set_t...”为内容创建页面</title>
		<link rel="alternate" type="text/html" href="http://wiki.jackslab.org/index.php?title=Linux_%E4%B8%8B%E7%BB%91%E5%AE%9A%E8%BF%9B%E7%A8%8B%E5%88%B0_CPU&amp;diff=9229&amp;oldid=prev"/>
				<updated>2017-10-30T03:23:26Z</updated>
		
		<summary type="html">&lt;p&gt;以“== GLibc API ==  &amp;lt;source lang=cpp&amp;gt;        #include &amp;lt;sched.h&amp;gt;         int sched_setaffinity(pid_t pid, unsigned int cpusetsize,                              cpu_set_t...”为内容创建页面&lt;/p&gt;
&lt;p&gt;&lt;b&gt;新页面&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== GLibc API ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=cpp&amp;gt;&lt;br /&gt;
       #include &amp;lt;sched.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
       int sched_setaffinity(pid_t pid, unsigned int cpusetsize,&lt;br /&gt;
                             cpu_set_t *mask);&lt;br /&gt;
&lt;br /&gt;
       int sched_getaffinity(pid_t pid, unsigned int cpusetsize,&lt;br /&gt;
                             cpu_set_t *mask);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=cpp&amp;gt;&lt;br /&gt;
    unsigned int cpu_mask;&lt;br /&gt;
    unsigned int pid = 1;&lt;br /&gt;
    unsigned int mask = 0x1;    // 绑定到 CPU0，0x2 则绑定到 CPU1&lt;br /&gt;
&lt;br /&gt;
    if (sched_getaffinity (pid, 4, &amp;amp;mask) &amp;lt; 0)&lt;br /&gt;
        fprintf (stderr, &amp;quot;failed to get cpu affinity: %s\n&amp;quot;, strerror (errno));&lt;br /&gt;
    else&lt;br /&gt;
    {&lt;br /&gt;
        printf (&amp;quot;setting cpu affinity of pid=%d to %x\n&amp;quot;, pid, cpu_mask);&lt;br /&gt;
        if (sched_setaffinity (pid, 4, &amp;amp;cpu_mask) &amp;lt; 0)&lt;br /&gt;
            fprintf (stderr, &amp;quot;failed to get cpu affinity: %s\n&amp;quot;, strerror (errno));&lt;br /&gt;
        else&lt;br /&gt;
            if (sched_getaffinity (pid, 4, &amp;amp;mask) &amp;lt; 0)&lt;br /&gt;
                fprintf (stderr, &amp;quot;failed to get cpu affinity: %s\n&amp;quot;, strerror (errno));&lt;br /&gt;
            else&lt;br /&gt;
                printf (&amp;quot;cpu affinity of pid=%d is %x\n&amp;quot;, pid, mask);&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Other macros to manipulate the bitset (set, reset bits):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=bash&amp;gt;&lt;br /&gt;
       void CPU_CLR(int cpu, cpu_set_t *set);&lt;br /&gt;
       int CPU_ISSET(int cpu, cpu_set_t *set);&lt;br /&gt;
       void CPU_SET(int cpu, cpu_set_t *set);&lt;br /&gt;
       void CPU_ZERO(cpu_set_t *set);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Reference:&lt;br /&gt;
&lt;br /&gt;
* http://www.gnu.org/software/libc/manual/html_node/CPU-Affinity.html&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Kernel interface (CPU Sets) ==&lt;br /&gt;
&lt;br /&gt;
运行一个无限循环程序 setaffinity_test (pid = 22580)，top 的输出为：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=bash&amp;gt;&lt;br /&gt;
......&lt;br /&gt;
Cpu0 : 24.1%us, 0.3%sy, 0.0%ni, 96.4%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st&lt;br /&gt;
Cpu1 : 77.8%us, 0.0%sy, 0.0%ni, 0.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st&lt;br /&gt;
Mem:   2033068k total, 1120456k used,   912612k free,    17348k buffers&lt;br /&gt;
Swap:        0k total,        0k used,        0k free,   125876k cached&lt;br /&gt;
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+COMMAND &lt;br /&gt;
22580 comcat    25   0 1496 312 252 R 100 0.0   2:54.74 setaffinity_test&lt;br /&gt;
......&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
其 status 为：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=bash&amp;gt;&lt;br /&gt;
$ cat /proc/22580/status | grep allow&lt;br /&gt;
Cpus_allowed:   03                 ----------&amp;gt; 可运行于 CPU0 和 CPU1&lt;br /&gt;
Mems_allowed:   1&lt;br /&gt;
&lt;br /&gt;
$ mkdir /dev/cpuset&lt;br /&gt;
$ mount -t cpuset x /dev/cpuset&lt;br /&gt;
$ cd /dev/cpuset&lt;br /&gt;
$ mkdir my_cpuset &amp;amp;&amp;amp; cd my_cpuset          # 创建自己的设置&lt;br /&gt;
$ echo 1 &amp;gt; cpus                                 # 绑定到 CPU1&lt;br /&gt;
$ echo 0 &amp;gt; mems&lt;br /&gt;
$ echo 22580 &amp;gt; tasks                        # 让 pid 为 22580 的进程绑定到 CPU1&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
cpuset 设置后，top 的输出为：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=bash&amp;gt;&lt;br /&gt;
......&lt;br /&gt;
Cpu0 : 3.3%us, 0.3%sy, 0.0%ni, 96.4%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st&lt;br /&gt;
Cpu1 : 100%us, 0.0%sy, 0.0%ni, 0.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st&lt;br /&gt;
Mem:   2033068k total, 1120456k used,   912612k free,    17348k buffers&lt;br /&gt;
Swap:        0k total,        0k used,        0k free,   125876k cached&lt;br /&gt;
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+COMMAND &lt;br /&gt;
22703 comcat    25   0 1496 312 252 R 100 0.0   2:54.74 setaffinity_test&lt;br /&gt;
......&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
此时其 status 为：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=bash&amp;gt;&lt;br /&gt;
$ cat /proc/22580/status | grep allow&lt;br /&gt;
Cpus_allowed:   02                 ----------&amp;gt; 只可运行于 CPU1&lt;br /&gt;
Mems_allowed:   1&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Reference ==&lt;br /&gt;
&lt;br /&gt;
* http://www.bullopensource.org/cpuset/&lt;br /&gt;
* http://www.mjmwired.net/kernel/Documentation/cpusets.txt&lt;br /&gt;
* http://www.mjmwired.net/kernel/Documentation/cpu-hotplug.txt&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Comcat</name></author>	</entry>

	</feed>