[英]How to use Java Native Access to set process affinity for processes besides Java.exe?
Although it is fairly easy to do manually, I am attempting to automate the setting of cpu affinity in Windows 7 for various VMs after their initial creation time. 尽管手动操作相当容易,但我尝试在初始创建时间后为各种VM自动设置Windows 7中的cpu亲和性。 The project is in Java and I am trying to avoid directly including C code, so I have been using Java Native Access, which masks things like winapi.
该项目是Java,我试图避免直接包含C代码,所以我一直在使用Java Native Access,它掩盖了像winapi这样的东西。 I'm new to the library, and it's a bit lacking in tutorials or examples, although there are some basic ones findable through quick Google searches.
我是图书馆的新手,虽然通过快速谷歌搜索可以找到一些基本版本,但它在教程或示例中有点缺乏。
Using the following code, I can set the affinity of the main Java process (ffffffffffffffff locally), but other processes remain completely unaffected, even when I have the privileges to manually set affinity using Task Manager. 使用以下代码,我可以设置主Java进程的亲缘关系(本地ffffffffffffffff),但其他进程仍然完全不受影响,即使我有权使用任务管理器手动设置关联。 I have also iterated over all integers from 0 to 10000, rather than just entering ids I know to be valid.
我还迭代了从0到10000的所有整数,而不是只输入我知道有效的id。
Main class: 主要课程:
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.WinNT.HANDLE;
public class SetAffinity {
public static void main(String[] args){
int pid = -1;
AffinityKernel instance = (AffinityKernel)Native.loadLibrary("Kernel32",AffinityKernel.class));
System.out.println(instance.SetProcessAffinityMask(new HANDLE(new Pointer(pid)), 1));
}
}
Utility interface: 实用界面:
import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.platform.win32.WinNT.HANDLE;
public interface AffinityKernel extends Kernel32{
public boolean SetProcessAffinityMask(HANDLE hProcess, int dwProcessAffinityMask);
}
Since I can check that it correctly sets the cpu affinity of the process it's running in, I know the syntax is correct. 因为我可以检查它是否正确设置了它正在运行的进程的cpu亲和性,我知道语法是正确的。
The question is: 问题是:
How do I access/reference processes besides the current process? 除当前流程外,如何访问/引用流程?
The other routine you're looking for is OpenProcess
, which allows you to get a HANDLE to another process, which you can then use in the SetProcessAffinityMask
. 您正在寻找的另一个例程是
OpenProcess
,它允许您获取另一个进程的HANDLE,然后您可以在SetProcessAffinityMask
使用该进程。 The calling signature: 主叫签名:
HANDLE OpenProcess(DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwProcessId);
It's exposed in the Kernel32 interface already. 它已经在Kernel32接口中公开了。
Determining the process id of the other java processes using JNA has been asked already , and should be understandable. 已经提出使用JNA确定其他java进程的进程ID,这应该是可以理解的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.