Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何包含Linux头文件,如Linux/getcpu.h?_C_Linux_Include Path - Fatal编程技术网

如何包含Linux头文件,如Linux/getcpu.h?

如何包含Linux头文件,如Linux/getcpu.h?,c,linux,include-path,C,Linux,Include Path,我使用的是Linux 3.5.0-17-generic#28 Ubuntu SMP周二10月9日19:31:23 UTC 2012 x86_64 GNU/Linux,我需要#包括。编译器抱怨找不到该文件。linux的头文件在哪里?简短回答:通常,您不直接包含这些头文件 其中大多数特定于操作系统/计算机的标头都会自动包含在更通用的标头中。那些不是linux独有的功能,可能对您正在运行的版本可用,也可能不可用 至于getcpu,有一个更标准的版本称为sched_getcpu,它位于sched.h中,

我使用的是
Linux 3.5.0-17-generic#28 Ubuntu SMP周二10月9日19:31:23 UTC 2012 x86_64 GNU/Linux
,我需要
#包括
。编译器抱怨找不到该文件。linux的头文件在哪里?

简短回答:通常,您不直接包含这些头文件

其中大多数特定于操作系统/计算机的标头都会自动包含在更通用的标头中。那些不是linux独有的功能,可能对您正在运行的版本可用,也可能不可用

至于
getcpu
,有一个更标准的版本称为
sched_getcpu
,它位于
sched.h
中,具有相同的功能

或者,您可以测试系统调用是否可用,并手动调用:

#define _GNU_SOURCE  
#include <unistd.h>
#include <sys/syscall.h>

static inline int getcpu() {
    #ifdef SYS_getcpu
    int cpu, status;
    status = syscall(SYS_getcpu, &cpu, NULL, NULL);
    return (status == -1) ? status : cpu;
    #else
    return -1; // unavailable
    #endif
}
定义GNU源
#包括
#包括
静态内联int getcpu(){
#ifdef系统
int cpu,状态;
status=syscall(SYS\u getcpu,&cpu,NULL,NULL);
返回(状态==-1)?状态:cpu;
#否则
return-1;//不可用
#恩迪夫
}

如果syscall返回-1,变量errno(
#include
)会给出错误代码。

glibc现在定义
sched.h下的
getcpu()

从Ubuntu 20.04 glibc 2.31开始测试,
#include
已经通过
#include
原型定义了一个
getcpu
函数:

extern int getcpu (unsigned int *, unsigned int *) __THROW;
而且似乎效果不错:

getcpu.c

#define _GNU_SOURCE
#include <assert.h>
#include <sched.h> /* getcpu */
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>

void* main_thread(void *arg) {
    (void)arg;
    unsigned cpu, numa;
    getcpu(&cpu, &numa);
    printf("%u %u\n", cpu, numa);
    return NULL;
}

int main(int argc, char **argv) {
    pthread_t *threads;
    unsigned int nthreads, i;
    if (argc > 1) {
        nthreads = strtoll(argv[1], NULL, 0);
    } else {
        nthreads = 1;
    }
    threads = malloc(nthreads * sizeof(*threads));
    for (i = 0; i < nthreads; ++i) {
        assert(pthread_create(
            &threads[i],
            NULL,
            main_thread,
            NULL
        ) == 0);
    }
    for (i = 0; i < nthreads; ++i) {
        pthread_join(threads[i], NULL);
    }
    free(threads);
    return EXIT_SUCCESS;
}
我的8芯笔记本电脑上的输出示例:

5 0
4 0
7 0
3 0

mangetcpu
说它可以在
#include
中找到,但这对我的系统来说是错误的。一个
定位getcpu.h
显示他唯一的命中目标是:
/usr/src/linux-headers-5.4.0-29/include/linux/getcpu.h
,但这只定义了
结构getcpu\u缓存
,其他什么都没有。

你在写内核模块吗?首先检查它们是否真的安装在你的系统上(package
linux-header*
)@n.m.否,我需要用户级的getcpu()函数。通常情况下,您并不真正需要它,您可以将NULL作为第三个参数传递(如果您想要最新的未缓存答案,您应该传递NULL)。顺便说一句,它通常位于
/usr/src/linux/include/linux/getcpu.h
(如果您的系统上安装了
linux header*
)。
5 0
4 0
7 0
3 0