linux - 尝试使用 "current"宏时出现编译错误

标签 linux kernel task

#include <linux/sched.h>
#include <stdio.h>
#include <stdlib.h>

int
main(int argc, char *argv[])
{
    struct task_struct *task;
    task = current;
    printf("Proc PID is %ld\n",(long)task->pid);
    exit(EXIT_SUCCESS);
}

当我尝试编译时出现以下错误:

*current.c: In function ‘main’:
current.c:9:9: error: ‘current’ undeclared (first use in this function)
current.c:9:9: note: each undeclared identifier is reported only once for each function it appears in
current.c:10:40: error: dereferencing pointer to incomplete type*

我使用的是 Linux 内核 3.2.0.4-amd64。 我哪里错了?

最佳答案

您是否尝试在用户空间编程中使用 Linux Kernel 的数据结构、变量?如果是这样,你就错了。 task_struct 是 Linux 内核的内部数据结构。 current 也是 Linux 内核内部寄存器变量。用户空间程序不能使用它们。

关于linux - 尝试使用 "current"宏时出现编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16247516/

相关文章:

c - 在 for 循环 C 中使用 fork() 时出现奇怪的输出

linux - ARM 如何在 IRQ 处理程序中切换到 svc 模式?

assembly - 当我切换到用户模式时,为什么我的 CPSR 中会出现垃圾?

Java生产者/消费者,检测处理结束

java - 自动将文件从 Linux 服务器传输到 Windows 机器

c - 我应该在 C 中声明系统调用函数吗?

linux - AWK 在 system() 中插入变量值

linux - 将参数从挂载传递到内核模块

c# - .net 中的 Task.Delay 提前 125 毫秒触发

singleton - celery :任务单例?