本文介绍了隐式函数声明'sched_setaffinity'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在编写一个需要在单核上运行的程序。为了将它绑定到单核上,我使用了 sched_setaffinity()
,但编译器给出了警告:
因此,在一天结束时,您的代码应如下所示:
# define _GNU_SOURCE
#include
#include< unistd.h>
#include< sched.h>
int main()
{
unsigned long cpuMask = 2;
sched_setaffinity(0,sizeof(cpuMask),& cpuMask);
printf(Hello world);
//一些其他函数调用
}
I'm writing a program which needed to be run on single core. To bind it to single core, I'm using sched_setaffinity()
, but the compiler gives warning:
My test code is:
#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
}
Can you please help me to figure it out. Actually code is compiled and run, but I'm not sure whether it is running on single core or is switching cores.
I'm using Ubuntu 15.10 and gcc version 5.2.1
解决方案
You need to move #define _GNU_SOURCE
to a top. In man sched_setaffinity
it says:
#define _GNU_SOURCE /* See feature_test_macros(7) */
while in man 7 feature_test_macros
it says:
So at the end of the day your code should look like this:
#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
}
这篇关于隐式函数声明'sched_setaffinity'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!