Skip to content Skip to sidebar Skip to footer

Is It Possible To Set Affinity With Sched_setaffinity In Android?

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/sche

Solution 1:

The following code works well with NDK r5 or newer:

#include<sys/syscall.h>#include<pthread.h>voidsetCurrentThreadAffinityMask(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);
    }
}

Solution 2:

For cpu_set_t, I was able to compile with -D_GNU_SOURCE=1 option for Android NDK.

Post a Comment for "Is It Possible To Set Affinity With Sched_setaffinity In Android?"