doing

 

https://blog.csdn.net/zyhse/article/details/107596418

 

 


int sched_setaffinity(pid_t tid, size_t size, const cpu_set_t *set)
{
return syscall(SYS_sched_setaffinity, tid, size, set);
}

int pthread_setaffinity_np(pthread_t td, size_t size, const cpu_set_t *set)
{
return -__syscall(SYS_sched_setaffinity, td->tid, size, set);
}

static int do_getaffinity(pid_t tid, size_t size, cpu_set_t *set)
{
long ret = __syscall(SYS_sched_getaffinity, tid, size, set);
if (ret < 0) return ret;
if (ret < size) memset((char *)set+ret, 0, size-ret);
return 0;
}

#include <stdio.h>
#include <sys/sysinfo.h>

int main(int argc, char* argv[])
{
printf("cpu total: %d\n", get_nprocs_conf());
printf("cpu num: %d\n", get_nprocs());
return 0;
}




#include <sched.h>

//int sched_setaffinity(pid_t pid, size_t cpusetsize,
// cpu_set_t *mask);
//
//int sched_getaffinity(pid_t pid, size_t cpusetsize,
// cpu_set_t *mask);

#include "stdint.h"
int main(int argc, char* argv[])
{
cpu_set_t mask = {};
CPU_SET(1, &mask);
int ok = sched_setaffinity(0, sizeof(cpu_set_t), &mask);

int r = sched_getaffinity(0, sizeof(cpu_set_t), &mask);
int total = 0;
for (const uint8_t *p = (const uint8_t *)&mask, *end = p + sizeof(cpu_set_t); p < end; p++){
for (uint8_t t = *p; t; t >>= 1){
total += t & 01;
}
}
return 1;
}
posted @ 2021-06-03 10:17  zJanly  阅读(127)  评论(0编辑  收藏  举报