c - 在 C 中设置 CPU 亲和性的问题

标签 c linux compiler-errors cpu compiler-warnings

我正在尝试计算 CPUID 指令运行所需的时间。

来源:

#include <stdio.h>
#include <sched.h>

cpu_set_t mask;

CPU_ZERO(&mask);
CPU_SET(0, &mask);
sched_setaffinity(0, sizeof(mask), &mask);

static inline unsigned long long tick()
{
    unsigned long long d;
    asm volatile ("rdtsc" : "=A" (d));
    return d;
}

void cpuid(void)
{
    int i;

    for(i=0; i != 5; i++)
    {
         asm volatile ("cpuid");
    }
}

int main()
{
    long long bef;
    long long aft;
    long long dif;

    bef=tick();
    cpuid();
    aft=tick();
    dif=aft-bef;

    printf("%d\n", bef);
    printf("%d\n", aft);
    printF("%d\n", dif);

    return 0;
}

现在我正在使用以下内容进行编译

gcc -D_GNU_SOURCE -o test test.c

我在不是文件的代码上遇到错误! 例如:

test.c:6:1: error: expected identifier or '(' before 'do'
test.c:6:1: error: expected identifier or '(' before 'while'
test.c:7:1: error: expected identifier or '(' before '__extension__'
test.c:8:1: warning: data definition has no type or storage class [enable by def...
test.c:8:1: error: intializer element is not constant

“def...”实际上并不是它的输出,因为我的终端窗口很小。我在 ESXi 中工作。

任何帮助都会很棒!!!

对于 future 的读者

用户@Iwillnotexist Idonotexist 说使用以下函数以获得完整的 x86 和 x64 支持是正确的。

static __inline__ unsigned long long rdtsc(void)
{
  unsigned hi, lo;
  __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
  return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
}

最佳答案

您有函数外的代码。那是不允许的。将以下内容移至 main:

CPU_ZERO(&mask);
CPU_SET(0, &mask);
sched_setaffinity(0, sizeof(mask), &mask);

关于c - 在 C 中设置 CPU 亲和性的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23203322/

相关文章:

c++ - 什么是变量的链接和存储说明符?

c - semaphore.c 中的等待列表实现

linux - 如何为作为可分发包安装的应用程序获取对/var/lib 文件夹的写入权限

c++ - 使用 g++4.5.2 编译时对类的引用不明确

c - 使用 TCP 客户端/服务器的生产者/消费者

c - 读取多行输入并将整数保存到数组中

python - 我如何使用 python 检测到有东西插入了 linux 上的音频插孔?

linux - 根据邮政编码打印名称

c++ - 使用atoi (“argument of type ' int *' is incompatible with type ' const char *' ”)并添加数组时出错

使用 makefile 编译 C 项目