undefined reference to'CPU_ZERO'问题

chinawhom 2012-03-25 11:36:19
我在使用sched_setaffinity将线程和CPU绑定,但总显示“undefined reference to'CPU_ZERO'”。我在文件头已经写了
#define __USE_GNU
#include <sched.h>
为什么出现这些问题,怎样解决?谢谢
...全文
2195 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq120848369 2012-05-28
  • 打赏
  • 举报
回复
       #define _GNU_SOURCE
#include <sched.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <assert.h>

int
main(int argc, char *argv[])
{
cpu_set_t *cpusetp;
size_t size;
int num_cpus, cpu;

if (argc < 2) {
fprintf(stderr, "Usage: %s <num-cpus>\n", argv[0]);
exit(EXIT_FAILURE);
}

num_cpus = atoi(argv[1]);

cpusetp = CPU_ALLOC(num_cpus);
if (cpusetp == NULL) {
perror("CPU_ALLOC");
exit(EXIT_FAILURE);
}

size = CPU_ALLOC_SIZE(num_cpus);

CPU_ZERO_S(size, cpusetp);
for (cpu = 0; cpu < num_cpus; cpu += 2)
CPU_SET_S(cpu, size, cpusetp);

printf("CPU_COUNT() of set: %d\n", CPU_COUNT_S(size, cpusetp));

CPU_FREE(cpusetp);
exit(EXIT_SUCCESS);
}


manpage里,运行毫无为题。
深夜航船 2012-05-28
  • 打赏
  • 举报
回复
1.你看看是不是包含了pthread.h 在sched.h之前如果是这样,那么把他放到后面去。
2.用gcc -E选项来查看一下预处理的代码

good lucky

23,223

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧