[英]how to define a global variable inside linux kernel?
I am new to kernel programming and trying to implement a system call in linux kernel 3.19 which keeps track of the processes in a linked list. 我是内核编程的新手,正在尝试在Linux内核3.19中实现系统调用,该系统跟踪链接列表中的进程。 So every time the system call is invoked from user-space (by some wrapper function) a new process has to be added to that list.
因此,每次从用户空间(通过某些包装函数)调用系统调用时,都必须将新进程添加到该列表中。 My system call looks something like
我的系统调用看起来像
asmlinkage long sys_newcall(pid_t pid)
{
/*
* mytasks is the name of the structure
* kmalloc() is invoked to create an instance
*/
struct mytasks newTask = kmalloc(sizeof(struct mytasks), GFP_KERNEL);
/* various checks */
/* now adding the new instance to the list */
list_add_tail(&(newTask->list),&(mylist->list));
/* i have put list_head struct in my own structure to make use of above interface */
}
Now the mylist
variable used above should be defined global so as to maintain the list for subsequent system calls. 现在,应将上面使用的
mylist
变量定义为全局变量,以维护后续系统调用的列表。 How to accomplish that? 如何做到这一点? Do I have to declare
mylist
variable in linux/init/main.c
or I can simply use EXPORT_GLOBAL
. 我必须在
linux/init/main.c
声明mylist
变量,还是可以简单地使用EXPORT_GLOBAL
。 I also read about using extern but couldn't figure out where to declare and define the variable. 我还阅读了有关使用extern的信息,但无法弄清楚在何处声明和定义变量。
最好使用EXPORT_SYMBOL,因为这对于可加载模块也是可见的,并将其声明为正在使用的文件或本地头文件中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.