How to Set CPU Affinity of a Process

There are a few ways to set a process’s CPU affinity. In this article, we will discuss a few options.

Web Server


With Ngix on FreeBSD and Linux, you can bind worker processes to a set of CPUs using the worker_cpu_affinity directive. Please find the details here: Worker_cpu_affinity.


About how to configure CPU usage parameters and CPU actions that will be used in application pools, please refer to CPU Settings for an Application Pool .



With PHP, you can set affinity by running taskset program through system() call. For example, below binds the PHP process to cores 0 and 1:

system('taskset -cp 0,1 '.getmypid());

C++ code

On Linux, please refer to the following code snippet:

#define _GNU_SOURCE
#include <sched.h>
#include <stdio.h>
#include <sys/sysinfo.h>

int main()
    cpu_set_t  mask;
    CPU_SET(0, &mask);
    CPU_SET(1, &mask);
    CPU_SET(2, &mask);
    CPU_SET(3, &mask);
    sched_setaffinity(0, sizeof(mask), &mask);

On Windows, please refer to the following code snippet:

#include < Windows.h>

int main()
           ::SetProcessAffinityMask(GetCurrentProcess(), 0xf/*first 4 cpus*/);

Operating System


We can run the taskset program to set affinity.

Get the CPU affinity of a process

$ taskset –p 22445
pid 22445’s current affinity list: 0-5

Set a process’s CPU affinity to 0-2 CPUs

$ taskset –pc  0-2 22445
pid 22445’s current affinity list: 0-5
pid 22445’s new affinity list: 0-2

Start a process with specified CPU affinity

$ taskset –c 0-3 ./ReadBarcode

Reference: taskset

Windows OS

There are a few different methods to achieve this.

Method 1: System-wide

System-wide we can set the boot.ini setting to use /numproc=4 so that the machine only uses 4 cores irrespective of the number of cores available (provided that there are 4 or more cores available)

Method 2: Task Manager

First, select a process from Task Manager.


Then, click “Set affinity”.


Method 3: Start /affinity
Launch a command window and run the highlighted commands as follows

cd C:\Windows\system32\inetsrv\


start /affinity 1F InetMgr.exe

Now you can check the affinity from the Task Manager or Process Explorer.

If you are using start /affinity, you will need to specify a hexadecimal value for affinity. There is a simple way to calculate affinity value.
Here is how the processors will be numbered.

CPU ID Associated value (n) Formula (2n-1) Affinity in Hex (h)
CPU0 1 1 1
CPU1 2 3 3
CPU2 4 7 7
CPU3 8 15 F
CPU4 16 31 1F
CPU5 32 63 3F
CPU6 64 127 7F
CPU7 128 255 FF

Based on the formula above , you can run the following command. Replace h with the value in the Affinity column. This will result in using all the CPUs listed above the specified value including the current.

If you specify a value of 1F for affinity, it will use CPU4, CPU3, CPU2, CPU1, and CPU0.

Start /affinity h BarcodeReaderDemo.exe

If you want to use specific CPUs, you will need to SUM the associated values and use the corresponding HEX value.

If you want to run a process on CPU0 & CPU4, you can sum the values which is 0x11.

Start /affinity 11 BarcodeReaderDemo.exe

Reference: how-to-launch-a-process-with-cpu-affinity-set

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe Newsletter

Subscribe to our mailing list to get the monthly update.