如何使用Java Native Access为Java.exe以外的进程设置进程亲和力? [英] How to use Java Native Access to set process affinity for processes besides Java.exe?

查看:98
本文介绍了如何使用Java Native Access为Java.exe以外的进程设置进程亲和力?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管手动操作相当容易,但我尝试在Windows 7中自动为各种VM在初始创建时间后自动设置cpu亲和力。该项目使用Java,并且我试图避免直接包含C代码,因此我一直在使用Java Native Access,它掩盖了诸如winapi之类的东西。我是图书馆的新手,虽然有些基本的内容可以通过Google的快速搜索找到,但它在教程或示例中还是有点缺乏。

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. 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. 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.

使用以下代码,我可以设置主要Java进程(在本地为ffffffffffffffffff)的关联性,但是即使我具有使用任务管理器手动设置关联性的特权,其他进程也不会完全受影响。我还遍历了从0到10000的所有整数,而不仅仅是输入我知道是有效的id。

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. I have also iterated over all integers from 0 to 10000, rather than just entering ids I know to be valid.

主类:

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));
  }
}

实用程序界面:

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);
}

因为我可以检查它是否正确设置了正在运行的进程的cpu亲和力在其中,我知道语法是正确的。

Since I can check that it correctly sets the cpu affinity of the process it's running in, I know the syntax is correct.

问题是:

如何访问/引用流程除了当前过程以外?

How do I access/reference processes besides the current process?

推荐答案

您正在寻找的另一个例程是 OpenProcess ,它使您可以将句柄转到另一个进程,然后可以在 SetProcessAffinityMask 中使用它。调用签名:

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. The calling signature:

HANDLE OpenProcess(DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwProcessId);

它已经暴露在Kernel32界面中。

It's exposed in the Kernel32 interface already.

已经被要求使用JNA 确定其他Java进程的进程ID ,并且应该是可以理解的。

Determining the process id of the other java processes using JNA has been asked already, and should be understandable.

这篇关于如何使用Java Native Access为Java.exe以外的进程设置进程亲和力?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆