c - 函数 ‘sched_setaffinity’ 的隐式声明

标签 c linux ubuntu gcc scheduled-tasks

我正在编写一个需要在单核上运行的程序。为了将它绑定(bind)到单核,我使用了 sched_setaffinity(),但编译器给出了警告:

implicit declaration of function ‘sched_setaffinity’

我的测试代码是:

#include <stdio.h>
#include <unistd.h>
#define _GNU_SOURCE
#include <sched.h>

int main()
{
    unsigned long cpuMask = 2;
    sched_setaffinity(0, sizeof(cpuMask), &cpuMask);
    printf("Hello world");
    //some other function calls
}

你能帮我弄清楚吗?实际上代码是编译运行的,但我不确定它是在单核上运行还是在切换核上运行。

我使用的是 Ubuntu 15.10 和 gcc 5.2.1 版

最佳答案

您需要将 #define _GNU_SOURCE 移到顶部。在 man sched_setaffinity 它说:

 #define _GNU_SOURCE             /* See feature_test_macros(7) */

man 7 feature_test_macros 中它说:

NOTE: In order to be effective, a feature test macro must be defined before including any header files. This can be done either in the compilation command (cc -DMACRO=value) or by defining the macro within the source code before including any headers.

所以在一天结束时,您的代码应该如下所示:

#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <sched.h>


int main()
{
    unsigned long cpuMask = 2;
    sched_setaffinity(0, sizeof(cpuMask), &cpuMask);
    printf("Hello world");
    //some other function calls
}

关于c - 函数 ‘sched_setaffinity’ 的隐式声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36794338/

相关文章:

c - 阿尔萨 API : how to use mmap in c?

c - malloc 还是不malloc,这是个问题!

linux - 如何在 unix 中删除文件的最后一个字符?

linux - 如何在适用于 Linux 的 Web 应用服务上配置应用服务身份验证

c - 越界递减指针;将其递增到边界

c++ - 3 字节 int 和 5 字节长?

linux - GitHub 永远在 Ubuntu 11.04 上推送/pull

ubuntu - 从主机推送文件后,可以在 lxc 容器中找到文件

python - 卸载 opencv 2.4.9 并安装 3.0.0

ubuntu - ubuntu中的hadoop命令和SLF4J错误信息cdh