SetProcessAffinityMask在C ++中的使用示例? [英] Example usage of SetProcessAffinityMask in C++?

查看:453
本文介绍了SetProcessAffinityMask在C ++中的使用示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将各种c / c ++进程固定到机器上的特定内核,以便仅在Windows 7 64位上进行基准测试。我的机器有16个核心(2x8)。我试图这样做通过调用SetProcessAffinityMask从给定进程的代码。假设是正确的,我不确定如何使用这个函数。我已经看到了文档,但无法理解它的第二个参数需要是什么的描述。我也没有找到任何示例c / c + +使用在SO上或在谷歌搜索。

I need to pin various c/c++ processes to specific cores on a machine for benchmarking only on Windows 7 64-bit. My machine has 16 cores (2x8). I'm trying to do this by calling SetProcessAffinityMask from within the code for a given process. Assuming that's correct I am unsure of how exactly to use this function. I've seen the documentation but am unable to understand its description of what the second argument needs to be. I also haven't found any example c/c++ usage either on SO or on Google having searched.

问题1:以一个16核心机器(2cpux8) / c ++项目,请提供一个说明性示例,如何使用SetProcessAffinityMask选择每个16核心和第二个参数的解释我的理解?如何将核心ID从0-15转换为其等效位掩码?

Question1: Taking a 16 core machine (2cpux8) for example and a c/c++ project would you please provide an illustrative example for how to use SetProcessAffinityMask to pick each of the 16 cores and an explanation of the second argument for my understanding? How would I translate a core id from 0-15 to its equivalent bitmask?

问题2:如果有2x8内核而不是16核心在一个CPU?或者是相同的用法吗?

Question2: Does it make a difference to the usage if there are 2x8 cores as opposed to 16 cores on one cpu? Or is it the same usage?

非常感谢。这是我到目前为止。

Many thanks. Here's what I have so far.

#include <Windows.h>
#include <iostream>

using namespace std;

int main () {

    HANDLE process = GetCurrentProcess();

    DWORD_PTR processAffinityMask = 0; /// What to do here?

    BOOL success = SetProcessAffinityMask(process, processAffinityMask);

    cout << success << endl;

    return 0;

}


推荐答案

参数是一个位掩码,其中一个位设置意味着进程可以在该处理程序上运行,一个位清楚意味着它不能。

The second parameter is a bitmask, where a bit that's set means the process can run on that proceesor, and a bit that's clear means it can't.

在你的情况下,让每个进程运行在一个单独的核心你可以(一种可能性)通过一个命令行参数给每个进程一个数字,并使用该进程内的数字确定使用的处理器:

In your case, to have each process run on a separate core you could (for one possibility) pass a command line argument giving each process a number, and use that number inside the process to determine the processor to use:

#include <Windows.h>
#include <iostream>

using namespace std;

int main (int argc, char **argv) {
    HANDLE process = GetCurrentProcess();
    DWORD_PTR processAffinityMask = 1 << atoi(argv[1]);

    BOOL success = SetProcessAffinityMask(process, processAffinityMask);

    cout << success << endl;
    return 0;
}

然后你可以用下面的命令运行:

Then you'd run this with something like:

for %c in (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15) do test %c

这篇关于SetProcessAffinityMask在C ++中的使用示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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