How to set processor affinity on an executable in Windows XP?

空扰寡人 提交于 2019-11-30 13:09:04

One feature of Process Lasso is to set the affinity of a process whenever that process is launched.

http://waynes-world-it.blogspot.com/2009/06/processor-affinity-on-windows-server.html

PowerShell

Use PowerShell to set the processor affinity for one or more running processes. There’s an example script below, setting the processor mask of calc.exe to the first 4 processors. I like this method because the script is simple, it would be easy to schedule, works on x86 and x64, supports multiple processes of the same name and at least partly because it highlights just how easy administration with PowerShell is.

Note that if you use factorial of a large number with calc.exe (n!) you’ll generate100% CPU which can be useful for testing. The mask below is 0xf = 1111 – a mask allowing use of only the first four processors:

$calcSet = Get-Process -ProcessName "calc"
foreach ($calc in $calcSet) {$calc.ProcessorAffinity=0xF}

You may want to look at the /AFFINITY parameter to start.

From the help:

AFFINITY    The new application will have the specified processor
            affinity mask, expressed as a hexadecimal number.

As processor affinity on Windows is a bitmask you may need some experimentation but I'd assume 1 being the first core, therefore 7 being the first three cores and F being all four. Or 8 for only the fourth.

You can then replace scheduled tasks or shortcuts with a call to start with the appropriate parameters.

You can use the single-proc affinity application shim to force one processor on the executable level, which will force the process onto one core.

This article, http://msdn.microsoft.com/en-us/library/bb173458.aspx, has a paragraph on enabling the shim towards the bottom.

Use SetProcessAffinityMask(). And beware, Processor Affinity is inherited!

You'll need to use ImageFileExecutionOptions, specifically the "Debugger" option, and write your own small executable that calls SetProcessAffinityMask() on itself, and then spawns a new process, which is the one you want to set affinity for. Set that as the debugger, and you're done.

The ImageCfg.exe utility does work. I just used it to solve a company issue today. It is available from http://www.robpol86.com/pages/imagecfg.php

Imagecfg -a 0x3 xxx.exe

limits the .exe to CPU0 and CPU1, for example.

patrik

Clearly this thread is outdated but im adding a comment anyway just in case anyone googles on this subject (as I did)

You could try setting the priority of the process so that even if it decides to use 100% of the CPU, something that is higher priority can take over when it needs to do so.

Doing this automatically (rather than having to play in the task manager) is something I asked about a while ago.

The start command can be used to set startup priority of a process.

Eg. start "my path\my process" /LOW for low priority.

Allowed priority switches LOW, NORMAL, HIGH, REALTIME, ABOVENORMAL, BELOWNORMAL

Could be called from a batchfile for example.

You could try setting the priority of the process so that even if it decides to use 100% of the CPU, something that is higher priority can take over when it needs to do so.

Doing this automatically (rather than having to play in the task manager) is something I asked about a while ago.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!