How to Run a Program on a Specific CPU Core (Set Affinity) in Batch Script
On a multi-core computer, the Windows scheduler does a good job of distributing tasks across all available CPU cores. However, for certain specialized tasks, you might want to force a program to run on only one, or a specific set of, CPU cores. This is called setting Processor Affinity. It's a powerful technique for performance tuning, especially for older, single-threaded applications or for isolating a resource-intensive process to prevent it from slowing down the entire system.
This guide will teach you how to use the built-in START command with its /AFFINITY switch to launch a program and lock it to specific CPU cores. You will learn the most critical part of this process: how to calculate the hexadecimal affinity mask.
What is Processor Affinity?
Processor affinity is a rule that binds a process to one or more specific CPU cores. Once set, the Windows scheduler will only run that process's threads on the cores you've designated. This can be useful to:
- Prevent a single-threaded legacy application from hopping between cores, which can sometimes improve performance.
- Isolate a heavy background task (like video encoding) to a specific set of cores, leaving the other cores free and responsive for your main work.
The Core Command: START /AFFINITY
The START command, used for launching new processes, has a dedicated switch for this task.
Syntax: START "" /AFFINITY <HexMask> "path\to\program.exe"
/AFFINITY: The switch to set the processor affinity.<HexMask>: A hexadecimal number that represents the set of cores you want to use. This is the most complex part.
CRITICAL: Understanding and Calculating the Affinity Mask
The affinity mask is a number where each bit corresponds to a CPU core, starting from the right (least significant bit).
- Core 0 is represented by the 1st bit.
- Core 1 is represented by the 2nd bit.
- Core 2 is represented by the 3rd bit, and so on.
To calculate the mask, you create a binary number where a 1 means "use this core" and a 0 means "don't use this core," and then you convert that binary number to hexadecimal.
Affinity Mask Table (for an 8-Core CPU)
| To Use This Core... | Binary Mask | Decimal Value | Hex Mask |
|---|---|---|---|
| Core 0 (CPU 1) | 00000001 | 1 | 1 |
| Core 1 (CPU 2) | 00000010 | 2 | 2 |
| Core 2 (CPU 3) | 00000100 | 4 | 4 |
| Core 3 (CPU 4) | 00001000 | 8 | 8 |
| Core 4 (CPU 5) | 00010000 | 16 | 10 |
| Core 5 (CPU 6) | 00100000 | 32 | 20 |
| Core 6 (CPU 7) | 01000000 | 64 | 40 |
| Core 7 (CPU 8) | 10000000 | 128 | 80 |
To use multiple cores, you add their hex values together.
- To use Core 0 and Core 2:
1+4=5. - To use Core 1 and Core 3:
2+8=A(in hex). - To use only the first four cores (0, 1, 2, 3):
1+2+4+8=F(in hex). - To use all eight cores:
1+2+4+8+10+20+40+80=FF(in hex).
Tip: The Windows Calculator in "Programmer" mode is a perfect tool for converting binary to hexadecimal.
Basic Example: Launching Notepad on a Single Core
This script will launch Notepad and restrict it to run only on Core 2 (the third CPU core).
@ECHO OFF
ECHO --- Launching Notepad on a Specific CPU Core ---
ECHO.
ECHO This will start Notepad locked to Core 2.
REM From our table, the hex mask for Core 2 is '4'.
START "Notepad on Core 2" /AFFINITY 4 notepad.exe
ECHO.
ECHO --- Command sent ---
You can verify this in the Task Manager by going to the "Details" tab, right-clicking notepad.exe, and selecting "Set affinity." You will see that only "CPU 2" is checked.
How the START /AFFINITY Command Works
The START /AFFINITY command is a shortcut for the SetProcessAffinityMask Windows API function. When you run the command, START first creates the new process in a suspended state, then calls this API function to set the affinity rule, and finally resumes the process. This ensures the rule is applied from the very beginning of the application's execution.
Common Pitfalls and How to Solve Them
The START Command's Quirky "Title" Parameter
This is a universal trap for the START command. If the first argument is in quotes, START treats it as a window title, not a command.
Solution: Always provide a dummy title (like "") as the first argument, especially if your program path is quoted.
REM This is the correct, robust syntax.
START "" /AFFINITY 1 "C:\Program Files\My App\app.exe"
Problem: Getting the Hex Mask Wrong
This is the most common error specific to /AFFINITY. Calculating the mask can be tricky.
Solution: Double-check your calculations. Use the table provided or the Windows Calculator in Programmer mode. Remember that the cores are zero-indexed (Core 0 is the first one).
Practical Example: Isolating a CPU-Intensive Task
This script launches a hypothetical, CPU-intensive video encoding job (encoder.exe). To keep the system responsive for the user, it restricts the encoder to run only on the last two cores of an 8-core system (Cores 6 and 7).
@ECHO OFF
SETLOCAL
TITLE CPU-Intensive Task Launcher
ECHO --- Launching Video Encoder with Restricted CPU Affinity ---
ECHO.
REM --- Calculate the affinity mask ---
REM For an 8-core CPU, we want Core 6 and Core 7.
REM From the table, the masks are 40 and 80.
REM Hex 40 + Hex 80 = Hex C0.
SET "AffinityMask=C0"
ECHO Restricting the process to CPU cores 6 and 7 (Mask: %AffinityMask%).
ECHO This will keep the rest of the system responsive.
ECHO.
SET "EncoderCmd=encoder.exe -i "input.mp4" -o "output.mp4" --preset=slow"
START "Video Encoder" /AFFINITY %AffinityMask% %EncoderCmd%
ECHO --- Encoder has been launched in the background ---
ENDLOCAL
Conclusion
The START /AFFINITY command is a powerful tool for performance tuning and resource management in a batch script.
Key takeaways:
- It allows you to lock a process to one or more specific CPU cores.
- The core of the command is the hexadecimal affinity mask, which you must calculate based on the binary representation of the cores you want to use.
- Always be mindful of the
START "" "..."title syntax to avoid errors with quoted paths. - This is an advanced technique, best used for specific scenarios like isolating CPU-intensive tasks or stabilizing legacy applications.