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

Ngix

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.

IIS

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 .

Programming

PHP

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_ZERO(&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

Linux

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.

task-manager-affinity

Then, click “Set affinity”.

processor-affinity

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

cd C:\Windows\system32\inetsrv\

Run

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.

Example:
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.

Example:
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

Subscribe Newsletter

Subscribe to our mailing list to get the monthly update.

Subscribename@email.com