How to limit a process to a single CPU core?

If you're running Windows Vista/7 (possibly XP, but not sure) it's really rather simple. You have to be an administrator to get this to work.

  1. Press Ctrl + Shift + Esc to get open Task Manager.
  2. Click on the Processes tab.
  3. Find the process that needs its processor affinity changed.
  4. Right-click on the process.
  5. Click on "Set Affinity".

Here you can select which processor(s) your process will use.


From the command line, use:

start /affinity 1 program.exe 

This will run program.exe on the first CPU as "1" is the hex value of the affinity mask.

CPU3 CPU2 CPU1 CPU0  Bin  Hex
---- ---- ---- ----  ---  ---
OFF  OFF  OFF  ON  = 0001 = 1
OFF  OFF  ON   OFF = 0010 = 2
OFF  OFF  ON   ON  = 0011 = 3
OFF  ON   OFF  OFF = 0100 = 4
OFF  ON   OFF  ON  = 0101 = 5 
OFF  ON   ON   OFF = 0110 = 6
OFF  ON   ON   ON  = 0111 = 7
ON   OFF  OFF  OFF = 1000 = 8
ON   OFF  OFF  ON  = 1001 = 9
ON   OFF  ON   OFF = 1010 = A 
ON   OFF  ON   ON  = 1011 = B
ON   ON   OFF  OFF = 1100 = C
ON   ON   OFF  ON  = 1101 = D
ON   ON   ON   OFF = 1110 = E 
ON   ON   ON   ON  = 1111 = F 

Depends on what you are willing to do:

Method 1: On demand

Use ImageCFG. This utility will let you setup an executable to run on any number of cores. Make sure you backup your target executable before making the changes and restore it when you are done playing with it.

Method 2: Force an entire Windows Session (Vista/7)

  1. Type bcdedit /set onecpu on on a command prompt
  2. Reboot the system.
  3. When you are done playing, type 2 - Type: bcdedit /set onecpu off and reboot again.

Method 2: Force an entire Windows Session (XP)

  1. Open your boot.ini file (Right-click My Computer -> Properties -> Advanced Tab -> Settings button under 'Startup and Recovery' -> Edit button in 'System Startup').
  2. You'll find the following (or similar) section in the file:

    [operating systems]

    multi(0)disk(0)rdisk(0)partition(2)\WINDOWS="Microsoft Windows XP Professional" /fastdetect

  3. Change it by adding the /onecpu flag:

    [operating systems]

    multi(0)disk(0)rdisk(0)partition(2)\WINDOWS="Microsoft Windows XP Professional" /fastdetect /onecpu

  4. Reboot. Once you are done playing remove the flag and reboot again.

Method 0: Not a good method (Processor Affinity)

Anything that otherwise involves Processor Affinity isn't a good option, I'm afraid. Processor affinity is a clue to the processor. The processor is not obliged to respect it, and often will not.


I put the following function in my program in python 2.6 and it calls windows functions. My machine has only two cores so you may need to change that part. The comments tell you how to see what the current affinity is. It works "as is" for a single core machine if you set or default to a mask of 1.

def setaffinitymask(pid = None, mask = 1):
    """ Set The Affinity Mask of a Windows Process.  Affinity Mask is a value between 1-3 where
        1 is for core 0, 2 is for core 1, 3 is for both cores (it is a bitmap)
        Default sets the Affinity Mask to core 0 only
        python process can take any valid process ID. """

    import win32api, win32process, win32con

    if pid  == None:
        pid  = win32api.GetCurrentProcessId()
    if mask == None:
        mask = 1
    if mask  < 1 or mask > 3:
        mask = 1
        print 'process affinity mask out of range [1..3] setting it to 1'
    handle = win32api.OpenProcess(win32con.PROCESS_ALL_ACCESS, True, pid)
    # see what mask is currently set to
    # processAffinityMask, systemAffinityMask = win32process.GetProcessAffinityMask(handle)
    # print 'process affinity mask = ', processAffinityMask, ', system affinity mask = ', systemAffinityMask
    # set affinity for process to mask value
    win32process.SetProcessAffinityMask(handle, mask) # 1=core 0, 2=core 1, 3=both
    processAffinityMask, systemAffinityMask = win32process.GetProcessAffinityMask(handle)
    #print 'process affinity mask = ', processAffinityMask, ', system affinity mask = ', systemAffinityMask