是否有可能设置亲和力Android中了sched_setaffinity? [英] Is it possible to set affinity with sched_setaffinity in Android?
问题描述
是否有可能设置与Android NDK编译本地C code CPU的亲和力?由于系统使用的是Linux内核,它应该是可以使用了sched_setaffinity / sched_getaffinity功能,但是当我用NDK编译,我得到的错误抱怨cpu_set_t类型未知(这是作为参数传递给函数) 。是否有任何其他的方式来做到这一点?当我与codeSourcerys ARM编译器(ARM-NONE-Linux的gnueabi-GCC)编译这似乎并不成为一个问题,因此该系统明显支持所需的系统调用。
Is it possible to set CPU affinity in native C code compiled with the Android NDK? Since the system is using a Linux kernel, it should be possible to use the sched_setaffinity/sched_getaffinity functions, but when I compile with the NDK, I get errors complaining that the cpu_set_t type is unknown (which is used as an argument to the functions). Is there any other way to accomplish this? When I compile with CodeSourcerys ARM compiler (arm-none-linux-gnueabi-gcc) this does not seem to be a problem, so the system obviously supports the required syscalls.
推荐答案
以下code。与NDK R5或行之有效的更新:
The following code works well with NDK r5 or newer:
#include <sys/syscall.h>
#include <pthread.h>
void setCurrentThreadAffinityMask(int mask)
{
int err, syscallres;
pid_t pid = gettid();
syscallres = syscall(__NR_sched_setaffinity, pid, sizeof(mask), &mask);
if (syscallres)
{
err = errno;
LOGE("Error in the syscall setaffinity: mask=%d=0x%x err=%d=0x%x", mask, mask, err, err);
}
}
这篇关于是否有可能设置亲和力Android中了sched_setaffinity?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!