How do you limit a single process program run in a Windows environment to run only on a single CPU on a multi-core machine?

Is it the same between a windowed program and a command line program?

UPDATE:

  • Reason for doing this: benchmarking various programming languages aspects
  • I need something that would work from the very start of the process, therefore @akseli's answer, although great for other cases, doesn't solve my case
link|improve this question

49% accept rate
2  
Why do you want to do this? Letting the OS decide which CPU/core runs which process it going to be much more efficient. – ChrisF Jul 12 '11 at 13:24
1  
in order to benchmark various programming language aspects – Jonathan Jul 12 '11 at 14:00
1  
That's a good reason - you should have included that information in your question. It's important. – ChrisF Jul 12 '11 at 14:08
Motivation isn't essential, but I added it following your advice – Jonathan Jul 12 '11 at 14:38
However, it might be the information someone needs to be able to provide an answer. – ChrisF Jul 12 '11 at 14:39
feedback

3 Answers

If you're running Windows Vista/7 (possibly XP, but not sure) it's really rather simple.

Type in: Control+Shift+Esc to get your taskmanager up.

Click on the Processes tab

Find the process that needs its processor affinity changed

Right-click on the process

Click on "Set Affinity"

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

Good luck!

EDIT: You have to be administrator to get this to work.

link|improve this answer
Really nice, i didn't know that. Good answear +1 for me. – Diogo Jul 12 '11 at 13:26
10  
An affinity is not a guarantee. The processes will prefer this processor, but if it has several threads they won't all stay there. – Joel Coehoorn Jul 12 '11 at 13:40
It's not a guarantee but it's a beginning. You can also maximise your probabily of getting the processor you've set the afinity for if you set logical affinities for your most processor-hungry tasks. – akseli Jul 12 '11 at 13:43
This is good, but I need something that will be deterministic right from the start – Jonathan Jul 12 '11 at 14:01
feedback

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 
link|improve this answer
Revolter: Do you know if I can do the same with Services initialization? – Diogo Jul 13 '11 at 11:51
feedback

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.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.