2013-04-27 53 views
0
#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的。 我哪里错了?

回答

1

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

+0

Thx。那么试验内核的API并构建一个可用的示例代码的正确方法是什么? – FabriX 2013-04-27 11:00:38

+0

我想你可以创建一个内核模块来试验内核的API。 – 2013-04-29 02:21:54

+0

这个快速手册将回答你的问题http://tldp.org/LDP/lkmpg/2.6/html/ – 2013-05-01 12:12:37